<script lang="ts" setup>
import { isDark } from '@/utils/is'
import { useAppStore } from '@/store/modules/app'
import { useDesign } from '@/hooks/web/useDesign'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import routerSearch from '@/components/RouterSearch/index.vue'
import { useAutoLogout } from './composable/useAutoLogout'

import { leftAsideStore } from '@/export'
// import demo from '/svgs/demo.svg?raw'

const electrical_modules_files = import.meta.glob('./assets/svgs/electrical/**.svg', {
  eager: true,
  as: 'raw'
})
const electrical_stroke_modules_files = import.meta.glob('./assets/svgs/electrical/stroke/**.svg', {
  eager: true,
  as: 'raw'
})
const process_demo_image_files = import.meta.glob('./assets/images/process-demo/**', {
  eager: true,
  query: '?url',
  import: 'default'
})

const materials_files = import.meta.glob('./assets/images/materials/**', {
  eager: true,
  query: '?url',
  import: 'default'
})
const danger_files = import.meta.glob('./assets/images/danger/**', {
  eager: true,
  query: '?url',
  import: 'default'
})
const icon_modules_files = {
  ...import.meta.glob('./assets/images/icons/**.svg', {
    eager: true,
    as: 'raw'
  })
}

const normalizeFillSvg = (svg: string) => {
  return svg.replace(/\sfill="(?!none\b)[^"]*"/gi, '')
}

const electrical_register_config: any = []
for (const key in electrical_modules_files) {
  //根据路径获取svg文件名
  const name = key.split('/').pop()!.split('.')[0]
  electrical_register_config.push({
    id: name,
    title: name,
    type: 'svg',
    thumbnail: 'data:image/svg+xml;utf8,' + encodeURIComponent(electrical_modules_files[key]),
    svg: electrical_modules_files[key],
    props: {
      fill: {
        type: 'color',
        val: '#FF0000',
        title: '填充色'
      }
    }
  })
}
for (const key in electrical_stroke_modules_files) {
  //根据路径获取svg文件名
  const name = key.split('/').pop()!.split('.')[0]
  electrical_register_config.push({
    id: name,
    title: name,
    type: 'svg',
    thumbnail:
      'data:image/svg+xml;utf8,' + encodeURIComponent(electrical_stroke_modules_files[key]),
    svg: electrical_stroke_modules_files[key],
    props: {
      stroke: {
        type: 'color',
        val: '#00ff00',
        title: '边框色'
      }
    }
  })
}
leftAsideStore.registerConfig('电气符号', electrical_register_config)

const icon_register_config: any = []
for (const key in icon_modules_files) {
  const name = key.split('/').pop()!.split('.')[0]
  const svg = normalizeFillSvg(icon_modules_files[key])
  icon_register_config.push({
    id: `icon-${name}`,
    title: name,
    type: 'svg',
    thumbnail: 'data:image/svg+xml;utf8,' + encodeURIComponent(svg),
    svg,
    props: {
      fill: {
        type: 'color',
        val: '#FF0000',
        title: '填充色'
      }
    }
  })
}
leftAsideStore.registerConfig('图标', icon_register_config)

const process_demo_register_config: any[] = Object.entries(process_demo_image_files).map(
  ([key, url]) => {
    const name = key.split('/').pop()!.split('.')[0]
    return {
      id: name,
      title: name,
      type: 'img',
      thumbnail: url,
      props: {}
    }
  }
)
leftAsideStore.registerConfig('工艺流程图', process_demo_register_config)

const materials_register_config: any[] = Object.entries(materials_files).map(([key, url]) => {
  const name = key.split('/').pop()!.split('.')[0]
  return {
    id: name,
    title: name,
    type: 'img',
    thumbnail: url,
    props: {}
  }
})
leftAsideStore.registerConfig('素材', materials_register_config)

const danger_register_config: any[] = Object.entries(danger_files).map(([key, url]) => {
  const name = key.split('/').pop()!.split('.')[0]
  return {
    id: `danger-${name}`,
    title: name,
    type: 'img',
    thumbnail: url,
    props: {}
  }
})
leftAsideStore.registerConfig('危险标识', danger_register_config)

const route = useRoute()
const { addListeners, removeListeners } = useAutoLogout()

const whiteList = [
  '/login',
  '/social-login',
  '/auth-redirect',
  '/bind',
  '/register',
  '/oauthLogin/gitee',
  '/dingding',
  '/deepoil',
  '/oli-connection/monitoring-board'
]

watch(
  () => route.path,
  (newPath) => {
    if (whiteList.includes(newPath)) {
      console.log('进入白名单页面，移除超时检测')
      removeListeners()
    } else {
      console.log('进入受控页面，开启超时检测')
      addListeners()
    }
  },
  { immediate: true } // 初始化时立即执行一次
)

defineOptions({ name: 'APP' })

const { getPrefixCls } = useDesign()
const prefixCls = getPrefixCls('app')
const appStore = useAppStore()
const currentSize = computed(() => appStore.getCurrentSize)
const greyMode = computed(() => appStore.getGreyMode)
const { wsCache } = useCache()

// 根据浏览器当前主题设置系统主题色
const setDefaultTheme = () => {
  let isDarkTheme = wsCache.get(CACHE_KEY.IS_DARK)
  if (isDarkTheme === null) {
    isDarkTheme = isDark()
  }
  appStore.setIsDark(isDarkTheme)
}
setDefaultTheme()
</script>
<template>
  <ConfigGlobal :size="currentSize">
    <RouterView :class="greyMode ? `${prefixCls}-grey-mode` : ''" />
    <routerSearch />
  </ConfigGlobal>
</template>
<style lang="scss">
$prefix-cls: #{$namespace}-app;

.size {
  width: 100%;
  height: 100%;
}

html,
body {
  @extend .size;

  padding: 0 !important;
  margin: 0;
  overflow: hidden;

  #app {
    @extend .size;
  }
}

.#{$prefix-cls}-grey-mode {
  filter: grayscale(100%);
}
</style>
