import router, {
  reloadRouter,
  removeRoutes,
} from '@/common/router/index.ts'
import Evt from '@/common/utils/evt.ts'
import { useAppSettingStore } from '@/common/app/app-setting-store.ts'
import { useAppPageStore } from '@/common/app/app-page-store.ts'
import { reloadUserInfo } from '@/common/app'
import { ElMessage } from 'element-plus'
import type { App } from 'vue'
import { SpecialPage } from '@/common/router/constants.ts'

type Option = Partial<Pick<AppTypes.PageContext, 'params' | 'insId' | 'title'>> & Pick<AppTypes.PageContext, 'routeName'>

/**
 * 打开页面并添加标签
 *
 * @param option
 */

/* function open(option: string | Option) {
 let ctx: AppTypes.PageContext
 const appSettingStore = useAppSettingStore()
 if (typeof option === 'string') {
 const routeName = option
 if (!router.hasRoute(routeName)) {
 return reloadUserInfo()
 .then(reloadRouter)
 .then(() => {
 if (!router.hasRoute(routeName)) {
 ElMessage.error('页面不存在')
 return Promise.reject('页面不存在')
 }
 const menu = appSettingStore.menus.find(it => it.routeName === routeName)
 if (menu == null) {
 ElMessage.error('页面不存在')
 return Promise.reject('页面不存在')
 }
 ctx = {
 insId: routeName,
 title: menu.title,
 keepAlive: true,
 params: {},
 routeName,
 menuId: menu.id,
 icon: menu.icon,
 breadcrumb: menu.breadcrumb,
 }
 useAppPageStore().open(ctx)
 return router.push({name: ctx.routeName, params: ctx.params})
 .then(err => {
 if (err == null) {
 Evt.emit('openPage')
 return Promise.resolve(true)
 } else {
 return Promise.reject(err)
 }
 })
 .catch(err => {
 return Promise.reject(err)
 })
 })
 }
 const menu = appSettingStore.menus.find(it => it.routeName === routeName)
 if (menu == null) {
 ElMessage.error('页面不存在')
 return Promise.reject('页面不存在')
 }
 ctx = {
 insId: routeName ,
 title: menu.title,
 keepAlive: true,
 params: {},
 routeName,
 menuId: menu.id,
 icon: menu.icon,
 breadcrumb: menu.breadcrumb,
 }
 } else {
 const routeName = option.routeName
 if (!router.hasRoute(routeName)) {
 return reloadUserInfo()
 .then(reloadRouter)
 .then(() => {
 if (!router.hasRoute(routeName)) {
 ElMessage.error('页面不存在')
 return Promise.reject('页面不存在')
 }
 const menu = appSettingStore.menus.find(it => it.routeName === routeName)
 if (menu == null) {
 ElMessage.error('页面不存在')
 return Promise.reject('页面不存在')
 }
 const option_ = option as Option
 ctx = {
 insId: option_.insId ?? routeName,
 title: option_.title ?? menu.title,
 routeName: routeName,
 keepAlive: true,
 params: option_.params ?? {},
 menuId: menu.id,
 icon: menu.icon,
 breadcrumb: menu.breadcrumb,
 }
 useAppPageStore().open(ctx)
 return router.push({name: ctx.routeName, params: ctx.params})
 .then(err => {
 if (err == null) {
 Evt.emit('openPage')
 return Promise.resolve(true)
 } else {
 return Promise.reject(err)
 }
 })
 .catch(err => {
 return Promise.reject(err)
 })
 })
 }
 const menu = appSettingStore.menus.find(it => it.routeName === routeName)
 if (menu == null) {
 ElMessage.error('页面不存在')
 return Promise.reject('页面不存在')
 }

 ctx = {
 insId: option.insId ?? routeName,
 title: option.title ?? menu.title,
 routeName: routeName,
 keepAlive: true,
 params: option.params ?? {},
 menuId: menu.id,
 icon: menu.icon,
 breadcrumb: menu.breadcrumb,
 }
 }
 useAppPageStore().open(ctx)
 return router.push({name: ctx.routeName, params: ctx.params})
 .then(err => {
 if (err == null) {
 Evt.emit('openPage')
 return Promise.resolve(true)
 } else {
 return Promise.reject(err)
 }
 })
 .catch(err => {
 return Promise.reject(err)
 })
 } */

