
import { StringUtils } from "../util/StringUtils";
import { geoLocationManager } from "@kit.LocationKit";
import { UserCache } from "./UserCache";
import { IdGeneratedUtils } from "../util/IdGeneratedUtils";

/**
 * 统一认证系统的SysCode
 */
export  const  AUTH_SYSCODE = "11e8f9a0948a43d78f68c6af41c12242"

export const  BASE_IMAGE_URL = "http://20.90.2.2/pcenter_img_out";

export class AppCache {
  private static appCache: AppCache = new AppCache();
  private idGeneratedUtils :IdGeneratedUtils|undefined=undefined;
  
  /*请求服务的Ip*/
  private ip: string | undefined = undefined
  /*请求服务的端口*/
  private port: string | undefined = undefined
  private serviceNodeName:string = "/pcenter_app_out/"

  //用于存放品高的Token
  private pgToken :string| undefined = undefined;

  //权限状态
  private permissionsStatus: boolean = false;

  /* TODO 临时人员ID  用于避免每次查询都直接改变peopleId，导致查询失败后，之前查询的人员信息和ID对不上 */
  private peopleTemporarilyId: string = ""
  /* TODO 人员ID  只有当查询成功时才将 tempPeopleId的值 赋给 peopleId */
  private peopleId: string = ""
  /*车辆ID*/
  private carId: string = ""
  /* TODO 主ID 由APP生成 传递给后端 */
  private historyId: string = ""
  /*物品ID*/
  // private goodsId:string =""

  /*缓存的物品数据*/
  private goodsJson: string = ""
  //本机IP
  private localIp: string |undefined= "";

  /*任务id*/
  private taskId: string|undefined = ""

  private taskName:string|undefined = ""
  /*卡点id*/
  private cardPointId: string|undefined  = ""
  private cardPointName :string|undefined= ""

  //定位的坐标
  private locationX: number = 0.0;
  private locationY: number = 0.0;

  //卡点的坐标
  private cardPointX: string = "0.0";
  private cardPointY: string = "0.0";

  private locationAddress:string|undefined="";
  private cardPointAddress:string|undefined="";

  private constructor() {

  }

  //
  public static getAppCache(): AppCache {
    if (AppCache.appCache == null) {
      AppCache.appCache = new AppCache()
    }
    return AppCache.appCache;
  }

  /**
   * 服务节点
   * @param ip
   * @param port
   * @param serviceName
   */
  public  setIpOrPort(ip:string,port:string,serviceNodeName:string){
    this.ip = ip;
    this.port = port;
    this.serviceNodeName = serviceNodeName;
  }

  public getServiceAddress(){
    return `http://${this.ip}:${this.port}${this.serviceNodeName}`
  }

  /**
   * 获取一个随机ID 生存规则 四位随机数 + 随机UUID+ 四位随机数
   * @returns
   */
  public  getRandomId(): string {
    if (this.idGeneratedUtils == undefined) {
      this.idGeneratedUtils = IdGeneratedUtils.getInstance(UserCache.getUserCache().getIdCardNum)
    }
    return this.idGeneratedUtils.generatedRandomID();
    // return (Math.round(1000) + 1000) + util.generateRandomUUID(false) + (Math.round(1000) + 1000)
  }

  /**
   * 将临时人员ID设为ID
   * 在查询人员成功后调用
   */
  public temporarilyPeopleIdEffect() {
    this.peopleId = this.peopleTemporarilyId
    this.peopleTemporarilyId = "";
  }

  /**
   * TODO 创建一个临时的人员ID,用于查询人员信息时传参,当查询人员成功后，再赋值给{ peopleId }
   *
   */
  public createPeopleTemporarilyId() {
    this.peopleTemporarilyId = this.getRandomId();
  }

  public getPeopleTemporarilyId(): string {
    if (StringUtils.isNullOrEmpty(this.peopleTemporarilyId)) {
      this.peopleTemporarilyId = this.getRandomId();
    }
    return this.peopleTemporarilyId;
  }

  public getPeopleId(): string {
    if (StringUtils.isNullOrEmpty(this.peopleId)) {
      this.peopleId = this.getPeopleTemporarilyId();
    }
    return this.peopleId
  }

  public getCarId(): string {
    if (StringUtils.isNullOrEmpty(this.carId)) {
      this.carId = this.getRandomId();
    }
    return this.carId;
  }

