import { JsonAsset } from "cc";
import { MathUtil } from "./MathUtil";

const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");

export class StringUtil {

    /**
     * 得到一个唯一值
     * @param len 长度
     * @param radix 进制
     */
    static getUUID(len: number = 12, radix: number = 16) {
        let uuid = [];
        radix = radix || chars.length;
        if (len) {
            for (let i = 0; i < len; ++i)
                uuid[i] = chars[0 | Math.random() * radix];
        } else {
            uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";
            uuid[14] = "4";
            for (let i = 0; i < 36; ++i) {
                if (!uuid[i]) {
                    let r = 0 | Math.random() * 16;
                    uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                }
            }
        }
        return uuid.join("");
    }
    /**
     * 解析配置表内容 
     * @param str 配置表中内容  1:20#2:20
     * */
    static analysisConfStr(str) {
        let replaceStr = str.replaceAll('#', ',"')
        replaceStr = replaceStr.replaceAll(':', '":')
        let ObjStr = `{"${replaceStr}}`
        let Json = JSON.parse(ObjStr)
        return Json
    }
    /**
     * 解析配置表内容 
     * @param str 配置表中内容  {"123":123}#{"1232":123}
     * */
    static analysisConfStrA(str) {
        let replaceStr = str.replaceAll('#', ',')
        let Json = JSON.parse(`[${replaceStr}]`)
        return Json
    }
    /**
     * 解析配置表内容 
     * @param str 配置表中内容  attack#hp#defence#speed
     * */
    static analysisConfStrE(str) {
        let replaceStr = str.replaceAll('#', '","');
        let Json = JSON.parse(`["${replaceStr}"]`)
        return Json
    }
    /**
     * 解析配置表内容 
     * @param str 配置表中内容  speed:60
     * */
    static analysisConfStrB(str) {
        let replaceStr = str.replaceAll(':', ',')
        let Json = JSON.parse(`[${replaceStr}]`)
        return Json
    }
    /**
     * 解析配置表内容 
     * @param str 配置表中内容  speed:14,hp:500,attack:105,defence:38#speed:14,hp:500,attack:105,defence:50
     * */
    static analysisConfStrC(str) {
        let replaceStr = str.replaceAll(',', ',"')
        replaceStr = replaceStr.replaceAll('#', '}],[{"')
        replaceStr = replaceStr.replaceAll(':', '":')
        let Json = JSON.parse(`[[{"${replaceStr}}]]`)
        // speed":14,"hp":500,"attack":105,"defence":38],["speed":14,"hp":500,"attack":105,"defence":50]]
        // let strA =[{speed:14,hp:500,attack":105,defence":38},{speed:14,hp:500,attack:105}]
        return Json
    }
    /**
     * 解析配置表内容 
     * @param str 配置表中内容  90100201:1000,90100202:1000
     * */
    static analysisConfStrD(str) {
        let replaceStr = str.replaceAll(',', ',"')
        replaceStr = replaceStr.replaceAll(':', '":')
        let Json = JSON.parse(`{"${replaceStr}}`)
        // speed":14,"hp":500,"attack":105,"defence":38],["speed":14,"hp":500,"attack":105,"defence":50]]
        // let strA =[{speed:14,hp:500,attack":105,defence":38},{speed:14,hp:500,attack:105}]
        return Json
    }
    /**
    * 解析配置奖励内容 
    * @param str 配置表中内容  material.1:100#material.1:100
    * */
    static analysisConfStrM(str) {
        let value = [];
        var allValue = [];
        var allAward = str.split('#');
        for (var n = 0; n < allAward.length; n++) {
            var oneAward = allAward[n].split(':');
            if (2 == oneAward.length) {
                value = oneAward[0].split('.');
                value.push(+oneAward[1]);
            } else {
                value = null;
            }
            allValue.push(value);
        }
        value = allValue;
        return value
    }

    /**
    * 概率计算
    * @param total 所有和
    * @param num 当前数值
    * */
    static oodsCount(total, num) {
        return MathUtil.accMul(num / total, 100) + '%';
    }

    /**
     * 格式化字符串
     * @param str 目标字符串
     * @param val 自定义参数
     */
    static format(str: string, ...val: any[]) {
        //format("{0} is {1} {2}", "This", "formatting", "hack");

        for (let i = 0; i < val.length; ++i) {
            str = str.replace(`{${i}}`, val[i].toString());
        }
        return str;
    }

    /**
     * 格式化数字,12345 = 12.35K
     * @param value 数字
     * @param fixed 保留小数位
     */
    static numberToThousand(value: number, fixed: number = 2) {
        let k = 1000;
        let sizes = ["", "K", "M", "G"];
        if (value < k) {
            return value.toString();
        } else {
            let i = Math.floor(Math.log(value) / Math.log(k));
            let r = ((value / Math.pow(k, i)));
            return r.toFixed(fixed) + sizes[i];
        }
    }

    /**
     * 格式化数字,12345 = 12345 = 1.23万
     * @param value 数字
     * @param fixed 保留小数位
     */
    static numberToTenThousand(value: number, fixed: number = 2) {
        let k = 10000;
        let sizes = ["", "万", "亿", "万亿"];
        if (value < k) {
            let count = value
            if (value.toString().length > 8) {
                count = Number(value.toFixed(2))
            }
            return count.toString();
        } else {
            let i = Math.floor(Math.log(value) / Math.log(k));
            // let count = ((value / Math.pow(k, i))).toFixed(fixed) + sizes[i];
            return ((value / Math.pow(k, i))).toFixed(fixed) + sizes[i];
        }
    }
}