/*
 * 小权限tkToken设置
 * EZOpenSDK
 *
 * Created by junecheng on 2024/12/27.
 * Copyright (c) 2024年 Ezviz. All rights reserved.
 */
import EZLog from '../../utils/EZLog'
import { EZPlayType } from '../../global/EZEnumerateHeader'
import GlobalKit from '../../global/GlobalKit'
import { NavigationView } from '../../global/view/NavigationView'
import PreviewList, { ListItemData } from '../../global/view/PreviewList'
import TKTokenDialog from './view/TKTokenDialog'
import TKTokenDialog2 from './view/TKTokenDialog2'
import TKTokenDialog4 from './view/TKTokenDialog4'
import { EZOpenSDK } from '@ohos/EZOpenSDK'

@Entry
@Component
@Preview
struct EZTKTokenTestPage {
  private readonly TAG: string = `${EZTKTokenTestPage.name}:`
  // 数据源
  @State private listData: ListItemData[] = []

  private playType: EZPlayType = EZPlayType.EZPlayTypeLive
  private playTypeName: string = '预览'

  // ------------------------ 生命周期 ------------------------

  aboutToAppear() {
    this.listData = [
      {
        mainTitle: '设备级tkToken设置',
        onItemClick: (): void => {
          this.tkTokenDialogController2.open()
        }
      },
      {
        mainTitle: '设备通道级tkToken设置',
        onItemClick: (): void => {
          this.tkTokenDialogController4.open()
        }
      },
      {
        mainTitle: '预览tkToken设置',
        onItemClick: (): void => {
          this.playType = EZPlayType.EZPlayTypeLive
          this.playTypeName = '预览'
          this.tkTokenDialogController.open()
        }
      },
      {
        mainTitle: '对讲tkToken设置',
        onItemClick: (): void => {
          this.playType = EZPlayType.EZPlayTypeTalk
          this.playTypeName = '对讲'
          this.tkTokenDialogController.open()
        }
      },
      {
        mainTitle: '回放tkToken设置',
        onItemClick: (): void => {
          this.playType = EZPlayType.EZPlayTypePlayback
          this.playTypeName = '回放'
          this.tkTokenDialogController.open()
        }
      }
    ]
  }

  aboutToDisappear() {

  }

  // ------------------------ Actions ------------------------

  // ------------------------ 网络请求 ------------------------

  // ------------------------ 自定义方法 ------------------------

  // ------------------------ UI ------------------------

  build() {
    Column() {
      NavigationView({
        title: '小权限tkToken设置'
      })
      PreviewList({
        items: this.listData
      })
    }.width('100%').height('100%')
  }


  // ------------------------ 取流tkToken输入窗口-一个输入框 ------------------------

  @State tkTokenDialogInputContent: string = ''
  tkTokenDialogController: CustomDialogController = new CustomDialogController({
    customStyle: true,
    builder: TKTokenDialog({
      title: `${this.playTypeName}tkToken设置`,
      message: '',
      placeholder: '请输入tkToken',
      inputContent: this.tkTokenDialogInputContent,
      confirmClick: async (streamToken) => {
        EZLog.error(this.TAG, `${streamToken}`)
        if (this.playType == EZPlayType.EZPlayTypeLive) {
          GlobalKit.getInstance().previewStreamToken = streamToken
        } else if (this.playType == EZPlayType.EZPlayTypeTalk) {
          GlobalKit.getInstance().talkStreamToken = streamToken
        } else if (this.playType == EZPlayType.EZPlayTypePlayback) {
          GlobalKit.getInstance().playbackStreamToken = streamToken
        }
      },
      cancelClick: () => {

      }
    }),
    alignment: DialogAlignment.Center,
    autoCancel: false
  })

  // ------------------------ 设备级tkToken输入窗口-两个输入框 ------------------------

  @State tkTokenDialog2InputContent: string = ''
  @State tkTokenDialog2InputContent2: string = ''
  tkTokenDialogController2: CustomDialogController = new CustomDialogController({
    customStyle: true,
    builder: TKTokenDialog2({
      title: '设备级tkToken设置',
      message: '注：channelNo传*，resourceCategory传global',
      placeholder: '请输入设备序列号',
      placeholder2: '请输入tkToken',
      inputContent: this.tkTokenDialog2InputContent,
      inputContent2: this.tkTokenDialog2InputContent2,
      confirmClick: async (deviceSerial, deviceToken) => {
        EZOpenSDK.setDeviceToken(deviceSerial, deviceToken)
      },
      cancelClick: () => {

      }
    }),
    alignment: DialogAlignment.Center,
    autoCancel: false
  })

  // ------------------------ 设备通道级tkToken输入窗口-四个输入框 ------------------------

  @State tkTokenDialog4InputContent: string = ''
  @State tkTokenDialog4InputContent2: string = '1'
  @State tkTokenDialog4InputContent3: string = ''
  @State tkTokenDialog4InputContent4: string = ''
  tkTokenDialogController4: CustomDialogController = new CustomDialogController({
    customStyle: true,
    builder: TKTokenDialog4({
      title: '设备通道级tkToken设置',
      message: '',
      placeholder: '请输入设备序列号',
      placeholder2: '请输入设备通道',
      placeholder3: '请输入global tkToken',
      placeholder4: '请输入video tkToken',
      inputContent: this.tkTokenDialog4InputContent,
      inputContent2: this.tkTokenDialog4InputContent2,
      inputContent3: this.tkTokenDialog4InputContent3,
      inputContent4: this.tkTokenDialog4InputContent4,
      confirmClick: async (deviceSerial, cameraNo, deviceGlobalToken, deviceVideoToken) => {
        EZOpenSDK.setDeviceCamerNoToken(deviceSerial, cameraNo, deviceGlobalToken, deviceVideoToken)
      },
      cancelClick: () => {

      }
    }),
    alignment: DialogAlignment.Center,
    autoCancel: false
  })

}