  public getHistoryId(): string {
    if (StringUtils.isNullOrEmpty(this.historyId)) {
      this.historyId = this.getRandomId();
    }
    return this.historyId;
  }

  /**
   * TODO 仅在初始化页面和提交或移交完成后调用
   * 重置ID
   */
  public resetIds(){
    this.historyId = this.getRandomId();
    this.peopleId =  this.getRandomId();
    this.carId =  this.getRandomId();
    this.createPeopleTemporarilyId()
  }

  /**
   * 提交成功重置id  Submitted successfully
   */
  public submitSuccessResetIDs(){
    this.peopleId = ""
    this.peopleTemporarilyId = ""
    this.carId = ""
    this.historyId = ""
    this.peopleId = ""
    this.peopleId = ""
  }


  /**
   * 权限状态
   * @returns
   */
  get getPermissionsStatus(): boolean {
    return this.permissionsStatus;
  }

  set setPermissionsStatus(permissionsStatus: boolean) {
    this.permissionsStatus = permissionsStatus;
  }

  public setLocation(location :geoLocationManager.Location){
    if (location == null) return
    this.locationX = location.longitude;
    this.locationY = location.latitude;
  }

  public getLocationX():number{
    return this.locationX;
  }
  public getLocationY():number{
    return this.locationY;
  }

  public setCardLocation(cardPointX:string,cardPointY:string,address:string){
    this.cardPointX = cardPointX;
    this.cardPointY = cardPointY;
    this.cardPointAddress = address;
  }

  public getCardPointLocationX():string{
    return this.cardPointX;
  }
  public getCardPointLocationY():string{
    return this.cardPointY;
  }

  public set setLocalIp(localIp :string|undefined){
    this.localIp = localIp;
  }

  public get getLocalIp():string|undefined{
    return this.localIp;
  }

  public get getPgToken():string|undefined{
    return this.pgToken
  }

  public set setPgToken(pgToken:string){
    this.pgToken = pgToken;
  }

  public get getTaskId(){
    if(StringUtils.isNullOrEmpty(this.taskId)){
      return "";
    }
    if (this.taskId != undefined){
      return this.taskId;
    }
    return ''
  }

  public set setTaskId(taskId:string|undefined){
    this.taskId = taskId;
  }

  public get getTaskName():string{
    if(StringUtils.isNullOrEmpty(this.taskName)){
      return "";
    }
    if (this.taskName != undefined){
      return this.taskName;
    }
    return ''
  }

  public set setTaskName(taskName:string|undefined){
    this.taskName = taskName;
  }

  public get getCardPointIdId(){
    if(StringUtils.isNullOrEmpty(this.cardPointId)){
      return "";
    }
    if (this.cardPointId != undefined){
      return this.cardPointId;
    }
    return ''
  }

  public set setCardPointId(cardPointId:string|undefined){
    this.cardPointId = cardPointId;
  }

  public get getCardPointName():string{
    if(StringUtils.isNullOrEmpty(this.cardPointName)){
      return "";
    }
    if (this.cardPointName != undefined){
      return this.cardPointName;
    }
    return ''
  }

  public set setCardPointName(cardPointName:string|undefined){
    this.cardPointName = cardPointName;
  }

  public get getLocationAddress():string{
    if(StringUtils.isNullOrEmpty(this.locationAddress)){
      return "";
    }
    if (this.locationAddress != undefined){
      return this.locationAddress;
    }
    return ''
  }

  public set setLocationAddress(locationAddress:string|undefined){
    this.locationAddress = locationAddress;
  }

  public get getCardPointAddress():string{
    if(StringUtils.isNullOrEmpty(this.cardPointAddress)){
      return "";
    }
    if (this.cardPointAddress != undefined){
      return this.cardPointAddress;
    }
    return ''
  }

  /**
   * TODO 图片地址转换
   * @param photoUrl        原始图片地址
   * @returns
   */
  convertPhotoUrl(photoUrl:string):string{
    if (StringUtils.isNullOrEmpty(photoUrl)) return""
    return photoUrl.replace("http://53.1.214.228:9000/ypc","http://20.90.2.2/rcwhy_image_out")
      .replace("http://53.1.227.17:9000/ypc","http://20.90.2.2/rcwhy_image_out")
  }
}