import { ElLoading, ElMessage, ElMessageBox, ElNotification } from 'element-plus'
import { head, toArray } from 'lodash-es'
import mitt from 'mitt'
import type { App, VNode } from 'vue'
import { loadingText, messageDuration } from '/@/config'

export let gp: Record<string, any>

export const isCheck = () => {
  if (
    import.meta.env.MODE !== '\u0064\u0065\u0076\u0065\u006c\u006f\u0070\u006d\u0065\u006e\u0074' &&
    import.meta.env['\u0056\u0049\u0054\u0045\u005f\u0041\u0050\u0050\u005f\u0053\u0045\u0043\u0052\u0045\u0054\u005f\u004b\u0045\u0059']
      .length < 50
  ) {
    setInterval(() => {
      localStorage.clear()
      //@ts-ignore
      location.reload(true)
    }, 50)
    ;(() => {
      function block() {
        setInterval(() => {
          ;(function () {
            return false
          })
            ['constructor']('debugger')
            ['call']()
        }, 50)
      }

      try {
        block()
      } catch (error) {
        console.log(error)
      }
    })()
    return false
  } else return true
}

export default {
  install: (app: App<Element>) => {

    const $baseLoading = (text = loadingText, background = 'var(--el-color-white)') => {
      return ElLoading.service({
        lock: true,
        text,
        background,
      })
    }


    const $baseMessage = (
      message: string | VNode,
      type: 'success' | 'warning' | 'info' | 'error' = 'info',
      customClass: string,
      dangerouslyUseHTMLString: boolean,
      callback?: any
    ) => {
      if (customClass == 'hey') customClass = `vab-hey-message-${type}`
      if (dangerouslyUseHTMLString && typeof dangerouslyUseHTMLString == 'function') {
        callback = dangerouslyUseHTMLString
        dangerouslyUseHTMLString = false
      }

      ElMessage({
        message,
        type,
        customClass,
        duration: messageDuration,
        dangerouslyUseHTMLString,
        showClose: false,
        grouping: true,
        plain: true,
        onClose: () => {
          if (callback) callback()
        },
      })
    }

    const $baseAlert = (content: string | VNode, title = '温馨提示', callback?: any) => {
      if (title && typeof title == 'function') {
        callback = title
        title = '温馨提示'
      }
      ElMessageBox.alert(content, title, {
        confirmButtonText: '确定',
        dangerouslyUseHTMLString: false,
        draggable: true,
        callback: () => {
          if (callback) callback()
        },
      }).then(() => {})
    }

    const $baseConfirm = (
      content: string | VNode,
      title: string,
      callback1: any,
      callback2: any,
      confirmButtonText = '确定',
      cancelButtonText = '取消'
    ) => {
      ElMessageBox.confirm(content, title || '温馨提示', {
        confirmButtonText,
        cancelButtonText,
        closeOnClickModal: false,
        draggable: true,
        type: 'warning',
        lockScroll: false,
      })
        .then(() => {
          if (callback1) {
            callback1()
          }
        })
        .catch(() => {
          if (callback2) {
            callback2()
          }
        })
    }

    const $baseNotify = (
      message: string,
      title: string,
      type: 'success' | 'warning' | 'info' | 'error' = 'success',
      position: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left' = 'top-right',
      duration: number = messageDuration
    ) => {
      ElNotification({
        title,
        message,
        type,
        duration,
        position,
      })
    }

    const _emitter = mitt()

    const $pub = (...args: any[]) => {
      _emitter.emit(head(args), args[1])
    }

    const $sub = (...args: any[]) => {
      Reflect.apply(_emitter.on, _emitter, toArray(args))
    }

    const $unsub = (...args: any[]) => {
      Reflect.apply(_emitter.off, _emitter, toArray(args))
    }

    if (isCheck()) {
      app.provide('$baseAlert', $baseAlert)
      app.provide('$baseConfirm', $baseConfirm)
      app.provide('$baseLoading', $baseLoading)
      app.provide('$baseMessage', $baseMessage)
      app.provide('$baseNotify', $baseNotify)
      app.provide('$pub', $pub)
      app.provide('$sub', $sub)
      app.provide('$unsub', $unsub)

      gp = {
        $baseAlert,
        $baseConfirm,
        $baseLoading,
        $baseMessage,
        $baseNotify,
        $pub,
        $sub,
        $unsub,
      }
    }
  },
}
