/*
 * 消息详情页面
 * EZOpenSDK
 *
 * Created by junecheng on 2024/3/6.
 * Copyright (c) 2024年 Ezviz. All rights reserved.
 */

import { EZAlarmInfo, EZOpenSDK } from '@ohos/EZOpenSDK'
import RouterManager from '../../global/RouterManager/RouterManager'
import EZToastUtil from '../../utils/EZToastUtil'
import { PreferenceUtil } from '../../utils/PreferenceUtil'
import EZLog from '../../utils/EZLog'

import { NavigationView } from '../../global/view/NavigationView'

@Entry
@Component
export struct EZMessagePhotoPage {
  private alarmInfo: EZAlarmInfo = new EZAlarmInfo()
  // 消息图片
  @State imageData: ResourceStr | PixelMap | null = ''

  aboutToAppear() {
    this.alarmInfo = RouterManager.getParams() as EZAlarmInfo
    // 图片加密
    if (this.isEncrypt(this.alarmInfo.alarmPicUrl)) {
      // 0-不确定 1-设备加密 2-平台加密
      let cryptType = this.alarmInfo.crypt

      let verifyCode = ''
      if (cryptType == 1) {
        verifyCode = PreferenceUtil.getString(getContext(this), this.alarmInfo?.deviceSerial) as string
        if (!verifyCode) {

          return
        }
      } else if (cryptType == 2) {
        verifyCode = this.alarmInfo.checksum
      }
      // 图片解密
      EZOpenSDK.decryptData(this.alarmInfo.alarmPicUrl, verifyCode, cryptType, (image, error) => {
        this.imageData = image
        if (error) {
          EZLog.error('MessageListCell', `${error}`)
        }
      })

    } else { // 不加密
      this.imageData = this.alarmInfo.alarmPicUrl
    }
  }

  /** 判断图片链接是否是加密 */
  isEncrypt(url: string): boolean {
    return url.includes('isEncrypted=1')
  }

  build() {
    Stack({ alignContent: Alignment.Top }) {
      Image(this.imageData).width('100%').height('100%').objectFit(ImageFit.Contain)
      NavigationView({
        title: '消息详情',
      })
      this.alarmInfoView()
    }
    .backgroundColor(Color.Black)
  }

  @Builder
  alarmInfoView() {
    Column() {
      Text('发生人体感应事件')
        .margin({ top: 25 })
        .height(20)
        .width('100%')
        .fontSize(16)
        .fontColor(Color.White)
      Text(this.alarmInfo?.alarmStart).height(20).width('100%')
        .fontSize(16)
        .fontColor($r('app.color.message_detail_info_content_color'))
      Text('来自:' + this.alarmInfo?.alarmName).height(20).width('100%')
        .fontSize(16)
        .fontColor($r('app.color.message_detail_info_content_color'))
      Button('消息录像')
        .margin({ top: 32 })
        .height(40)
        .width(280)
        .fontSize(16)
        .backgroundColor($r('app.color.ez_text_blue'))
        .onClick(() => {
          // RouterManager.pushUrl(RouterManager.playbackPage)
          EZToastUtil.showToast('代码与回放页面一样，后续再开发')
        })
    }
    // .margin({top:display.getDefaultDisplaySync().height-180})
    .margin({ top: 600 })
    .padding({ left: 10, right: 10 })
    .height(180)
    .width('100%')
    .backgroundColor($r('app.color.message_detail_info_color'))
    .align(Alignment.Bottom)
  }
}