import { toaster } from '@/components/ui/toaster';
import {
  AiOutlineInfoCircle,
  AiFillWarning,
  AiFillCheckCircle,
  AiFillExclamationCircle,
  AiOutlineLoading,
} from 'react-icons/ai';

const surfaceStyle = {
  borderRadius: '1rem',
  border: '1px solid rgba(255,255,255,0.3)',
  backdropFilter: 'blur(10px)',
  background: 'rgba(255, 255, 255, 0.1)',
  color: 'white',
};

/**
 * 通用通知组件
 * @type {{
 *   info({title: *, description: *, button: *}): void,
 *   success({title: *, description: *, button: *}): void,
 *   warn({title: *, description: *, button: *}): void,
 *   error({title: *, description: *, button: *}): void,
 *   promise({promise: *, loading: *, success: *, error: *}): void
 * }}
 */
const Notification = {
  /**
   * 信息
   * @param title 标题
   * @param description 描述
   * @param button 按钮
   */
  info({ title, description, button }) {
    toaster.create({
      title,
      description,
      type: 'info',
      duration: 3000,
      icon: <AiOutlineInfoCircle size={24} />,
      actionLabel: button?.label,
      action: button?.onClick,
      style: surfaceStyle,
    });
  },

  /**
   * 成功信息
   * @param title 标题
   * @param description 描述
   * @param button 按钮
   */
  success({ title, description, button }) {
    toaster.create({
      title,
      description,
      type: 'success',
      duration: 3000,
      icon: <AiFillCheckCircle size={24} />,
      actionLabel: button?.label,
      action: button?.onClick,
      style: surfaceStyle,
    });
  },

  /**
   * 异步操作通知
   * @param {Object} params 参数对象
   * @param {Promise} params.promise 异步操作Promise
   * @param {Object} params.loading 加载中状态配置
   * @param {Object} params.success 成功状态配置
   * @param {Object} params.error 错误状态配置
   * @returns {{id: string | undefined, unwrap: () => Promise<unknown>}}
   */
  async promise({ promise, loading, success, error }) {
    return toaster.promise(promise, {
      loading: {
        title: loading.title,
        description: loading.description,
        icon: <AiOutlineLoading className={'animate-spin'} size={24} />,
        style: surfaceStyle,
      },
      success: {
        title: success.title,
        description: success.description,
        icon: <AiFillCheckCircle size={24} />,
        style: surfaceStyle,
      },
      error: {
        title: error.title,
        description: error.description,
        icon: <AiFillWarning size={24} />,
        style: surfaceStyle,
      },
    });
  },

  /**
   * 警告
   * @param title 标题
   * @param description 描述
   * @param button 按钮
   */
  warn({ title, description, button }) {
    toaster.create({
      title,
      description,
      type: 'warning',
      duration: 3000,
      icon: <AiFillExclamationCircle size={24} />,
      actionLabel: button?.label,
      action: button?.onClick,
      style: surfaceStyle,
    });
  },

  /**
   * 错误提示
   * @param title 标题
   * @param description 描述
   * @param button 按钮
   */
  error({ title, description, button }) {
    toaster.create({
      title,
      description,
      type: 'error',
      duration: 3000,
      icon: <AiFillWarning size={24} />,
      actionLabel: button?.label,
      action: button?.onClick,
      style: surfaceStyle,
    });
  },
};

export default Notification;
