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;
        }
        return;
    },
    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)
        //     }
        // }
        let sUserAgent = navigator.userAgent.toLowerCase()

        let bIsMobile = /AppleWebKit.*Mobile/i.test(navigator.userAgent)

        let bIsIpad = sUserAgent.match(/ipad/i) !== null

        let bIsIphoneOs = sUserAgent.match(/iphone os/i) !== null

        let bIsMidp = sUserAgent.match(/midp/i) !== null

        let bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) !== null

        let bIsUc = sUserAgent.match(/ucbrowser/i) !== null

        let bIsAndroid = sUserAgent.match(/android/i) !== null

        let bIsCE = sUserAgent.match(/windows ce/i) !== null

        let bIsWM = sUserAgent.match(/windows mobile/i) !== null

        if (

            bIsMobile ||

            bIsIpad ||

            bIsIphoneOs ||

            bIsMidp ||

            bIsUc7 ||

            bIsUc ||

            bIsAndroid ||

            bIsCE ||

            bIsWM

        ) {
            return false;

        } else {
            return true;

        }

    },
    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);
        }
    },
};
module.exports = library;
