var HTTPS = require('./ts/HTTPS').HTTPSIns


window.Utils = {
    // name: 'Utils',

    javaToJs(id) {
        // const ANDROIDApi = require('ANDROIDApi');
        // ANDROIDApi.payOk(id)
    },

    stringFormat(args) {
        let out = arguments[0];
        if (arguments.length >= 1) {
            for (let key in arguments) {
                if (arguments[key] !== undefined && key > 0) {
                    out = out.replace('{' + (key - 1) + '}', arguments[key]);
                }
            }
        }
        return out;
    },

    versionStrToInt(ver) {
        let verArr = ver.split('.');
        return verArr.reduce((total, cur, index) => {
            return (total + Number(cur) * Math.pow(10, verArr.length - index - 1));
        }, 0);
    },
    //获取当前时间，格式YYYY-MM-DD
    getNowFormatDate() {
        let date = new Date();
        // let seperator1 = "-";
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        // let currentdate = year + seperator1 + month + seperator1 + strDate;
        let currentdate = year.toString() + month.toString() + strDate.toString();
        return currentdate;
    },

    isSecondDay() {
        // 计算是否到了第二天
        let yesterday = CC_WECHATGAME ? wx.getStorageSync('yesterday') : cc.sys.localStorage.getItem('yesterday');
        yesterday = yesterday ? yesterday : 0;
        let today = Utils.getNowFormatDate();
        if (CC_WECHATGAME) {
            wx.setStorageSync('yesterday', today);
        } else {
            cc.sys.localStorage.setItem('yesterday', today);
        }
        if (Number(today) > Number(yesterday))
            return true;
        return false;
    },

    isFirstLogin() {
        let isFirstLogin = CC_WECHATGAME ? wx.getStorageSync('isFirstLogin') : cc.sys.localStorage.getItem('isFirstLogin');
        if (isFirstLogin) return false;
        cc.sys.localStorage.setItem('isFirstLogin', 'true');
        if (CC_WECHATGAME) {
            wx.setStorageSync('isFirstLogin', 'true')
        }
        return true;
    },

    getTodayReconmendNum() {
        let num = cc.sys.localStorage.getItem('recommendNum');
        if (CC_WECHATGAME) {
            num = wx.getStorageSync('recommendNum')
        }
        num = num ? num : 0;
        return Number(num);
    },

    addTodayReconmendNum() {
        let num = cc.sys.localStorage.getItem('recommendNum');
        if (CC_WECHATGAME) {
            num = wx.getStorageSync('recommendNum');
        }
        num = num ? Number(num) : 0;
        num++;
        cc.sys.localStorage.setItem('recommendNum', num);
        if (CC_WECHATGAME) {
            wx.setStorageSync('recommendNum', num);
        }
    },

    clearTodayReconmendNum() {
        cc.sys.localStorage.setItem('recommendNum', 0);
        if (CC_WECHATGAME) {
            wx.setStorageSync('recommendNum', 0);
        }
    },

    //生成从minNum到maxNum的随机数
    randomNum(minNum, maxNum) {
        return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum);
    },

    // 生成3-32位随机串：randomString(true, 3, 32)
    // 生成43位随机串：randomString(false, 43)
    randomString(randomLen, min, max) {
        let str = "",
            range = min,
            arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
                'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
                'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        // 随机产生
        if (randomLen) {
            range = Math.round(Math.random() * (max - min)) + min;
        }
        for (let i = 0; i < range; i++) {
            let pos = Math.round(Math.random() * (arr.length - 1));
            str += arr[pos];
        }
        return str;
    },

    encryptedData(appId, sessionKey) {
        // let WXBizDataCrypt = require('./WXBizDataCrypt')
        // // let QQApi = require("QQApi");
        // let encryptedData = 
        //     'CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM'+
        //     'QmRzooG2xrDcvSnxIMXFufNstNGTyaGS'+
        //     '9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+'+
        //     '3hVbJSRgv+4lGOETKUQz6OYStslQ142d'+
        //     'NCuabNPGBzlooOmB231qMM85d2/fV6Ch'+
        //     'evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6'+
        //     '/1Xx1COxFvrc2d7UL/lmHInNlxuacJXw'+
        //     'u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn'+
        //     '/Hz7saL8xz+W//FRAUid1OksQaQx4CMs'+
        //     '8LOddcQhULW4ucetDf96JcR3g0gfRK4P'+
        //     'C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB'+
        //     '6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns'+
        //     '/8wR2SiRS7MNACwTyrGvt9ts8p12PKFd'+
        //     'lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV'+
        //     'oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG'+
        //     '20f0a04COwfneQAGGwd5oa+T8yO5hzuy'+
        //     'Db/XcxxmK01EpqOyuxINew=='
        // let iv = 'r7BXXKkLb8qrSNn05n0qiA=='

        // let pc = new WXBizDataCrypt(appId, sessionKey)
        // let data = pc.decryptData(encryptedData , iv)
        // console.log('解密后 data: ', data)
        // return data;
    },

    hadRedPacket() {
        let num = cc.sys.localStorage.getItem('hadRedPacket');
        if (CC_WECHATGAME) {
            num = wx.getStorageSync('hadRedPacket');
        }
        if (num) return true;
        return false;
    },

    setRedPacket() {
        if (CC_WECHATGAME) {
            wx.setStorageSync('hadRedPacket', 'true')
        } else {
            cc.sys.localStorage.setItem('hadRedPacket', 'true');
        }
    },

    clearRedPacket() {
        if (CC_WECHATGAME) {
            wx.removeStorageSync('hadRedPacket')
        } else {
            cc.sys.localStorage.removeItem('hadRedPacket');
        }
    },

    getRedPacketNum() {
        // let num = cc.sys.localStorage.getItem('RedPacketNum');
        // if (CC_WECHATGAME) {
        //     num = wx.getStorageSync('RedPacketNum');
        // }
        // num = num ? num : 0;

        // HTTPS.Instance.user_info.balance
        return Number(HTTPS.user_info.balance);
    },

    addRedPacketNum(value) {
        let num = cc.sys.localStorage.getItem('RedPacketNum');
        if (CC_WECHATGAME) {
            num = wx.getStorageSync('RedPacketNum');
        }
        num = num ? Number(num) : 0;
        num += value;
        num = num.toFixed(2);
        // num += '.00';
        cc.sys.localStorage.setItem('RedPacketNum', num);
        if (CC_WECHATGAME) {
            wx.setStorageSync('RedPacketNum', num);
        }
    },

    reduceRedPacketNum(value) {
        let num = cc.sys.localStorage.getItem('RedPacketNum');
        if (CC_WECHATGAME) {
            num = wx.getStorageSync('RedPacketNum');
        }
        num = num ? Number(num) : 0;
        num -= value;
        num = num.toFixed(2);
        // num += '.00';
        if (num <= 0) num = 0;
        cc.sys.localStorage.setItem('RedPacketNum', num);
        if (CC_WECHATGAME) {
            wx.setStorageSync('RedPacketNum', num);
        }
    },

    hadSignIn() {
        let num = cc.sys.localStorage.getItem('hadSignIn');
        if (CC_WECHATGAME) {
            num = wx.getStorageSync('hadSignIn');
        }
        if (num) return true;
        return false;
    },

    clearSignIn() {
        if (CC_WECHATGAME) {
            wx.removeStorageSync('hadSignIn')
        } else {
            cc.sys.localStorage.removeItem('hadSignIn');
        }
    },

    setSignIn() {
        cc.sys.localStorage.setItem('hadSignIn', "true");
        if (CC_WECHATGAME) {
            wx.setStorageSync('hadSignIn', "true");
        }
    },

    // getHadExchangeRedPacketNum() {
    //     let num = cc.sys.localStorage.getItem('HadExchangeRedPacketNum');
    //     if (CC_WECHATGAME) {
    //         num = wx.getStorageSync('HadExchangeRedPacketNum');
    //     }
    //     num = num ? Number(num) : 0;
    //     let value = this.randomNum(5, 9);
    //     num += value;
    //     if (num >= 50) num = 50;
    //     cc.sys.localStorage.setItem('HadExchangeRedPacketNum', num);
    //     if (CC_WECHATGAME) {
    //         wx.setStorageSync('HadExchangeRedPacketNum', num);
    //     }
    //     return num;
    // },

    // clearHadExchangeRedPacketNum() {
    //     cc.sys.localStorage.setItem('HadExchangeRedPacketNum', 0);
    //     if (CC_WECHATGAME) {
    //         wx.setStorageSync('HadExchangeRedPacketNum', 0);
    //     }
    // },

    // 数组从小到大排序
    arrSort(arr) {
        for (let i = 0; i < arr.length - 1; i++) {
            for (let j = i + 1; j < arr.length; j++) {
                let cur = arr[i];
                if (cur > arr[j]) {
                    let index = arr[j];
                    arr[j] = cur;
                    arr[i] = index;
                }
            }
        }
        return arr;
    },
    // 数组重新排序并去重
    deDuplication(arr) {
        let mArr = [];
        for (let i = 0; i < arr.length; i++) {
            if (mArr.indexOf(arr[i]) == -1) {
                mArr.push(arr[i]);
            }
        }
        mArr = this.arrSort(mArr);
        return mArr;
    },

    getConfig(cb) {

        let remoteUrl = AppConst.HTTPURL + "/config/popStarConfig.json";
        let xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {

                console.log("远程config加载完成")
                let response = xhr.responseText;
                cb && cb(response);

            } else {
                console.log("远程config加载失败")

            }
        };
        xhr.open("GET", remoteUrl, true);
        xhr.send();


    },

};

