<template>
  <view>
    <video v-if="url" autoplay :src="url" :is-live="true" :controls="false" class="w-full" />
  </view>
</template>

<script setup>
  // #ifdef APP-PLUS
  import { onMounted, ref } from 'vue'
  import { passthrough } from '@/api/sdk'
  import { useBluetoothStore } from '@/stores/bluetooth'
  import { useBasicStore } from '@/stores/basic'
  import { startService, getLiveUrl } from '@/uni_modules/xhj-tencent-xp2p'

  const $bluetooth = useBluetoothStore()
  const $basic = useBasicStore()

  const deviceInfo = ref()

  const url = ref()

  onMounted(async () => {
    // const callback = new XP2PCallback1()
    // setCallback(callback)

    const { code, data, message } = await passthrough({
      request_method: 'GET',
      request_uri: '/api/v1/tencentYun/getDeviceDetailData',
      post_args: {
        lockId: $bluetooth.currentLockInfo.lockId
      }
    })

    if (code === 0) {
      deviceInfo.value = data
      console.log(1)
      const result = await startService({
        appKey: 'aanuJXFtISXFYVVsd',
        appSecret: 'SsnOMHJUcazCvpULSVWY',
        productId: deviceInfo.value.productId,
        deviceName: deviceInfo.value.deviceName,
        xp2pInfo: deviceInfo.value.xp2pInfo
      })
      if (result.code === 0) {
        setTimeout(async () => {
          const urlResult = await getLiveUrl({
            id: `${deviceInfo.value.productId}/${deviceInfo.value.deviceName}`
          })
          console.log(12, urlResult)
          if (urlResult.code === 0) {
            uni.request({
              url: urlResult.data.url,
              method: 'GET',
              success: res => {
                console.log(13, res)
              }
            })
            url.value = urlResult.data.url
          }
        }, 200)
      }
    } else {
      $basic.backAndToast(message)
    }
  })
  // #endif
</script>
