/*
 * 业务工具类
 * EZOpenSDK
 *
 * Created by junecheng on 2024/5/17.
 * Copyright (c) 2024年 Ezviz. All rights reserved.
 */
import { image } from '@kit.ImageKit'
import { EZCameraInfo, EZDeviceInfo, EZSubDeviceInfo, EZVideoLevelType, EzvizSupportKit } from '@ohos/EZOpenSDK'

export default class EZBusinessTool {
  // ------------------------ 预览 ------------------------

  static getDeviceQualityTitle(videoLevelType: EZVideoLevelType): ResourceStr {
    let qualityTitle = $r('app.string.device_quality_low')
    switch (videoLevelType) {
      case EZVideoLevelType.EZVideoLevelMiddle:
        qualityTitle = $r('app.string.device_quality_median')
        break
      case EZVideoLevelType.EZVideoLevelHigh:
        qualityTitle = $r('app.string.device_quality_high')
        break
      case EZVideoLevelType.EZVideoLevelSuperHigh:
        qualityTitle = $r('app.string.device_quality_super')
        break
      case EZVideoLevelType.EZVideoLevelExtremeHigh:
        qualityTitle = $r('app.string.device_quality_extreme')
        break
      case EZVideoLevelType.EZVideoLevel3K:
        qualityTitle = $r('app.string.device_quality_3k')
        break
      case EZVideoLevelType.EZVideoLevel4K:
        qualityTitle = $r('app.string.device_quality_4k')
        break
      default:
        break
    }
    return qualityTitle
  }

  /**
   * 根据type获取取流方式名
   *
   * @param streamType
   */
  static getStreamType(streamType: number): string {
    let strStreamType: string
    switch (streamType) {
    // 取流方式切换到私有流媒体转发模式
      case 0:
        strStreamType = "private_stream"
        break
    // 取流方式切换到P2P模式
      case 1:
        strStreamType = "p2p"
        break
    // 取流方式切换到内网直连模式
      case 2:
        strStreamType = "direct_inner"
        break
    // 取流方式切换到外网直连模式
      case 3:
        strStreamType = "direct_outer"
        break
    // 取流方式切换到云存储回放
      case 4:
        strStreamType = "cloud_playback"
        break
    // 取流方式切换到云存储留言
      case 5:
        strStreamType = "cloud_leave_msg"
        break
    // 取流方式切换到反向直连模式
      case 6:
        strStreamType = "direct_reverse"
        break
    // 取流方式切换到HCNETSDK
      case 7:
        strStreamType = "hcnetsdk"
        break
      default:
        strStreamType = `unknown(${streamType})`
        break
    }
    return `stream type: ${strStreamType}`
  }

  /**
   * Uint8Array 转 PixelMap
   * @param data
   * @returns
   */
  static async convertToPixelMap(data: Uint8Array): Promise<image.PixelMap | null> {
    const arrayBuffer = data.buffer
    let imageSource: image.ImageSource = image.createImageSource(arrayBuffer)
    try {
      let pixelMap = await imageSource.createPixelMap()
      console.debug(`EZRecordCoverFetcherManager: succeed to create PixelMap`)
      return pixelMap
    } catch (error) {
      console.error('EZRecordCoverFetcherManager: Failed to create PixelMap:', error)
      return null
    }
  }

  // ------------------------ 能力集判断 ------------------------

  /** 是否支持对讲 */
  static isSupportTalk(deviceInfo: EZDeviceInfo | null, cameraInfo: EZCameraInfo | null) {
    if (cameraInfo && cameraInfo instanceof EZSubDeviceInfo) {
      return EzvizSupportKit.isSupportTalk(cameraInfo.abilities)
    }
    if (!deviceInfo) {
      return false
    }
    return EzvizSupportKit.isSupportTalk(deviceInfo.abilities)
  }

  /** 是否支持云台 */
  static isSupportPTZ(deviceInfo: EZDeviceInfo | null, cameraInfo: EZCameraInfo | null) {
    if (!deviceInfo) {
      return false
    }
    return EzvizSupportKit.isSupportPTZ(deviceInfo.abilities)
  }

  /** 是否支持云台焦距模式 */
  static isSupportPTZFocus(deviceInfo: EZDeviceInfo | null, cameraInfo: EZCameraInfo | null) {
    if (!deviceInfo) {
      return false
    }
    return EzvizSupportKit.isSupportPTZFocus(deviceInfo.abilities)
  }

  /** 是否支持内网直连时倍数设置 */
  static isSupportDirectInnerRelaySpeed(deviceInfo: EZDeviceInfo | null, cameraInfo: EZCameraInfo | null) {
    if (!deviceInfo) {
      return false
    }
    return EzvizSupportKit.isSupportDirectInnerRelaySpeed(deviceInfo.abilities)
  }

  /** 是否支持回放倍率设置 */
  static isSupportPlaybackRate(deviceInfo: EZDeviceInfo | null, cameraInfo: EZCameraInfo | null) {
    if (!deviceInfo) {
      return false
    }
    return EzvizSupportKit.isSupportPlaybackRate(deviceInfo.abilities)
  }

  /** 是否支持SD卡录像封面 */
  static isSupportSdCover(deviceInfo: EZDeviceInfo | null, cameraInfo: EZCameraInfo | null) {
    if (!deviceInfo) {
      return false
    }
    return EzvizSupportKit.isSupportSdCover(deviceInfo.abilities)
  }

  /** 是否支持双目 */
  static isSupportMultiChannel(deviceInfo: EZDeviceInfo | null, cameraInfo: EZCameraInfo | null) {
    if (!deviceInfo) {
      return false
    }
    return EzvizSupportKit.isSupportMultiChannel(deviceInfo.abilities)
  }

  /** 是否支持SD卡录像下载 */
  static isSupportSDRecordDownload(deviceInfo: EZDeviceInfo | null, cameraInfo: EZCameraInfo | null) {
    if (!deviceInfo) {
      return false
    }
    return EzvizSupportKit.isSupportSDRecordDownload(deviceInfo.abilities)
  }

  /**
   * 获取设备热点前缀
   * @param deviceHotSpot 0-EZVIZ，1-SoftAP，2-CAMGO
   */
  static getWiFiConfigPrefix(deviceHotSpot: number) {
    let WiFiConfigPrefix = 'EZVIZ'
    switch (deviceHotSpot) {
      case 1:
        WiFiConfigPrefix = 'SoftAP'
        break;
      case 2:
        WiFiConfigPrefix = 'CAMGO'
        break;
      default:
        break;
    }
    return WiFiConfigPrefix
  }
}