import HashMap from '@ohos.util.HashMap';
import { Base64Util } from './Base64Util';
import { CryptoJS } from '@ohos/crypto-js';
import { logger } from './logger';
import image from '@ohos.multimedia.image';
import fs from '@ohos.file.fs';
import { fileIo } from '@kit.CoreFileKit';
import util from '@ohos.util';
import { i18n } from '@kit.LocalizationKit';
export class ChangeUtil {
  /**
   * 将HashMap转成JsonString
   * @param map
   * @returns
   */
  static map2Json(map:HashMap<string, string>): string {
    let jsonObject: Record<string, Object> = {};
    map.forEach((value, key) => {
      if(key != undefined && value != undefined){
        jsonObject[key] = value;
      }
    })
    return JSON.stringify(jsonObject);
  }

  static getSign(extraDatas1: HashMap<string, string>, secret: string): string {
    if(secret!=null) {
      let keyValueStr: string = "";
      let entriesArray: Array<string> = Array.from(extraDatas1.keys());
      entriesArray.sort();

      let sortedMap:HashMap<string, string>  = new HashMap();
      entriesArray.forEach((value: string, index: number) => {
        sortedMap.set(value,extraDatas1.get(value));
        keyValueStr +=value+extraDatas1.get(value)
      });
      keyValueStr = keyValueStr.replace(" ", "");
      keyValueStr = keyValueStr + CryptoJS.MD5(secret).toString();
      let Md5keyValueStr: string = CryptoJS.MD5(keyValueStr).toString();
      let base64Str:string=Base64Util.encodeToStrSync(Md5keyValueStr);
      return base64Str;
    }
    else
    {
      return '';
    }
  }


  static isMobileNum(mobiles:string): boolean {
    const reg2: RegExp = new RegExp('^(1[3-9])[0-9]{9}$')

  return reg2.test(mobiles);
}
  static isPassword(password:string): boolean {
    const reg2: RegExp = new RegExp('^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$')

    return reg2.test(password);
  }


  static async  convertUriToBase64(uri: string): Promise<string> {
    // 打开文件并读取数据
    const file = fileIo.openSync(uri, fileIo.OpenMode.READ_ONLY);
    const stat = await fileIo.stat(file.fd);
    const buffer = new ArrayBuffer(stat.size);
    fileIo.readSync(file.fd, buffer);
    fileIo.closeSync(file);

    // 编码为 Base64
    const base64Helper = new util.Base64Helper();
    return base64Helper.encodeToStringSync(new Uint8Array(buffer));
  }
  /**
   * 将图片转换为base64字符串
   * @param imageUri 图片URI
   * @returns Promise<string> base64字符串
   */
  static async imageToBase64(imageUri: string): Promise<string> {
    // 1. 验证URI有效性
    if (!imageUri || !imageUri.startsWith('file://')) {
      throw new Error('无效的图片URI，必须以file://开头');
    }
    let imageSource: image.ImageSource | undefined;
    let pixelMap: image.PixelMap | undefined;
    let imagePacker: image.ImagePacker | undefined;
    try {
      const file = fs.openSync(imageUri, fs.OpenMode.READ_ONLY);
      const imageSource = image.createImageSource(file.fd);
      if (!imageSource) {
        throw new Error('创建ImageSource失败，请检查图片路径');
      }
      logger.info('正在获取图片信息...');
      const imageInfo = await imageSource.getImageInfo();
      logger.info(`图片尺寸: ${imageInfo.size.width}x${imageInfo.size.height}`);
      pixelMap = await imageSource.createPixelMap({
        desiredSize: {
          width: imageInfo.size.width,
          height: imageInfo.size.height
        }
      });
      if (!pixelMap) {
        throw new Error('创建PixelMap失败');
      }
      // 5. 压缩图片
      imagePacker = image.createImagePacker();
      const packOpts: image.PackingOption = {
        format: "image/jpeg",
        quality: 80  // 适当提高质量保证清晰度
      };
      logger.info('正在压缩图片...');
      const arrayBuffer = await imagePacker.packing(pixelMap, packOpts);
      const unit8Array = new Uint8Array(arrayBuffer);
      let binary = '';
      unit8Array.forEach(byte => {
        binary += String.fromCharCode(byte);
      });
      const base64String = Base64Util.encodeToStrSync(binary);
      logger.info(`图片转换成功，大小: ${Math.round(base64String.length / 1024)}KB`);
      return base64String;
    } catch (error) {
      logger.error('图片处理失败: ' + JSON.stringify(error));
      throw new Error(`图片处理失败: ${error.message}`);
    } finally {
      // 7. 确保释放资源
      try {
        pixelMap?.release();
        imageSource?.release();
        imagePacker?.release();
      } catch (e) {
        logger.error('资源释放异常: ' + JSON.stringify(e));
      }
    }
  }

  static isLetter(char: string): boolean {
    if (char.length !== 1) return false;
    const code = char.charCodeAt(0);
    return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
  }
  static isFirstDigit(str: string): boolean {
    if (str.length === 0) return false;
    return i18n.Unicode.isDigit(str.charAt(0));
  }

}