/*
 * 通道号选中页面
 * EZOpenSDK
 *
 * Created by junecheng on 2024/8/24.
 * Copyright (c) 2024年 Ezviz. All rights reserved.
 */
import EZToastUtil from '../../utils/EZToastUtil'
import { EZCameraInfo, EZDeviceInfo } from '@ohos/EZOpenSDK'
import { EZPlayType } from '../../global/EZEnumerateHeader'
import RouterManager from '../../global/RouterManager/RouterManager'
import { NavigationView } from '../../global/view/NavigationView'

@Entry
@Component
struct EZCameraTablePage {
  // 播放类型
  playType: EZPlayType = EZPlayType.EZPlayTypeLive
  // 设备对象
  deviceInfo: EZDeviceInfo | undefined = undefined
  // 设备通道数组
  @State cameraArray: Array<EZCameraInfo> = []

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

  aboutToAppear() {
    const routerParams = RouterManager.getParams() as Record<string, object>
    this.playType = parseInt(`${routerParams.playType}`) as EZPlayType
    this.deviceInfo = routerParams.deviceInfo as EZDeviceInfo

    if (this.deviceInfo.cameraInfo) {
      this.cameraArray = this.deviceInfo.cameraInfo
    } else {
      this.cameraArray = this.deviceInfo.subDeviceInfo
    }
  }

  // ------------------------ Action ------------------------

  /** 通道号选择Action */
  cameraNoChooseAction(camera: EZCameraInfo, cameraIndex: number) {
    if (!EZCameraInfo.isCamera(camera)) { // 不是视频类设备
      EZToastUtil.showToast($r('app.string.device_isnot_camera_device'))
      return
    }
    if (this.playType == EZPlayType.EZPlayTypeLive) {
      RouterManager.pushUrl(RouterManager.realPlayPage, { deviceInfo: this.deviceInfo, cameraIndex: cameraIndex })
    } else {
      RouterManager.pushUrl(RouterManager.playbackPage, { deviceInfo: this.deviceInfo, cameraIndex: cameraIndex })
    }
  }

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

  build() {
    Column() {
      NavigationView({
        title: $r('app.string.device_camera_list_title')
      })
      List({ space: 3 }) {
        ForEach(this.cameraArray, (camera: EZCameraInfo, index: number) => {
          ListItem() {
            Row() {
              Text(`${camera.cameraNo}`).textAlign(TextAlign.Start).width('10%').padding({ left: 15 })
              Text(`${camera.cameraName}`).textAlign(TextAlign.End).width('90%').padding({ right: 15 })
            }.height(50)
            .onClick((): void => {
              this.cameraNoChooseAction(camera, index)
            })
          }
        })
      }
      .listDirection(Axis.Vertical)
      .divider({ strokeWidth: 1, color: 0xEFEFEF }) // 分割线
      .scrollBar(BarState.Off) // 关闭滚动条
      .edgeEffect(EdgeEffect.Spring)
      .width('100%')
    }

  }
}