import { EZAlarmInfo, EZOpenSDK, EZTimeUtil } from '@ohos/EZOpenSDK'
import { PreferenceUtil } from '../../../utils/PreferenceUtil'
import EZLog from '../../../utils/EZLog'

@Component
export struct MessageListCell {
  // 编辑状态
  @Prop isEditMode: boolean
  // 选中状态
  @Link selectArr: Array<Array<boolean>>
  // 消息对象
  alarmInfo?: EZAlarmInfo
  index: number = 0
  inIndex: number = 0
  onCellClick?: () => void
  onVerifyCodeError?: () => void
  // 消息图片
  @State imageData: ResourceStr | PixelMap | null = ''

  aboutToAppear() {
    if (!this.alarmInfo) {
      return
    }
    // 图片加密
    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) {
          if (this.onVerifyCodeError) {
            this.onVerifyCodeError()
          }
          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 decryptData error:', `${error}`)
        }
      })

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

  build() {
    Column() {
      Row() {
        this.editModeImage()
        Flex() {
          Image(this.imageData).objectFit(ImageFit.Contain).height(62).width(110).margin(4)
          Circle({ width: 16, height: 16 })
            .fill(this.alarmInfo?.isChecked ? $r('app.color.ez_clear_color') : $r('app.color.ez_text_blue'))
            .alignSelf(ItemAlign.Center)
          Column({ space: 16 }) {
            Row() {
              Text('人体感应事件').fontColor(Color.Black).fontSize(16)
              Text(EZTimeUtil.dateFormat(this.alarmInfo?.alarmStart, 'HH:mm:ss'))
                .fontColor(Color.Gray)
                .fontSize(14)
            }
            .width('100%')
            .justifyContent(FlexAlign.SpaceBetween)
            .alignItems(VerticalAlign.Center)

            Text('来自：' + this.alarmInfo?.alarmName).fontColor(Color.Gray).fontSize(14)
          }
          .height(64)
          .margin({ left: 8, right: 8 })
          .alignSelf(ItemAlign.Center)
          .justifyContent(FlexAlign.Center)
          .alignItems(HorizontalAlign.Start)
        }
      }
      .width('100%')
      .backgroundColor(this.getBackgroundColor())
      .justifyContent(FlexAlign.Start)
      .onClick(() => {
        if (this.onCellClick !== undefined) {
          this.onCellClick()
        }
      })

      Divider()
    }
  }

  getBackgroundColor(): ResourceColor {
    let bgColor: ResourceColor = Color.White
    if (this.selectArr && this.selectArr.length > 0 && this.selectArr[this.index].length > 0) {
      bgColor = this.selectArr[this.index][this.inIndex] ? $r('app.color.message_cell_select_color') : Color.White
    }
    return bgColor
  }

  @Builder
  editModeImage() {
    if (this.isEditMode) {
      Flex() {
        Image(this.selectArr && this.selectArr[this.index][this.inIndex] ? $r('app.media.icon_select_circle') :
        $r('app.media.icon_default_circle'))
          .height(24)
          .width(24)
          .margin({ left: 8 })
      }.flexBasis(32)
    }
  }

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