
//暂时只有gltf里会用到。如果还有其他地方用到，放到utils里
const _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
/**
 * 编码
 * @param input 
 * @param utf8Encode 
 * @returns 
 */
export function btoa_base64Encode(input, utf8Encode = false) {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;
    if (utf8Encode) input = _utf8_encode(input);//加了会出问题,TODO
    while (i < input.length) {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }
        output = output +
            _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
            _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
    }
    return output;
}
/**
 * 解码
 * @param input 
 * @param utf8Decode 
 * @returns 
 */
export function atob_base64Decode(input, utf8Decode = false) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    while (i < input.length) {
        enc1 = _keyStr.indexOf(input.charAt(i++));
        enc2 = _keyStr.indexOf(input.charAt(i++));
        enc3 = _keyStr.indexOf(input.charAt(i++));
        enc4 = _keyStr.indexOf(input.charAt(i++));
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output = output + String.fromCharCode(chr1);
        if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }
    }
    if (utf8Decode) output = _utf8_decode(output);//加了会出问题,TODO
    return output;
}
// private method for UTF-8 encoding  
function _utf8_encode(string) {
    string = string.replace(/\r\n/g, "\n");
    var utftext = "";
    for (var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);
        if (c < 128) {
            utftext += String.fromCharCode(c);
        } else if ((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        } else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }

    }
    return utftext;
}
// private method for UTF-8 decoding  
function _utf8_decode(utftext) {
    var string = "";
    var i = 0;
    var c = 0;
    var c2 = 0;
    var c3 = 0
    while (i < utftext.length) {
        c = utftext.charCodeAt(i);
        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        } else if ((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i + 1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = utftext.charCodeAt(i + 1);
            c3 = utftext.charCodeAt(i + 2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    return string;
}

////////////////////////下面的是jquery，2012年的代码

// var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
//     a256 = '',
//     r64 = [256],
//     r256 = [256],
//     i = 0;

// var UTF8 = {

//     /**
//      * Encode multi-byte Unicode string into utf-8 multiple single-byte characters
//      * (BMP / basic multilingual plane only)
//      *
//      * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
//      *
//      * @param {String} strUni Unicode string to be encoded as UTF-8
//      * @returns {String} encoded string
//      */
//     encode: function (strUni) {
//         // use regular expressions & String.replace callback function for better efficiency
//         // than procedural approaches
//         var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
//             function (c) {
//                 var cc = c.charCodeAt(0);
//                 return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f);
//             })
//             .replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
//                 function (c) {
//                     var cc = c.charCodeAt(0);
//                     return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f);
//                 });
//         return strUtf;
//     },

//     /**
//      * Decode utf-8 encoded string back into multi-byte Unicode characters
//      *
//      * @param {String} strUtf UTF-8 string to be decoded back to Unicode
//      * @returns {String} decoded string
//      */
//     decode: function (strUtf) {
//         // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
//         var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
//             function (c) { // (note parentheses for precence)
//                 var cc = ((c.charCodeAt(0) & 0x0f) << 12) | ((c.charCodeAt(1) & 0x3f) << 6) | (c.charCodeAt(2) & 0x3f);
//                 return String.fromCharCode(cc);
//             })
//             .replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
//                 function (c) { // (note parentheses for precence)
//                     var cc = (c.charCodeAt(0) & 0x1f) << 6 | c.charCodeAt(1) & 0x3f;
//                     return String.fromCharCode(cc);
//                 });
//         return strUni;
//     }
// };

// while (i < 256) {
//     var c = String.fromCharCode(i);
//     a256 += c;
//     r256[i] = i;
//     r64[i] = b64.indexOf(c);
//     ++i;
// }

// function code(s, discard, alpha, beta, w1, w2) {
//     s = String(s);
//     var buffer = 0,
//         i = 0,
//         length = s.length,
//         result = '',
//         bitsInBuffer = 0;

//     while (i < length) {
//         var c = s.charCodeAt(i);
//         c = c < 256 ? alpha[c] : -1;

//         buffer = (buffer << w1) + c;
//         bitsInBuffer += w1;

//         while (bitsInBuffer >= w2) {
//             bitsInBuffer -= w2;
//             var tmp = buffer >> bitsInBuffer;
//             result += beta.charAt(tmp);
//             buffer ^= tmp << bitsInBuffer;
//         }
//         ++i;
//     }
//     if (!discard && bitsInBuffer > 0) result += beta.charAt(buffer << (w2 - bitsInBuffer));
//     return result;
// }

// // var Plugin = function (dir, input, encode) {
// //     return input ? Plugin[dir](input, encode) : dir ? null : this;
// // };

// // Plugin["btoa"] = Plugin["encode"] = function (plain, utf8encode) {
// //     plain = Plugin["raw"] === false || Plugin["utf8encode"] || utf8encode ? UTF8.encode(plain) : plain;
// //     plain = code(plain, false, r256, b64, 8, 6);
// //     return plain + '===='.slice((plain.length % 4) || 4);
// // };

// // Plugin["atob"] = Plugin["decode"] = function (coded, utf8decode) {
// //     coded = coded.replace(/[^A-Za-z0-9\+\/\=]/g, "");
// //     coded = String(coded).split('=');
// //     var i = coded.length;
// //     do {
// //         --i;
// //         coded[i] = code(coded[i], true, r64, a256, 6, 8);
// //     } while (i > 0);
// //     coded = coded.join('');
// //     return Plugin["raw"] === false || Plugin["utf8decode"] || utf8decode ? UTF8.decode(coded) : coded;
// // };
// //base64编码
// export function btoa_base64Encode(plain, utf8encode = false) {
//     if (utf8encode) plain = UTF8.encode(plain);
//     plain = code(plain, false, r256, b64, 8, 6);
//     return plain + '===='.slice((plain.length % 4) || 4);
// };
// //base64解码
// export function atob_base64Decode(coded, utf8decode = false) {
//     coded = coded.replace(/[^A-Za-z0-9\+\/\=]/g, "");
//     coded = String(coded).split('=');
//     var i = coded.length;
//     do {
//         --i;
//         coded[i] = code(coded[i], true, r64, a256, 6, 8);
//     } while (i > 0);
//     coded = coded.join('');
//     return utf8decode ? UTF8.decode(coded) : coded;
// };

