import type { VxeModalDefines } from 'vxe-pc-ui'

import { VxeUI } from 'vxe-pc-ui'

import obj from './obj'

type ConfirmOptions = VxeModalDefines.ConfirmOptions & {
  content: string
  onConfirm?: () => void
  title?: string
}

type ToastOptions =
  | string
  | {
      content: string
      duration?: number
      id?: any
    }

type LoadOptions = {
  error?: () => Promise<void>
  hideError?: boolean // 是否隐藏失败的toast
  hideSuccess?: boolean // 是否隐藏成功的toast
  option?: string
  success?: () => Promise<void>
  task: () => Promise<void>
}

const _toast = (options: ToastOptions, status: any) => {
  if (!options) return
  if (typeof options === 'string') {
    options = {
      content: options,
    }
  }
  // options.duration = status === 'loading' ? -1 : options.duration || 2000
  options.duration = options.duration || 2000
  VxeUI.modal.message({
    ...options,
    status,
  })
}

const confirm = (options: ConfirmOptions) => {
  options.title = options.title || '提醒'
  VxeUI.modal.confirm(options)
}

const alert = (options: ConfirmOptions | string) => {
  if (typeof options === 'string') {
    options = {
      content: options,
    }
  }
  options.title = options.title || '提醒'
  VxeUI.modal.alert(options)
}

const toastLoading = (options: ToastOptions) => {
  _toast(options, 'loading')
}

const success = (options: ToastOptions) => {
  _toast(options, 'success')
}

const error = (options: ToastOptions) => {
  _toast(options, 'error')
}

const warning = (options: ToastOptions) => {
  _toast(options, 'warning')
}

const hideToast = (id?: any) => {
  VxeUI.modal.close(id)
}

const confirmLoad = (
  confirmOptions: ConfirmOptions | string,
  loadOptions: LoadOptions,
) => {
  if (typeof confirmOptions === 'string') {
    confirmOptions = {
      content: confirmOptions,
    } as ConfirmOptions
  }
  confirm({
    ...confirmOptions,
    onConfirm: () => {
      load(loadOptions)
    },
  })
}

const _load = async (options: LoadOptions, toast: boolean = false) => {
  let loadingContent = '加载中...'
  let successContent = '操作成功！'
  let errorContent = '操作失败！'
  if (options?.option) {
    loadingContent = `${options.option}中...`
    successContent = `${options.option}成功！`
    errorContent = `${options.option}失败！`
  }

  // 默认不想提示成功
  if (options.hideSuccess === undefined) {
    options.hideSuccess = true
  }
  if (options.hideError === undefined) {
    options.hideError = true
  }

  let toastId
  if (toast) {
    toastId = obj.snowflakeId()
    toastLoading({
      content: loadingContent,
      id: toastId,
    })
  } else {
    loading(loadingContent)
  }

  try {
    await options.task()
    !options.hideSuccess && success(successContent)
    options?.success && (await options.success())
  } catch (error_: any) {
    if (error_?.message) {
      errorContent += `${error_.message}`
    }
    !options.hideError && error(errorContent)
    options?.error && (await options.error())
  } finally {
    if (toast) {
      hideToast(toastId)
    } else {
      hide()
    }
  }
}

const toastLoad = async (options: LoadOptions) => {
  return await _load(options, true)
}

const load = async (options: LoadOptions) => {
  return await _load(options)
}

const loading = (content?: string) => {
  VxeUI.loading.open({
    text: content || '加载中...',
  })
}

const hide = () => {
  VxeUI.loading.close()
}

// function highlight(txt: any, highColor = '#f00a0a') {
//   return `<span style="color:${highColor}">${txt}</span>`
// }
const highlight = (txt: any) => {
  return txt ? `【${txt}】` : ''
}

export default {
  highlight,
  confirm,
  alert,
  success,
  error,
  warning,
  load,
  loading,
  hide,
  hideToast,
  confirmLoad,
  toastLoad,
  toastLoading,
}
