<template>
  <ie-page :fix-height="true">
    <view class="flex-1 min-h-1 h-full flex items-center justify-center">
      <uv-image :src="imgLaunch" width="55vw" height="auto" mode="widthFix" bgColor="white" custom-class="-mt-300">
        <template #loading>
          <span></span>
        </template>
      </uv-image>
    </view>
    <maintain-popup ref="maintainPopupRef" />
  </ie-page>
</template>

<script lang="ts" setup>
import config from '@/config';
import { useEnv } from '@/hooks/useEnv';
import { useAppStore } from '@/store/appStore';
import { useTransferPage } from '@/hooks/useTransferPage';
import { load } from '@/utils/loadFont';
import MaintainPopup from './components/maintain-popup.vue';

const { platform } = useEnv();
const splashTimeout = 1200;
const appStore = useAppStore();
const { transferTo, routes } = useTransferPage();

// #ifdef H5
uni.hideTabBar();
// #endif
const imgLaunch = computed(() => {
  return config.ossUrl + '/launch.png'
});
const maintainPopupRef = ref();
const handleLoad = () => {
  if (typeof window !== 'undefined' && window !== null && window.platform === 'wap2app') {
    window.webviewBridge.getStatusBarHeight().then(height => {
      appStore.statusBarHeight = height;
    });
  }
  appStore.getMaintainNotice().then(async (isMaintaining) => {
    // 执行初始化的操作：预加载字典、提前校验token是否有效等
    if (!isMaintaining) {
      appStore.init().then(async () => {
        try {
          const usedTime = await load();
          if (usedTime < splashTimeout) {
            await new Promise(resolve => setTimeout(resolve, splashTimeout - usedTime));
          }
        } catch (error) {
          console.error('初始化失败: ', error);
        } finally {
          transferTo('/pagesMain/pages/index/index', {
            type: 'reLaunch'
          });
          // transferTo('/pagesStudy/pages/knowledge-practice/knowledge-practice', {
          //   type: 'reLaunch'
          // });
        }
      });
    } else {
      setTimeout(() => {
        nextTick(() => {
          maintainPopupRef.value?.open().then(() => {
            // #ifdef H5
            if (platform.value === 'wap2app') {
              plus.runtime.quit();
            } else {
              window.close();
            }
            // #endif
            // #ifdef MP-WEIXIN
            wx.exitMiniProgram();
            // #endif
          });
        });
      }, 800);

    }
  });
};
onLoad(() => {
  handleLoad();
});
</script>

<style></style>
