import { StringUtils } from "../util/StringUtils";
import { buffer, util } from "@kit.ArkTS";
import { UserInfoModel } from "../model/cacheModel/UserInfoModel";
import { AppCache } from "./AppCache";

export class UserCache {
  public static userCache: UserCache = new UserCache();
  private userName: string = "";
  private userCode: string = "";
  private deptName: string = "";
  private deptCode: string = "";
  private idCardNum: string = "";
  private phoneNum: string = "";
  private userInfo: string = "";
  private authToken: string = ""
  private profilePhoto: string = "";
  private workPhone: string = "";
  private email: string = ""
  private age: number = 0
  private birthdate: string = ""
  private policeTypeId: string = ""
  private policeTypeName: string = ""
  private departmentCode: string = ""
  private departmentName: string = ""

  private constructor() {

  }

  public static getUserCache(): UserCache {
    if (UserCache.userCache == null) {
      UserCache.userCache = new UserCache();
    }
    return UserCache.userCache;
  }


  get getDepartmentCode(): string {
    return this.departmentCode;
  }

  set setDepartmentCode(departmentCode: string) {
    this.departmentCode = departmentCode;
  }
  get getDepartmentName(): string {
    return this.departmentName;
  }

  set setDepartmentName(departmentName: string) {
    this.departmentName = departmentName;
  }

  get getUserName(): string {
    return this.userName;
  }

  set setUserName(userName: string) {
    this.userName = userName;
  }

  get getUserCode(): string {
    return this.userCode;
  }

  set setUserCode(userCode: string) {
    this.userCode = userCode;
  }

  get getDeptName(): string {
    return this.deptName;
  }

  set setDeptName(deptName: string) {
    this.deptName = deptName;
  }

  get getDeptCode(): string {
    return this.deptCode;
  }

  set setDeptCode(deptCode: string) {
    this.deptCode = deptCode;
  }

  get getPhoneNum(): string {
    return this.phoneNum;
  }

  set setPhoneNum(phoneNum: string) {
    this.phoneNum = phoneNum;
  }

  get getIdCardNum(): string {
    return this.idCardNum;
  }

  set setIdCardNum(idCardNum: string) {
    this.idCardNum = idCardNum;
  }

  public getUserInfo(): string {
    if (StringUtils.isNullOrEmpty(this.userInfo)) {
      let cs: UserInfoModel = {
        "userCode": UserCache.getUserCache().getUserCode,
        "userName": UserCache.getUserCache().getUserName,
        "deptName": UserCache.getUserCache().getDeptName,
        "deptCode": UserCache.getUserCache().getDeptCode,
        "idCardNum": UserCache.getUserCache().getIdCardNum,
        "ip": AppCache.getAppCache().getLocalIp || "10.115.101.1",
        "departmentCode": UserCache.getUserCache().getDepartmentCode,
        "departmentName": UserCache.getUserCache().getDepartmentName,
      }
      let userInfo = JSON.stringify(cs)
      let byte = new Uint8Array(buffer.from(userInfo, 'utf-8').buffer);
      this.userInfo = new util.Base64Helper().encodeToStringSync(byte);
    }
    return this.userInfo
  }

  public set setUserInfo(userInfo: string) {
    this.userInfo = userInfo;
  }


  get getAuthToken(): string {
    return this.authToken;
  }

  set setAuthToken(authToken: string) {
    this.authToken = authToken;
  }

  get getProfilePhoto(): string {
    return this.profilePhoto;
  }

  set setProfilePhoto(profilePhoto: string) {
    this.profilePhoto = profilePhoto;
  }

  get getWorkPhone(): string {
    return this.workPhone;
  }

  set setWorkPhone(workPhone: string) {
    this.workPhone = workPhone;
  }

  get getEmail(): string {
    return this.email;
  }

  set setEmail(email: string) {
    this.email = email;
  }

  get getAge(): number {
    return this.age;
  }

  set setAge(age: number) {
    this.age = age;
  }

  get getBirthdate(): string {
    return this.birthdate;
  }

  set setBirthdate(birthdate: string) {
    this.birthdate = birthdate;
  }

  get getPoliceTypeId(): string {
    return this.policeTypeId;
  }

  set setPoliceTypeId(policeTypeId: string) {
    this.policeTypeId = policeTypeId;
  }
  get getPoliceTypeName(): string {
    return this.policeTypeName;
  }

  set setPoliceTypeName(policeTypeName: string) {
    this.policeTypeName = policeTypeName;
  }


}