<script setup>
import Init from '@/pages/Init.vue'
import Launch from '@/pages/Launch.vue'
import Update from '@/pages/Update.vue'
import Fix from '@/pages/Fix.vue'
import { dateZhCN, zhCN } from 'naive-ui'
import Settings from '@/pages/Settings.vue'
import { useConfigStore } from '@/stores/config.js'

const configStore = useConfigStore()
const loading = ref(true)
const page = ref(null)
let conf

function show_doc() {
  window.open('https://hedgedoc.zhaozuohong.vip/s/LfSzK2n0K', '_blank')
}

async function init_version() {
  version.value = await pywebview.api.get_version()
  new_version.value = await pywebview.api.get_new_version()
  if (new_version.value.tag_name > version.value) {
    update_able.value = true
  }
}
async function initialize_config() {
  await configStore.load_config()
  conf = configStore.config
  loading.value = false
  if (!conf.is_already_show_doc) {
    show_doc()
    conf.is_already_show_doc = true
  }
  await init_version()
}

const log = ref('')
provide('log', log)
const log_ele = ref(null)
provide('log_ele', log_ele)
watch(log, () => {
  nextTick(() => {
    log_ele.value?.scrollTo({ position: 'bottom' })
  })
})

onMounted(() => {
  if (window.pywebview && pywebview.api) {
    initialize_config()
  } else {
    window.addEventListener('pywebviewready', () => {
      initialize_config()
    })
  }
  window.addEventListener('log', (e) => {
    log.value += e.detail.log
  })
})

function set_page(value) {
  log.value = ''
}

const running = ref(false)
provide('running', running)

const steps = ref([])
provide('steps', steps)

const update_able = ref(false)
provide('update_able', update_able)

const version = ref('')
provide('version', version)

const new_version = ref({})
provide('new_version', new_version)
</script>

<template>
  <n-config-provider :locale="zhCN" :dateLocale="dateZhCN">
    <n-spin v-if="loading" class="container">
      <template #description>加载中</template>
    </n-spin>
    <n-notification-provider v-else>
      <n-tabs
        type="card"
        placement="left"
        class="container"
        v-model:value="conf.page"
        @update:value="set_page"
      >
        <n-tab-pane :disabled="running" name="init" tab="初始化"><init /></n-tab-pane>
        <n-tab-pane :disabled="running" name="update" tab="更新代码"><update /></n-tab-pane>
        <n-tab-pane :disabled="running" name="launch" tab="启动程序"><launch /></n-tab-pane>
        <n-tab-pane :disabled="running" name="fix" tab="依赖修复"><fix /></n-tab-pane>
        <n-tab-pane :disabled="running" name="settings">
          <template #tab>
            <n-space :wrap="false">
              设置
              <n-tag v-if="update_able" round type="success">新</n-tag>
            </n-space>
          </template>
          <settings />
        </n-tab-pane>
        <template #suffix>
          <n-button type="primary" secondary size="small" @click="show_doc">帮助文档</n-button>
        </template>
      </n-tabs>
    </n-notification-provider>
    <n-global-style />
  </n-config-provider>
</template>

<style scoped>
.container {
  width: 100vw;
  height: 100vh;
}
</style>
