/**
 使用方法：Alert('title', 'content') 或者 Alert('content') 或者 Alert.info('title', 'content') 或者 Alert.info('content')
 Alert.success, Alert.error, Alert.warning 使用方法同理

 注意，Confirm({...}) 或者 Confirm.info({...}) 方法参数与上面的不同，由于confirm有回调函数，故传参为
 Confirm.info({
      title?: string; // 标题，可选
      content: string; // 内容，必选
      okText?: string; // 可选，默认为 '确认'
      cancelText?: string; // 可选，默认为 '取消'
      onOk?: () => void; // 确认按钮回调
      onCancel?: () => void; // 取消按钮回调
    })
 Confirm.success, Confirm.error, Confirm.warning 使用方法同理
 */
import { VNode } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';

type MessageBoxType = 'info' | 'success' | 'error' | 'warning' | undefined;

function alert(type: MessageBoxType, content: string, callback?: (action: string) => void) {
  ElMessageBox.alert(content, {
    type,
    showClose: false,
    callback,
  });
}

const Alert = (content: string, callback?: (action: string) => void): void => {
  alert(undefined, content, callback);
};
Alert.info = (content: string, callback?: (action: string) => void): void => {
  alert('info', content, callback);
};
Alert.success = (content: string, callback?: (action: string) => void): void => {
  alert('success', content, callback);
};
Alert.error = (content: string, callback?: (action: string) => void): void => {
  alert('error', content, callback);
};
Alert.warning = (content: string, callback?: (action: string) => void): void => {
  alert('warning', content, callback);
};

interface ConfirmParam {
  title?: string;
  content: string;
  okText?: string;
  cancelText?: string;
  onOk?: () => void;
  onCancel?: () => void;
}

// options的属性可为element-plus notification-box options的任意一项:https://element-plus.gitee.io/#/zh-CN/component/message-box
function confirm(type: MessageBoxType, param: ConfirmParam, options?: Record<string, any>) {
  const { title, content, onOk, onCancel } = param;
  let { okText, cancelText } = param;
  if (!okText) {
    okText = '确认';
  }

  if (!cancelText) {
    cancelText = '取消';
  }
  const message = content;
  const head = title;

  const props = {
    confirmButtonText: okText,
    cancelButtonText: cancelText,
    type,
    showClose: false,
  };

  if (options) {
    Object.assign(props, options);
  }

  let promise;
  if (message && head) {
    promise = ElMessageBox.confirm(message, head, props);
  } else if (message) {
    promise = ElMessageBox.confirm(message, props);
  }
  promise &&
    promise
      .then(() => {
        onOk && onOk();
      })
      .catch(() => {
        onCancel && onCancel();
      });
}

const Confirm = (param: ConfirmParam, options?: Record<string, any>): void => {
  confirm(undefined, param, options);
};
Confirm.info = (param: ConfirmParam, options?: Record<string, any>): void => {
  confirm('info', param, options);
};
Confirm.success = (param: ConfirmParam, options?: Record<string, any>): void => {
  confirm('success', param, options);
};
Confirm.error = (param: ConfirmParam, options?: Record<string, any>): void => {
  confirm('error', param, options);
};
Confirm.warning = (param: ConfirmParam, options?: Record<string, any>): void => {
  confirm('warning', param, options);
};

enum MessageType {
  SUCCESS = 'success',
  WARNING = 'warning',
  INFO = 'info',
  ERROR = 'error',
}
interface IMessageConfig {
  message: string | VNode;
  type: MessageType;
  iconClass: string;
  dangerouslyUseHTMLString: boolean;
  customClass: string;
  duration: number;
  showClose: boolean;
  center: boolean;
  onClose: () => void;
  offset: number;
}

interface IMessageHandle {
  close: () => void;
}

const baseMessageConfig: Partial<IMessageConfig> = {
  duration: 3000,
  // offset: 30,
};

/* 提示 */
// 可统一共用的提示文本
export enum MessageContent {
  PERMISSION = '对不起，您没有权限进行该操作',
  CELL_VALIDATE = '输入的数据类型不对，请重新输入',
}
const Message = (message: string, options: Partial<IMessageConfig> = {}): IMessageHandle => {
  return ElMessage({ message, ...baseMessageConfig, ...options });
};

Message.success = (message: string, options: Partial<IMessageConfig> = {}): IMessageHandle => {
  return ElMessage({ message, ...baseMessageConfig, ...options, type: MessageType.SUCCESS });
};

Message.warning = (message: string, options: Partial<IMessageConfig> = {}): IMessageHandle => {
  return ElMessage({ message, ...baseMessageConfig, ...options, type: MessageType.WARNING });
};

Message.info = (message: string, options: Partial<IMessageConfig> = {}): IMessageHandle => {
  return ElMessage({ message, ...baseMessageConfig, ...options, type: MessageType.INFO });
};

Message.error = (message: string, options: Partial<IMessageConfig> = {}): IMessageHandle => {
  return ElMessage({ message, ...baseMessageConfig, ...options, type: MessageType.ERROR });
};

export const asyncConfirm = (content: string, title?: string) => {
  return new Promise<boolean>((resolve, reject) => {
    Confirm.warning({
      content,
      title,
      onOk: () => {
        resolve(true);
      },
      onCancel: () => {
        console.log('cancel');
        resolve(false);
      },
    });
  });
};

export { Alert, Confirm, Message };
