/**
 * 消息提示工具
 */
export class MessageExtend {
  // 消息通知
  static Notify(message: any, type?: 'primary' | 'success' | 'danger' | 'warning') {
    showNotify({ type, message })
  }

  //  提示弹窗
  static ShowDialog(message: string, title?: string) {
    showDialog({ title: title, message: message })
  }

  //  确认提示弹窗
  static Dialog(title: string, message: string, onConfirm?: () => void, onCancel?: () => void, confirmButtonText?: string) {
    showConfirmDialog({
      title: title,
      message: message,
      confirmButtonText: confirmButtonText || '确认',
    })
      .then(() => {
        onConfirm?.()
      })
      .catch(() => {
        onCancel?.()
      })
  }

  // 成功失败默认提示
  static ShowToast(text: any, type?: 'success' | 'fail' | 'default') {
    if (type == 'success') {
      showSuccessToast(text)
    } else if (type == 'fail') {
      showFailToast(text)
    } else {      
      console.log(text);
      showToast(text)
    }
  }

  // 自定义图标提示
  static ShowIconToast(icon: any, text: any) {
    showToast({
      message: text,
      icon: icon,
    })
  }

  // 多条消息通知（堆叠展示）
  static NotifyList(messages: string[], type: 'primary' | 'success' | 'danger' | 'warning', duration: number = 3000) {
    if (typeof window === 'undefined') return
    if (!messages || messages.length === 0) return

    const colorMap = {
      primary: '#1989fa',
      success: '#07c160',
      danger: '#ee0a24',
      warning: '#ff976a',
    }

    let container = document.getElementById('custom-notify-container')
    if (!container) {
      container = document.createElement('div')
      container.id = 'custom-notify-container'
      document.body.appendChild(container)
    }

    messages.forEach((message, index) => {
      const item = document.createElement('div')
      item.className = 'custom-notify-item'
      item.style.backgroundColor = colorMap[type]
      item.textContent = String(message)
      item.style.animationDelay = `${index * 100}ms`

      item.addEventListener('click', () => {
        this.closeNotifyItem(item)
      })

      container!.appendChild(item)

      setTimeout(() => {
        this.closeNotifyItem(item)
      }, duration)
    })
  }

  // 关闭单条通知
  private static closeNotifyItem(item: HTMLElement) {
    if (!item || item.classList.contains('leaving')) return
    item.classList.add('leaving')
    item.addEventListener('animationend', () => {
      item.remove()
      const container = document.getElementById('custom-notify-container')
      if (container && container.children.length === 0) {
        container.remove()
      }
    })
  }
}
