import { util } from "@kit.ArkTS";
import { StrUtil } from "@pura/harmony-utils/src/main/ets/utils/StrUtil";

export class IdGeneratedUtils {
  private static idGeneratedUtils = new IdGeneratedUtils("");
  private static idCardNum: string = "";

  private constructor(idCardNum: string) {
    IdGeneratedUtils.idCardNum = idCardNum
  }

  public static getInstance(idCardNum: string): IdGeneratedUtils {
    if (IdGeneratedUtils.idGeneratedUtils == null || IdGeneratedUtils.idCardNum !== idCardNum) {
      IdGeneratedUtils.idGeneratedUtils = new IdGeneratedUtils(idCardNum)
    }
    return IdGeneratedUtils.idGeneratedUtils;
  }

  public generatedRandomID(): string {
    let uuid = util.generateRandomUUID(false).replaceAll("-", "").replaceAll("-", "");
    if (StrUtil.isNotEmpty(IdGeneratedUtils.idCardNum)) {
      let start = IdGeneratedUtils.idCardNum.substring(0, IdGeneratedUtils.idCardNum.length - 1);
      let end = IdGeneratedUtils.idCardNum.substring(IdGeneratedUtils.idCardNum.length - 1);
      let startStr = parseInt(start).toString(16).toUpperCase()
      return startStr + end + uuid + (this.getRandomInt(1,1000) + 1000);
    }else {
      return Date.now()+uuid+ (this.getRandomInt(1,1000) + 1000);
    }
  }

  // 生成指定范围的随机整数
   getRandomInt(min: number, max: number): number {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

}