/*
 * 全局账号信息管理类
 * EZOpenSDK
 *
 * Created by junecheng on 2024/5/13.
 * Copyright (c) 2024年 Ezviz. All rights reserved.
 */
import { ValueType } from '@ohos.data.ValuesBucket'
import { PreferenceUtil } from 'ets/utils/PreferenceUtil'
import DemoConfig from './DemoConfig'

export default class GlobalKit {
  static isJCTest: boolean = false
  /// 设备序列号
  deviceSerialNo: string = ''
  /// 设备类型字符串
  deviceModel: string = ''
  /// 设备验证码
  deviceVerifyCode: string = ''
  /// 内存存储：设备验证码（Value）序列号（Key）字典
  // deviceVerifyCodeBySerial: Object
  /// 配网的前缀：萤石是EZVIZ，定制设备根据能力集获取
  WiFiConfigPrefix: string

  /// 小权限预览取流token
  previewStreamToken: string = ''
  /// 小权限对讲取流token
  talkStreamToken: string = ''
  /// 小权限回放取流token
  playbackStreamToken: string = ''

  // 应用层本地记录各个取流方式的开关状态
  // 是否允许内网直连取流
  enableDirectInner: boolean = true
  // 是否允许P2P取流
  enableP2P: boolean = false
  // 是否允许流媒体取流
  enableVTDU: boolean = true

  private static instance: GlobalKit
  /// 屏幕宽度
  screenWidth: number | undefined = undefined

  constructor() {
    // this.deviceVerifyCodeBySerial = JSON.parse(this.getSavedData(DemoConfig.PATH_DeviceVerifyCodeBySerial) as string)
    // if (!this.deviceVerifyCodeBySerial) {
    //   this.deviceVerifyCodeBySerial = new Object()
    // }
    this.WiFiConfigPrefix = 'EZVIZ'
  }

  static getInstance(): GlobalKit {
    if (!GlobalKit.instance) {
      GlobalKit.instance = new GlobalKit()
    }
    return GlobalKit.instance
  }

  clearDeviceInfo() {
    this.deviceVerifyCode = ''
    this.deviceSerialNo = ''
    this.deviceModel = ''
  }

  // saveDeviceVerifyCodes2File() {
  //   this.saveData(DemoConfig.PATH_DeviceVerifyCodeBySerial, JSON.stringify(this.deviceVerifyCodeBySerial))
  // }

  /*
   * 保存上次sdk初始化的参数
   */
  saveLastSdkInitParams(appKey: string, accessToken: string, apiUrl: string, authUrl: string, specifiedDevice: string) {
    let context = getContext()
    PreferenceUtil.saveString(context, DemoConfig.UserAppKey, appKey)
    PreferenceUtil.saveString(context, DemoConfig.UserAccessToken, accessToken)
    PreferenceUtil.saveString(context, DemoConfig.UserApiUrl, apiUrl)
    PreferenceUtil.saveString(context, DemoConfig.UserAuthUrl, authUrl)
    PreferenceUtil.saveString(context, DemoConfig.UserDevSerial, specifiedDevice)
  }

  /*
   * 移除账号信息
   */
  removeLastSDKInitParams() {
    let context = getContext()
    PreferenceUtil.saveString(context, DemoConfig.UserAppKey, '')
    PreferenceUtil.saveString(context, DemoConfig.UserAccessToken, '')
  }

  saveData(key: string, value: ValueType) {
    PreferenceUtil.saveString(getContext(), key, value)
  }

  /*
   * 获取保存数据
   */
  getSavedData(key: string): ValueType {
    return PreferenceUtil.getString(getContext(), key)
  }
}