function checkRoute(option: string | Option) {
  let opt: Option
  if (typeof option === 'string') {
    opt = {
      routeName: option,
    }
  } else {
    opt = option
  }
  const appSettingStore = useAppSettingStore()
  const menu = appSettingStore.menus.find(it => it.routeName === opt.routeName)

  if (menu == null
    || !router.hasRoute(opt.routeName)) {
    return reloadUserInfo()
      .then(reloadRouter)
      .then(() => {
        const menu_ = appSettingStore.menus.find(it => it.routeName === opt.routeName)
        if (menu_ == null || !router.hasRoute(opt.routeName)) {
          ElMessage.error('页面不存在')
          return Promise.reject('页面不存在')
        } else {
          return {option: opt, menu: menu_}
        }
      })
  }
  return Promise.resolve({option: opt, menu})
}

function jump(ctx: AppTypes.PageContext) {
  return router.push({name: ctx.routeName, params: ctx.params})
    .then(err => {
      if (err == null) {
        return Promise.resolve(ctx)
      }
      return Promise.reject(err)
    })
}

function open(option: string | Option) {
  return checkRoute(option)
    .then(({option, menu}) => {
      return jump({
        insId: option.insId ?? option.routeName,
        title: option.title ?? menu.title,
        keepAlive: true,
        params: option.params ?? {},
        routeName: option.routeName,
        menuId: menu.id,
        icon: menu.icon,
        breadcrumb: menu.breadcrumb,
      })
    })
    .then(useAppPageStore().open)
}

/**
 * 关闭页面并删除标签
 *
 * @param id
 */
function close(id: string) {
  useAppPageStore()
    .closePage(id)
    .then(ctx => {
      if (ctx == null) return Promise.resolve(null)
      return jump(ctx)
    })
}

function closeCurrent() {
  return useAppPageStore().closeCurrent()
    .then(ctx => {
      if (ctx == null) return Promise.resolve(null)
      return jump(ctx)
    })
}

function closeLeft() {
  useAppPageStore().closeLeft()
}

function closeRight() {
  useAppPageStore().closeRight()
}

function closeOther() {
  useAppPageStore().closeOther()
}

function closeAll() {
  return useAppPageStore().closeAll()
    .then(jump)
}

const install = (_: App): void => {
  Evt.on('login', (_) => {
    open(SpecialPage.Home)
  })

  Evt.on('browserReflash', (routeName: string) => {
    if (routeName === SpecialPage.Home) {
      open(routeName)
      return
    }
    checkRoute(SpecialPage.Home)
      .then(({option, menu}) => {
        useAppPageStore().open({
          insId: option.insId ?? option.routeName,
          title: option.title ?? menu.title,
          keepAlive: true,
          params: option.params ?? {},
          routeName: option.routeName,
          menuId: menu.id,
          icon: menu.icon,
          breadcrumb: menu.breadcrumb,
        })
        open(routeName)
      })
  })

  Evt.on('logout', (_) => {
    const routes = router
      .getRoutes()
      .filter((it) => it.name !== SpecialPage.Main && it.name !== SpecialPage.Login && it.name !== SpecialPage.NotFound && it.name !== SpecialPage.Home)
      .map((it) => it.name as string)
    removeRoutes(routes)
    router.push({replace: true, name: SpecialPage.Login}).then(() => {
      // console.log(r)
    })
  })

}
export default {
  open,
  close,
  closeCurrent,
  closeLeft,
  closeRight,
  closeOther,
  closeAll,
  install,
}


