/*
 * http接口测试
 * EZOpenSDK
 *
 * Created by junecheng on 2024/8/9.
 * Copyright (c) 2024年 Ezviz. All rights reserved.
 */
import { EZOpenSDK, EZPTZAction, EZPTZCommand } from '@ohos/EZOpenSDK'
import EZToastUtil from '../../utils/EZToastUtil'
import GlobalKit from '../../global/GlobalKit'
import DemoConfig from '../../global/DemoConfig'
import RouterManager from '../../global/RouterManager/RouterManager'

import { NavigationView } from '../../global/view/NavigationView'
import PreviewList, { ListItemData } from '../../global/view/PreviewList'
import ProgressView from '../../global/view/ProgressView'
import CaptureCameraPicDialog from './view/CaptureCameraPicDialog'

@Entry
@Component
struct EZHttpInterfaceTestPage {
  private readonly TAG: string = `${EZHttpInterfaceTestPage.name}:`
  // 数据源
  @State private listData: ListItemData[] = []
  // 指定的设备序列号
  private mSingleDeviceSerial = ""
  // 抓图url
  private captureCameraImagUrl = ""
  // 加载进程
  @State private rotateValue: number = 0
  @State private isShowLoading: boolean = false

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

  aboutToAppear() {

    // 初始化数据源
    this.listData = [
      {
        mainTitle: '获取设备列表(支持网关子设备)',
        subTitle: 'EZOpenSDK.getDeviceListEx',
        onItemClick: this.getDeviceListEx
      },
      {
        mainTitle: '获取设备信息(支持网关子设备)',
        subTitle: 'EZOpenSDK.getDeviceInfoEx',
        onItemClick: this.getDeviceInfoEx
      },
      {
        mainTitle: '抓图',
        subTitle: 'EZOpenSDK.captureCamera',
        onItemClick: this.captureCamera
      },
      {
        mainTitle: '获取用户信息',
        subTitle: 'EZOpenSDK.getUserInfo',
        onItemClick: this.getUserInfo
      },
      {
        mainTitle: 'SD卡格式化',
        subTitle: 'EZOpenSDK.formatStorage',
        onItemClick: this.formatStorage
      },
      {
        mainTitle: '云台结束转动Mix',
        subTitle: 'EZOpenSDK.controlPTZMix',
        onItemClick: this.ptzStopMix
      },
      {
        mainTitle: '刷新设备详情数据',
        subTitle: 'EZOpenSDK.refreshDeviceInfo',
        onItemClick: this.refreshDeviceInfo
      },
      {
        mainTitle: '绑定在NVR下的IPC设备录像直查',
        subTitle: 'EZOpenSDK.searchRecordFileFromDevice',
        onItemClick: this.searchRecordFileFromDevice
      }
    ]
  }

  aboutToDisappear() {

  }

  // ------------------------ Actions ------------------------
  /**
   * 获取设备列表(支持网关子设备)
   */
  getDeviceListEx = () => {
    this.showLoading()
    EZOpenSDK.getDeviceListEx(0, 10, (deviceList, totalCount, error) => {
      this.requestSuccess()
    })
  }
  /**
   * 获取设备列表(支持网关子设备)
   */
  getDeviceInfoEx = () => {
    if (!this.isExistDeviceSerial()) {
      return
    }
    this.showLoading()
    EZOpenSDK.getDeviceInfoEx(this.mSingleDeviceSerial, (deviceInfo, error) => {
      this.requestSuccess()
    })
  }
  /**
   * 抓图
   */
  captureCamera = () => {
    if (!this.isExistDeviceSerial()) {
      return
    }
    this.showLoading()
    EZOpenSDK.captureCamera(this.mSingleDeviceSerial, 1, (url, error) => {
      this.requestSuccess()
      this.captureCameraImagUrl = url
      this.captureCameraPicDialogController.open()
    })
  }
  /**
   * 获取用户信息
   */
  getUserInfo = () => {
    this.showLoading()
    EZOpenSDK.getUserInfo((userInfo, error) => {
      this.requestSuccess()
    })
  }
  /**
   * SD卡格式化
   */
  formatStorage = () => {
    if (!this.isExistDeviceSerial()) {
      return
    }
    this.showLoading()
    EZOpenSDK.getStorageStatus(this.mSingleDeviceSerial, (storageStatusList, error) => {
      if (storageStatusList && storageStatusList.length > 0) {
        EZOpenSDK.formatStorage(this.mSingleDeviceSerial, storageStatusList[0].index, (error) => {
          this.requestSuccess()
        })
      }
    })
  }
  /**
   * 云台结束转动Mix
   */
  ptzStopMix = () => {
    if (!this.isExistDeviceSerial()) {
      return
    }
    this.showLoading()
    EZOpenSDK.controlPTZMix(this.mSingleDeviceSerial, 1, EZPTZCommand.EZPTZCommandUp, EZPTZAction.EZPTZActionStop, 2,
      (error) => {
        this.requestSuccess()
      })
  }
  /**
   * 刷新设备详情数据
   */
  refreshDeviceInfo = () => {
    if (!this.isExistDeviceSerial()) {
      return
    }
    this.showLoading()
    EZOpenSDK.refreshDeviceDetailInfo(this.mSingleDeviceSerial, 1, (error) => {
      this.requestSuccess()
    })
  }
  /**
   * 绑定在NVR下的IPC设备录像直查
   */
  searchRecordFileFromDevice = () => {
    if (!this.isExistDeviceSerial()) {
      return
    }
    RouterManager.pushUrl(RouterManager.deviceRecordDirectQueryPage)
  }
  /**
   * 抓图图片弹窗
   */
  private captureCameraPicDialogController: CustomDialogController = new CustomDialogController({
    customStyle: true,
    alignment: DialogAlignment.Center,
    builder: CaptureCameraPicDialog({
      imagUrl: this.captureCameraImagUrl
    }),
    autoCancel: true,
    offset: { dx: 0, dy: 0 }
  })

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

  isExistDeviceSerial(): boolean {
    this.mSingleDeviceSerial = GlobalKit.getInstance().getSavedData(DemoConfig.UserDevSerial) as string
    if (!this.mSingleDeviceSerial) {
      EZToastUtil.showToast('please specify the device serial number on the login page')
      return false
    }
    return true
  }

  requestSuccess() {
    this.hideLoading()
    EZToastUtil.showToast('request success, please check the demo log')
  }

  showLoading() {
    this.rotateValue = 360
    this.isShowLoading = true
  }

  hideLoading() {
    this.isShowLoading = false
  }

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

  build() {
    Stack() {
      Column() {
        NavigationView({
          title: '接口测试'
        })
        PreviewList({
          items: this.listData
        })
      }.width('100%').height('100%')

      ProgressView({
        rotateValue: this.rotateValue,
        isShowLoading: this.isShowLoading
      })
    }
  }
}