/*
 * 设备固件升级页面
 * EZOpenSDK
 *
 * Created by junecheng on 2024/9/9.
 * Copyright (c) 2024年 Ezviz. All rights reserved.
 */
import { EZDeviceVersion, EZError, EZOpenSDK } from '@ohos/EZOpenSDK'
import RouterManager from '../../global/RouterManager/RouterManager'
import { NavigationView } from '../../global/view/NavigationView'
import EZToastUtil from '../../utils/EZToastUtil'

@Entry
@Component
@Preview
struct EZDeviceUpgradePage {
  private readonly TAG: string = `${EZDeviceUpgradePage.name}:`
  // 设备固件版本信息
  @State deviceVersion: EZDeviceVersion | null = null
  // 设备序列号
  deviceSerial: string = ''
  // 升级按钮是否显示
  @State isUpgradeBtnShow: boolean = true
  //
  @State upgradeText: ResourceStr = $r('app.string.setting_device_updating')
  // 升级进度值
  @State progressValue: number = 0
  // 升级进度检查 定时器
  upgradeStatusIntervalID: number = 0

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

  aboutToAppear() {
    const routerParams = RouterManager.getParams() as Record<string, object>
    this.deviceVersion = routerParams.deviceVersion as EZDeviceVersion
    this.deviceSerial = `${routerParams.deviceSerial}`
  }

  aboutToDisappear() {
    clearInterval(this.upgradeStatusIntervalID)
  }

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


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

  upgradeDevice() {
    EZOpenSDK.upgradeDevice(this.deviceSerial, (error: EZError) => {
      if (error) {
        EZToastUtil.showToast(JSON.stringify(error))
        return
      }
      this.isUpgradeBtnShow = false
      this.upgradeStatusIntervalID = setInterval(() => {
        this.checkUpgradeStatus()
      }, 5000)
    })
  }

  /**
   * 升级进度查询
   */
  checkUpgradeStatus() {
    EZOpenSDK.getDeviceUpgradeStatus(this.deviceSerial, (status, error) => {
      if (status && status.upgradeStatus == 2) {
        this.upgradeText = $r('app.string.setting_device_update_complete')
        this.progressValue = 100
        clearInterval(this.upgradeStatusIntervalID)
      } else {
        this.upgradeText = $r('app.string.setting_device_updating')
        this.progressValue = status.upgradeProgress
      }
    })
  }

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

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

  build() {
    Column() {
      NavigationView({
        title: $r('app.string.device_upgrade_title')
      })
      Text(this.deviceVersion?.upgradeDesc)
        .margin({ top: 30 })
        .padding(10)
        .backgroundColor(Color.White)
      // 升级
      Button($r('app.string.setting_device_update'))
        .backgroundColor($r('app.color.ez_text_blue'))
        .width('80%')
        .margin({ top: 32 })
        .visibility(this.isUpgradeBtnShow ? Visibility.Visible : Visibility.None)
        .onClick(() => {
          this.upgradeDevice()
        })
      // 升级进度
      Column() {
        Text(this.upgradeText).padding(10).margin({ top: 32 })
        Progress({ value: this.progressValue, type: ProgressType.Linear }).width('80%')
      }
      .visibility(!this.isUpgradeBtnShow ? Visibility.Visible : Visibility.Hidden)
    }
    .height('100%')
    .backgroundColor($r('app.color.message_cell_select_color'))
  }
}