import useRouterStore, { type RouteStr } from '@/useRouterStore'
import { computed, defineComponent, ref, Transition, watch } from 'vue'
import { OhVueIcon, addIcons } from 'oh-vue-icons'
import { MdClose, MdOpeninfull, MdClosefullscreen } from 'oh-vue-icons/icons'
import asyncLoader from './utils/asyncLoader'
import widgetList from '@/widgets'
addIcons(MdClose, MdOpeninfull, MdClosefullscreen)
const SearchPage = asyncLoader(() => import('@/layout/header/search/SearchPage'))
const AdderPage = asyncLoader(() => import('@/layout/adder/AdderPage'))
const BackgroundSwtich = asyncLoader(() => import('@/layout/background/BackgroundSwtich'))

const noFullList: RouteStr[] = ['global-search', 'global-adder', 'global-background']

export default defineComponent(() => {
  const router = useRouterStore()
  const show = computed(
    () =>
      router.path.startsWith('widget-') ||
      router.path === 'global-search' ||
      router.path === 'global-adder' ||
      router.path === 'global-background'
  )
  const full = ref(false)
  watch(router, () => {
    full.value = false
  })
  return () => (
    <div class="fixed left-0 top-0 z-50 w-full">
      {/* 背景遮罩 */}
      <Transition>
        {show.value && (
          <div
            class="w-full h-screen bg-black/20 backdrop-blur"
            onClick={() => {
              router.back()
            }}
          ></div>
        )}
      </Transition>
      {/* 弹框主体 */}
      <Transition name="modal">
        {show.value && (
          <div
            class="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 overflow-hidden transition-all"
            style={{
              width: full.value ? '100%' : '900px',
              height: full.value ? '100vh' : '540px',
              borderRadius: full.value ? '0' : '1rem'
            }}
          >
            {/* 关闭按钮 */}
            <div
              class={
                'w-5 h-5 flex justify-center items-center rounded-full overflow-hidden absolute bg-red-500/70 hover:bg-red-500 transition-all cursor-pointer z-30 ' +
                (router.path === 'global-adder' ? 'top-6 right-4' : 'top-2 right-2')
              }
              onClick={() => {
                router.back()
              }}
            >
              <OhVueIcon name="md-close" scale={0.7} fill="white" />
            </div>
            {/* 全屏按钮 */}
            {noFullList.includes(router.path) ? null : (
              <div
                class="w-5 h-5 flex justify-center items-center rounded-full overflow-hidden absolute top-2 right-10 bg-green-500/70 hover:bg-green-500 transition-all cursor-pointer z-30"
                onClick={() => {
                  full.value = !full.value
                }}
              >
                <OhVueIcon
                  name={full.value ? 'md-closefullscreen' : 'md-openinfull'}
                  scale={0.6}
                  fill="white"
                />
              </div>
            )}

            <div class="w-full h-full flex justify-center items-center">
              <Transition>
                {router.path === 'global-search' ? (
                  <SearchPage />
                ) : router.path === 'global-adder' ? (
                  <AdderPage />
                ) : router.path === 'global-background' ? (
                  <BackgroundSwtich/>
                ) :
                  router.path.startsWith('widget-') ? (
                    (() => {
                      const name = router.path.split('-')[1]
                      const selected = widgetList.find((el) => el.name === name)
                      if (!selected)
                        return (
                          <div class="w-full h-full flex justify-center items-center text-black/80">
                            组件维护中
                          </div>
                        )
                      const compo = selected.modal
                      console.log(compo)
                      return <compo />
                    })()
                  ) : null}
              </Transition>
            </div>
          </div>
        )}
      </Transition>
    </div>
  )
})
