import React from 'react';
import { toast as sonnerToast } from 'sonner';
import { CheckCircle2, XCircle, AlertTriangle, Info, Loader2 } from 'lucide-react';

type SonnerToast = typeof sonnerToast;
type ToastOptions = Parameters<SonnerToast>[1];
type ToastAction = {
  label: string;
  onClick: () => void;
};

const withDuration = (options: ToastOptions | undefined, duration: number) => ({
  ...options,
  duration: options?.duration ?? duration,
});

const iconClass = (color: string) => ({
  className: `h-4 w-4 ${color}`,
});

const icons = {
  success: React.createElement(CheckCircle2, iconClass('text-[#00b42a]')),
  error: React.createElement(XCircle, iconClass('text-[#f53f3f]')),
  warning: React.createElement(AlertTriangle, iconClass('text-[#ff7d00]')),
  info: React.createElement(Info, iconClass('text-[#3370ff]')),
  loading: React.createElement(Loader2, {
    className: 'h-4 w-4 animate-spin text-[#3370ff]',
  }),
  neutral: React.createElement(Info, iconClass('text-[#8f959e]')),
};

/**
 * 现代化的通知系统
 * 使用 Sonner 库提供流畅的 Toast 通知
 */

export const toast = {
  /**
   * 成功通知
   */
  success: (message: string, options?: ToastOptions) => {
    sonnerToast.success(message, withDuration({
      icon: icons.success,
      ...options,
    }, 4000));
  },

  /**
   * 错误通知
   */
  error: (message: string, options?: ToastOptions) => {
    sonnerToast.error(message, withDuration({
      icon: icons.error,
      ...options,
    }, 5000));
  },

  /**
   * 警告通知
   */
  warning: (message: string, options?: ToastOptions) => {
    sonnerToast.warning(message, withDuration({
      icon: icons.warning,
      ...options,
    }, 4000));
  },

  /**
   * 信息通知
   */
  info: (message: string, options?: ToastOptions) => {
    sonnerToast.info(message, withDuration({
      icon: icons.info,
      ...options,
    }, 4000));
  },

  /**
   * 加载中通知
   */
  loading: (message: string, options?: ToastOptions) => {
    return sonnerToast.info(message, withDuration({
      icon: icons.loading,
      ...options,
    }, 0));
  },

  /**
   * 中性通知
   */
  neutral: (message: string, options?: ToastOptions) => {
    sonnerToast(message, withDuration({
      icon: icons.neutral,
      ...options,
    }, 4000));
  },

  /**
   * 带操作按钮的通知
   */
  action: (message: string, action: ToastAction, options?: ToastOptions) => {
    sonnerToast.warning(message, withDuration({
      icon: icons.warning,
      ...options,
      action,
    }, 6000));
  },

  /**
   * 加载通知（带 Promise）
   */
  promise: <T,>(
    promise: Promise<T>,
    {
      loading,
      success,
      error,
    }: {
      loading: string;
      success: string | ((data: T) => string);
      error: string | ((error: any) => string);
    }
  ) => {
    return sonnerToast.promise(promise, {
      loading,
      success,
      error,
    });
  },

  /**
   * 自定义通知
   */
  custom: (message: string, options?: any) => {
    sonnerToast(message, options);
  },

  /**
   * 关闭所有通知
   */
  dismiss: (toastId?: string | number) => {
    sonnerToast.dismiss(toastId);
  },
};
