<script setup>
  // #ifdef APP-PLUS
  import { setSdkLanguageFromSystem } from '@/uni_modules/tuikit-atomic-x/utils/setSdkLanguageFromSystem'
  // import * as call from "@/uni_modules/RongCloud-CallWrapper/lib/index"
  // #endif
  // #ifdef H5
  import * as RongIMLib from '@rongcloud/imlib-next'
  import { installer as rtcInstaller } from '@rongcloud/plugin-rtc'
  import { installer as callPlusInstaller } from '@rongcloud/plugin-call-plus'
  // #endif
  import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
  import { reLaunch } from './utils/router'
  import { useAuthUser } from './composables/useAuthUser'
  import { useUserStore } from './stores/user'
  import { TUIChatKit } from './TUIKit'
  import { getIpBlack, getAppVersion } from './api'
  import { reasonDeal, errorDeal, imCode } from '@/utils/code.js'
  import { compareVersion } from '@/utils/util.js'
  import { navigateTo } from '@/utils/router'

  TUIChatKit.init()

  const { token, tencentUserSig } = useAuthUser()
  const { loginTencentIM } = useUserStore()
  /** 静默登录逻辑 */
  const silentLogin = async e => {
    if (token.value) {
      loginTencentIM()
      // reLaunch('/TUIKit/components/TUIConversation/index')
      return
    }

    console.log(tencentUserSig, '=====222==')
    if (e?.query?.invitationCode) {
      reLaunch('/pages/login/phone-register/phone-register', {
        invitationCode: e.query.invitationCode
      })
    } else {
      // 没有token去登录页
      reLaunch('/pages/login/login')
    }
  }

  /** 初始化 H5 音视频通话 */
  const iniCall = () => {
    // IM 初始化
    RongIMLib.init({
      appkey: '<Your-Appkey>'
    })

    // RTC 初始化
    const rtcClient = RongIMLib.installPlugin(rtcInstaller, {})

    // CallPlus 初始化
    const callPlusClient = RongIMLib.installPlugin(callPlusInstaller, {
      rtcClient
    })

    console.log('====1111111111=====', callPlusClient)
  }

  onLaunch(e => {
    // #ifdef APP-PLUS
    plus.runtime.getProperty(plus.runtime.appid, function (widgetInfo) {
      getAppVersion().then(res => {
        console.log('应用版本信息', res.data)
        const lastVersion = res.data.version
        const currentVersion = widgetInfo.version
        if (compareVersion(currentVersion, lastVersion) < 0) {
          // 当前版本低于最新版本，提示用户更新
          console.log('当前版本低于最新版本，请更新应用')
          const isForceUpdate = res.data.forceUpdate
          const url = res.data.updateUrl
          uni.showModal({
            title: '更新提示',
            content: res.data.updateMessage,
            confirmText: '立即更新',
            cancelText: '稍后再说',
            showCancel: !isForceUpdate,
            success: res => {
              if (res.confirm) {
                plus.runtime.openURL(url)
              }
            }
          })
        } else {
          console.log('当前版本为最新版本')
        }
      })
    })
    // #endif
    // return
    console.log('App Launch111')
    silentLogin(e)

    // #ifdef APP-PLUS
    setSdkLanguageFromSystem()
    // onAllListeners()
    // #endif

    // #ifdef H5
    iniCall()
    // #endif
  })

  onShow(() => {
    console.log('App Show222')
  })

  onHide(async () => {
    if (token.value) {
      const show = await getIpBlack(false)
      console.log(show.data.data, '======')
    }
    // #ifdef APP-PLUS
    removeAllListeners()
    // #endif
    console.log('App Hide333')
  })

  function removeAllListeners() {
    call.unInit()
    //移除监听-接收到通话呼入
    call.removeCallReceivedListener()
    // 移除监听-开始呼叫通话的回调
    call.removeCallOutgoingListener()
    // 移除监听-通话已接通
    call.removeCallReceivedListener()
    // 移除监听-通话已结束
    call.removeCallDisconnectedListener()
    // 移除监听-对端用户正在振铃
    call.removeRemoteUserRingingListener()
    // 移除监听-对端用户加入了通话
    call.removeRemoteUserJoinedListener()
    // 移除监听-有用户被邀请加入通话
    call.removeRemoteUserInvited()
    // 移除监听-对端用户挂断
    call.removeRemoteUserLeftListener()
    // 移除监听-对端用户切换了媒体类型
    call.removeRemoteUserMediaTypeChangedListener()
    // 移除监听-通话出现错误的回调
    call.removeErrorListener()
  }
</script>

<style lang="scss">
  /*每个页面公共css */
  @import './styles/global.scss';

  /* common css for page */
  // uni-page-body,
  // html,
  // body,
  // page {
  //   width: 100% !important;
  //   height: 100% !important;
  //   overflow: hidden;
  // }
</style>
