let library = {
    //数组元素位置替换
    swapArray: function(arr, index1, index2) {
        arr[index1] = arr.splice(index2, 1, arr[index1])[0];
        return arr;
    },
    swapLeft : function(arr) {
        let element = arr.shift();
        arr.push(element);
    },
    //生成指定范围随机整数
    randomFromIntRange: function(min, max) {
        return Math.floor(Math.random() * (max - min)) + min;
    },
    happenRate: function(rate) {
        let result = this.randomFromIntRange(0,100);
        if(result<rate)
        {
            return true;
        }
        else
        {
            return false;
        }
    },
    openInWebview () {
        let ua = navigator.userAgent.toLowerCase()
        if (ua.match(/MicroMessenger/i) == 'micromessenger') { // 微信浏览器判断
            return false
        } else if (ua.match(/QQ/i) == 'qq') { // QQ浏览器判断
            return false
        } else if (ua.match(/WeiBo/i) == "weibo") {
            return false
        } else {
            if (ua.match(/Android/i) != null) {
                return ua.match(/browser/i) == null
            } else if (ua.match(/iPhone/i) != null) {
                return ua.match(/safari/i) == null
            } else {
                return (ua.match(/macintosh/i) == null && ua.match(/windows/i) == null)
            }
        }
    },
    indexOf (arr,item) {
        for (let i = 0; i < arr.length; i++) {
            if (arr[i] == item) return i;
        }
        return -1;
    },
    remove(arr, item) {
        let index = arr.indexOf(item);
        if (index > -1) {
            arr.splice(index, 1);
        }
    },
    setImageBase64(base64Url,callback){
        let img = new Image();
        img.src = base64Url;
        img.onload = function(){
            let texture = new cc.Texture2D();
            texture.initWithElement(img);
            texture.handleLoadedTexture();
            if (callback)
                callback(texture);
        }
    },
    /**
     * 压缩
     */
    compress(strNormalString) {
        alert("压缩前长度：" + strNormalString.length);
        var strCompressedString = "";

        var ht = new Array();
        for(i = 0; i < 128; i++) {
            ht[i] = i;
        }

        var used = 128;
        var intLeftOver = 0;
        var intOutputCode = 0;
        var pcode = 0;
        var ccode = 0;
        var k = 0;

        for(var i=0; i<strNormalString.length; i++) {
            ccode = strNormalString.charCodeAt(i);
            k = (pcode << 8) | ccode;
            if(ht[k] != null) {
                pcode = ht[k];
            } else {
                intLeftOver += 12;
                intOutputCode <<= 12;
                intOutputCode |= pcode;
                pcode = ccode;
                if(intLeftOver >= 16) {
                    strCompressedString += String.fromCharCode( intOutputCode >> ( intLeftOver - 16 ) );
                    intOutputCode &= (Math.pow(2, (intLeftOver - 16)) - 1);
                    intLeftOver -= 16;
                }
                if(used < 4096) {
                    used ++;
                    ht[k] = used - 1;
                }
            }
        }

        if(pcode != 0) {
            intLeftOver += 12;
            intOutputCode <<= 12;
            intOutputCode |= pcode;
        }

        if(intLeftOver >= 16) {
            strCompressedString += String.fromCharCode( intOutputCode >> ( intLeftOver - 16 ) );
            intOutputCode &= (Math.pow(2,(intLeftOver - 16)) - 1);
            intLeftOver -= 16;
        }

        if( intLeftOver > 0) {
            intOutputCode <<= (16 - intLeftOver);
            strCompressedString += String.fromCharCode( intOutputCode );
        }

        alert("压缩后长度：" + strCompressedString.length);
        return strCompressedString;
    },
    /**
     * 解压缩
     */
    decompress(strCompressedString) {

        var strNormalString = "";
        var ht = new Array();

        for (i = 0; i < 128; i++) {
            ht[i] = String.fromCharCode(i);
        }

        var used = 128;
        var intLeftOver = 0;
        var intOutputCode = 0;
        var ccode = 0;
        var pcode = 0;
        var key = 0;

        for (var i = 0; i < strCompressedString.length; i++) {
            intLeftOver += 16;
            intOutputCode <<= 16;
            intOutputCode |= strCompressedString.charCodeAt(i);

            while (1) {
                if (intLeftOver >= 12) {
                    ccode = intOutputCode >> (intLeftOver - 12);
                    if (typeof (key = ht[ccode]) != "undefined") {
                        strNormalString += key;
                        if (used > 128) {
                            ht[ht.length] = ht[pcode] + key.substr(0, 1);
                        }
                        pcode = ccode;
                    } else {
                        key = ht[pcode] + ht[pcode].substr(0, 1);
                        strNormalString += key;
                        ht[ht.length] = ht[pcode] + key.substr(0, 1);
                        pcode = ht.length - 1;
                    }

                    used++;
                    intLeftOver -= 12;
                    intOutputCode &= (Math.pow(2, intLeftOver) - 1);
                } else {
                    break;
                }
            }
        }
        return strNormalString;
    }
};
module.exports = library;
