<script lang="ts" setup>
import {useConfigStore} from "@/store";
import {setDayjsLocale, setWotDesignLocale} from "@/plugin";

const configStore = useConfigStore()

onLaunch(initConfig);


onShow(() => {
  console.log('%c外卖商户端', 'background: #FF6106; color: white; padding: 3px; border-radius: 2px;');
  console.log("App Show");
});

onHide(() => {
  console.log("App Hide");
});

onError((error: string) => {
  console.log('App Error', error)
})


function initApp() {
  // #ifdef APP-PLUS
  try {
    plus.screen.lockOrientation('portrait-primary')
    const color = plus.android.newObject("android.graphics.Color");
    const ac = plus.android.runtimeMainActivity();
    const c2int = plus.android.invoke(color, "parseColor", "#FFFFFF");
    console.log("c2int===" + JSON.stringify(c2int))
    const win = plus.android.invoke(ac, "getWindow");
    console.log("win===" + JSON.stringify(win))
    plus.android.invoke(win, "setNavigationBarColor", c2int);
  } catch (e) {
    console.log('error', e)
  }
  // #endif
}

// 初始化配置
function initConfig() {
  // #ifdef APP-PLUS
  plus.screen.lockOrientation('portrait-primary')
  // #endif
  const {statusBarHeight, windowHeight, windowWidth, screenWidth, screenHeight, safeAreaInsets} =
      uni.getWindowInfo()

  const {deviceId} =
      uni.getDeviceInfo()


  const {appVersion} =
      uni.getAppBaseInfo()

  const {
    uniPlatform,
    osName,
    osVersion,

  } =
      uni.getSystemInfoSync()

  configStore.deviceId = deviceId
  configStore.osName = osName
  configStore.osVersion = osVersion
  configStore.appVersion = appVersion
  configStore.uniPlatform = uniPlatform
  configStore.statusBarHeight = statusBarHeight ?? 0
  configStore.windowHeight = windowHeight ?? 0
  configStore.windowWidth = windowWidth ?? 0
  configStore.screenWidth = screenWidth ?? 0
  configStore.screenHeight = screenHeight ?? 0

  configStore.safeAreaInsets = safeAreaInsets ?? {
    bottom: 0,
    top: 0,
  }
  setWotDesignLocale()
  setDayjsLocale()
  if (osName === 'android') {
    initApp()
  }

  if (!configStore.isShowedLanguageSelectPage) {
    console.log('未展示过语言选择页面，导航到语言选择页面')
    uni.navigateTo({
      url: '/pages-login/pages/choose-language/index',
      success() {
        configStore.isShowedLanguageSelectPage = true
        console.log('导航到语言选择页面成功')
      },
    })
  }
}
</script>
<style lang="scss">
@import "@/styles/index.scss";

view {
  box-sizing: border-box;
}
</style>
