import dataPreferences from '@ohos.data.preferences';
import util from '@ohos.util';
import cryptoFramework from "@ohos.security.cryptoFramework";
import fs from '@ohos.file.fs';
import UPLogger from './UPLogger';
import UPSysInfoUtil from './UPSysInfoUtil';
import UPConstant from '../common/UPConstant';
import UPPayEncrypt from './UPPayEncrypt';
export default class UPUtil {
    static async getDeviceInfo() {
        let i16 = {};
        i16['terminal_version'] = UPConstant.SDK_VERSION;
        try {
            let n16 = await UPSysInfoUtil.systemResolution();
            i16['terminal_resolution'] = n16;
        }
        catch (m16) {
            UPLogger.error('terminal_resolution获取失败，失败原因：' + JSON.stringify(m16));
        }
        i16['os_name'] = UPConstant.OS_NAME;
        i16['os_version'] = UPSysInfoUtil.systemVersion();
        i16['device_model'] = UPSysInfoUtil.machineType();
        i16['time_zone'] = UPSysInfoUtil.timeZone();
        i16['sign'] = UPSysInfoUtil.systemAppSign();
        i16['locale'] = UPSysInfoUtil.systemLanguage();
        i16['network_mode'] = UPSysInfoUtil.systemNetMode();
        i16['country'] = UPSysInfoUtil.systemRegion();
        i16['phone_model'] = UPSysInfoUtil.systemManufacture();
        i16['package'] = UPSysInfoUtil.systemBundleName();
        return new Promise((k16, l16) => {
            k16(i16);
        });
    }
    static uuidString() {
        let z15 = '';
        try {
            let b16 = { name: UPConstant.PREFERENCES_NAME };
            let c16 = dataPreferences.getPreferencesSync(UPUtil.getApplicationContext(), b16);
            let d16 = c16.getSync(UPConstant.PREFERENCES_KEY_UUID, '');
            let e16 = UPPayEncrypt.aesDecryptData(d16);
            if (e16) {
                z15 = e16;
            }
            else {
                let f16 = util.generateRandomUUID(true);
                let g16 = new RegExp('-', 'g');
                f16 = f16.replace(g16, '');
                let h16 = UPPayEncrypt.aesEncryptData(f16);
                c16.putSync(UPConstant.PREFERENCES_KEY_UUID, h16);
                c16.flush();
                z15 = h16;
            }
        }
        catch (a16) {
            UPLogger.error('uuidString生产失败，失败原因：' + JSON.stringify(a16));
        }
        return z15;
    }
    static strToInt(x15) {
        let y15 = Number.parseInt(x15, 10);
        return y15;
    }
    static bytesToHexStr(p15) {
        let q15 = "0123456789ABCDEF";
        let r15 = "";
        try {
            p15.forEach((u15) => {
                let v15 = (0xF0 & u15) >>> 4;
                let w15 = 0x0F & u15;
                r15 += q15.charAt(v15);
                r15 += q15.charAt(w15);
            });
        }
        catch (s15) {
            UPLogger.error('bytesToHexStr失败，失败原因：' + JSON.stringify(s15));
        }
        return r15;
    }
    static strToHexStr(k15) {
        let l15 = "";
        try {
            let n15 = new util.TextEncoder();
            let o15 = n15.encodeInto(k15);
            l15 = UPUtil.bytesToHexStr(o15);
        }
        catch (m15) {
            UPLogger.error('strToHexStr失败，失败原因：' + JSON.stringify(m15));
        }
        return l15;
    }
    static rsaEncryptMsg(g15, h15) {
        let i15 = '';
        try {
            i15 = UPPayEncrypt.forWap(UPUtil.strToInt(g15), h15);
        }
        catch (j15) {
            UPLogger.error('rsaEncryptMsg失败，失败原因：' + JSON.stringify(j15));
        }
        return i15;
    }
    static rsaDecryptMsg(c15, d15) {
        let e15 = '';
        try {
            e15 = UPPayEncrypt.forConfig(UPUtil.strToInt(c15), d15);
        }
        catch (f15) {
            UPLogger.error('rsaDecryptMsg失败，失败原因：' + JSON.stringify(f15));
        }
        return e15;
    }
    static verifySign(h14, i14, j14) {
        return new Promise((l14, m14) => {
            try {
                let o14 = cryptoFramework.createMd("SHA256");
                let p14 = new util.TextEncoder();
                let q14 = p14.encodeInto(i14);
                let r14 = { data: q14 };
                o14.update(r14).then(() => {
                    let b15 = o14.digest();
                    return b15;
                }).then((w14) => {
                    let x14 = UPUtil.bytesToHexStr(w14.data).toLowerCase();
                    let y14 = UPUtil.strToHexStr(x14);
                    let z14 = UPUtil.rsaDecryptMsg(h14, j14);
                    if (y14 === z14) {
                        l14(true);
                    }
                    else {
                        let a15 = {
                            code: UPConstant.VERIFY_ERROR_CODE,
                            name: UPConstant.ERROR_NAME,
                            message: UPConstant.VERIFY_ERROR_MSG
                        };
                        m14(a15);
                    }
                }).catch((v14) => {
                    m14(v14);
                });
            }
            catch (n14) {
                m14(n14);
            }
        });
    }
    static getServerUrl(e14) {
        let f14 = '';
        try {
            f14 = UPPayEncrypt.forUrl(UPUtil.strToInt(e14), true);
        }
        catch (g14) {
            UPLogger.error('getServerUrl失败，失败原因：' + JSON.stringify(g14));
        }
        return f14;
    }
    static getApplicationContext() {
        if (UPUtil.applicationContext) {
            return UPUtil.applicationContext;
        }
        else {
            let d14 = getContext().getApplicationContext();
            UPUtil.applicationContext = d14;
            return d14;
        }
    }
    static configPath(a14) {
        let b14 = UPUtil.getApplicationContext();
        let c14 = '';
        if (b14) {
            if (a14) {
                c14 = b14.filesDir + '/uppaymentconfig_seType_' + a14 + '.txt';
            }
            else {
                c14 = b14.filesDir + '/uppaymentconfig.txt';
            }
        }
        return c14;
    }
    static writeDataToFile(u13, v13) {
        if (u13 && v13) {
            try {
                let x13 = fs.openSync(v13, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE | fs.OpenMode.TRUNC);
                let y13 = UPPayEncrypt.aesEncryptData(u13);
                let z13 = fs.writeSync(x13.fd, y13);
                UPLogger.info("写入数据成功，数据大小为：" + z13);
            }
            catch (w13) {
                UPLogger.error("写入失败,原因为：" + JSON.stringify(w13));
            }
        }
    }
    static readDataFromFile(q13) {
        let r13 = '';
        if (q13) {
            try {
                let t13 = fs.readTextSync(q13);
                r13 = UPPayEncrypt.aesDecryptData(t13);
                UPLogger.info("读取文件成功，内容为:" + r13);
            }
            catch (s13) {
                UPLogger.error("读取文件失败，原因为:" + JSON.stringify(s13));
            }
        }
        return r13;
    }
    static writeDataToPreferences(d13, e13, f13) {
        return new Promise((h13, i13) => {
            dataPreferences.getPreferences(f13, UPConstant.PREFERENCES_NAME)
                .then((o13) => {
                let p13 = UPPayEncrypt.aesEncryptData(d13);
                o13.put(e13, p13);
                return o13;
            }).then((n13) => {
                n13.flush();
                h13();
            }).catch((m13) => {
                UPLogger.error("写入Preferences失败,原因为：" + JSON.stringify(m13));
                i13(m13);
            });
        });
    }
    static writeDatasToPreferences(p12, q12) {
        return new Promise((s12, t12) => {
            dataPreferences.getPreferences(q12, UPConstant.PREFERENCES_NAME)
                .then((z12) => {
                Object.entries(p12).forEach((b13) => {
                    let c13 = UPPayEncrypt.aesEncryptData(b13[1]);
                    z12.putSync(b13[0], c13);
                });
                return z12;
            }).then((y12) => {
                y12.flush();
                s12();
            }).catch((x12) => {
                UPLogger.error("写入Preferences失败,原因为：" + JSON.stringify(x12));
                t12(x12);
            });
        });
    }
    static readDataFromPreferences(e12, f12) {
        return new Promise((h12, i12) => {
            dataPreferences.getPreferences(f12, UPConstant.PREFERENCES_NAME)
                .then((m12) => {
                let n12 = m12.getSync(e12, '');
                let o12 = UPPayEncrypt.aesDecryptData(n12);
                h12(o12);
            }).catch((l12) => {
                UPLogger.error("从Preferences读取数据失败,原因为：" + JSON.stringify(l12));
                i12(l12);
            });
        });
    }
    static deleteDataFromPreferences(v11, w11) {
        return new Promise((y11, z11) => {
            dataPreferences.getPreferences(w11, UPConstant.PREFERENCES_NAME)
                .then((d12) => {
                d12.deleteSync(v11);
                d12.flush();
                y11();
            }).catch((c12) => {
                UPLogger.error("从Preferences删除数据失败,原因为：" + JSON.stringify(c12));
                z11(c12);
            });
        });
    }
    static seType2CarrierType(u11) {
        if (u11 === '02') {
            return '10';
        }
        else if (u11 === '04') {
            return '22';
        }
        else {
            return u11;
        }
    }
    static toCorrectScheme(s11) {
        let t11 = s11;
        if (!s11.endsWith('://')) {
            t11 = s11 + '://';
        }
        return t11;
    }
}
