<script>
  import { mapState, mapActions } from 'pinia'
  import { useBluetoothStore } from '@/stores/bluetooth'
  import { useUserStore } from '@/stores/user'
  import { getStorage } from './utils/storage'

  let firstCheckSetting = true

  export default {
    globalData: {
      // 更新登录状态
      updateIsLogin(isLogin) {
        useUserStore().updateLoginStatus(isLogin)
      },
      // 账号信息
      appid: '',
      // 小程序版本
      envVersion: '',
      sn: '',
      // 获取环境配置
      getEnvConfig() {
        const envVersionStorage = getStorage('envVersion')
        if (envVersionStorage) {
          return envVersionStorage
        }
        // #ifdef MP-WEIXIN
        if (this.envVersion === 'develop') {
          return 'XHJ'
        }
        if (this.envVersion === 'trial') {
          return 'XHJ'
        }
        // #endif
        return 'PRE'
      }
    },
    computed: {
      ...mapState(useBluetoothStore, ['bluetoothStatus'])
    },
    async onLaunch() {
      // #ifdef MP-WEIXIN
      // 检查强制升级
      this.updateMiniProgram()
      // 设置voip配置
      this.setVoipConfig()
      // #endif

      // 监听蓝牙开关状态
      this.onBluetoothState()
      // 检查蓝牙权限
      const checkResult = await this.checkSetting()
      if (checkResult === true) {
        this.initAndListenBluetooth(false)
      }
    },
    onShow() {
      if (firstCheckSetting) {
        firstCheckSetting = false
      } else {
        this.checkSetting()
      }
    },
    methods: {
      ...mapActions(useBluetoothStore, [
        'initBluetooth',
        'onBluetoothState',
        'updateBluetoothStatus',
        'checkSetting',
        'onBluetoothConnectStatus',
        'onBluetoothCharacteristicValueChange',
        'updateInitBluetooth',
        'initAndListenBluetooth'
      ]),
      ...mapActions(useUserStore, ['updateLoginStatus']),
      // voip设置
      setVoipConfig() {
        const wmpfVoip = requirePlugin('wmpf-voip').default
        wmpfVoip.setUIConfig({
          btnText: '去开门',
          customBoxHeight: '75vh',
          listenerUI: {
            cameraRotation: 270,
            objectFit: 'fill',
            enableToggleCamera: true
          }
        })
        wmpfVoip.setVoipEndPagePath({
          url: '/pages/main/home',
          key: 'Call',
          routeType: 'switchTab'
        })

        wmpfVoip.onVoipEvent(event => {
          if (event.eventName === 'callPageOnShow') {
            const query = wmpfVoip.getPluginOnloadOptions()
            getApp().globalData.sn = query.callerId
          }
        })
      },
      // 强制升级
      updateMiniProgram() {
        const updateManager = uni.getUpdateManager()
        updateManager.onUpdateReady(function () {
          uni.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            showCancel: false,
            success() {
              updateManager.applyUpdate()
            }
          })
        })
      }
    }
  }
</script>

<style lang="scss">
  @import 'uview-plus/index';
</style>
