var CryptoJS = require('./js/crypto-js.js');

function getNewKey(as){
     /**
     * encryptedStr(SHA3-256) ---> f25f1614appsapi2
     * encryptedValue ---> 49d3a9685870cc30f63330b8136c7adfdb8859c6b538308992a1c9a456db2e59
     *
     * encryptedValue(MD5) ---> 5e4ebc8cappsapi2
     * encryptedValue ---> c30b8b5289e46489598de382a658cc7f
    */
    var encryptedStr = as + "appsapi2";
    var r = as.substr(as.length - 1, 1);
    switch (true) {
        case ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'a', 'b', 'c', 'd', 'e', 'f', 'g'].includes(r):
            encryptedValue = CryptoJS.MD5(encryptedStr).toString();
            break;
        case ['H', 'I', 'J', 'K', 'L', 'M', 'N', 'h', 'i', 'j', 'k', 'l', 'm', 'n'].includes(r):
            encryptedValue = CryptoJS.SHA1(encryptedStr).toString(CryptoJS.enc.Hex);
            break;
        case ['O', 'P', 'Q', 'R', 'S', 'T', 'o', 'p', 'q', 'r', 's', 't'].includes(r):
            encryptedValue = CryptoJS.SHA256(encryptedStr).toString(CryptoJS.enc.Hex);
            break;
        case ['U', 'V', 'W', 'X', 'Y', 'Z', 'u', 'v', 'w', 'x', 'y', 'z'].includes(r):
            encryptedValue = CryptoJS.SHA512(encryptedStr).toString(CryptoJS.enc.Hex);
            break;
        case ['0', '1', '2', '3', '4'].includes(r):
            encryptedValue = CryptoJS.SHA3(encryptedStr, { outputLength: 256 }).toString(CryptoJS.enc.Hex);
            break;
        case ['5', '6', '7', '8', '9'].includes(r):
            encryptedValue = CryptoJS.SHA3(encryptedStr, { outputLength: 512 }).toString(CryptoJS.enc.Hex);
            break;
    default:
        encryptedValue = e;
    }
    var key = encryptedValue.slice(0, 16);
    return key;
}

function aesEncrypt(t, key) {
    var r = key
      , o = CryptoJS.enc.Utf8.parse(r)
      , i = CryptoJS.enc.Utf8.parse(t)
      , r = CryptoJS.AES.encrypt(i, o, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.ZeroPadding
    });
    return r.toString()
}

function getRzData(backstr, ac_c){
    var rzData = {
        "common": {
            "cl": [],
            "mv": [],
            "sc": [],
            "kb": [],
            "sb": [],
            "sd": [],
            "sm": [],
            "cr": {
                "screenTop": 0,
                "screenLeft": 0,
                "clientWidth": 1903,
                "clientHeight": 395,
                "screenWidth": 1920,
                "screenHeight": 1080,
                "availWidth": 1920,
                "availHeight": 1032,
                "outerWidth": 1920,
                "outerHeight": 1032,
                "scrollWidth": 1903,
                "scrollHeight": 1903
            },
            "simu": 0
        },
        "backstr": backstr,
        "captchalist": {
            "spin-0": {
                "mv": [
                    {
                        "t": 1691824376147,
                        "fx": 1001,
                        "fy": 295
                    },
                    {
                        "t": 1691824376435,
                        "fx": 1002,
                        "fy": 295
                    }
                ],
                "ac_c": ac_c,
                "cl": [
                    {
                        "t": 1691824376212,
                        "x": 1002,
                        "y": 295
                    }
                ],
                "p": {},
                "cr": {
                    "left": 806,
                    "top": 40,
                    "width": 290,
                    "height": 280
                },
                "back": {
                    "left": 875,
                    "top": 84,
                    "width": 152,
                    "height": 152
                }
            }
        }
    }
    return rzData;
}

function getTrackAn(an_spin_0, c_value){
  const i = Date.now();
  let n, o = 0, s = "0";
  let zeroNum = "";
  for (let i = 0; i < c_value; i++) {
    zeroNum += "0";
  }
  for (; s !== zeroNum && true; )
    s = CryptoJS.SHA1(an_spin_0 + o).toString().substring(0, c_value),
    o++;
  return n = Date.now() - i,
  {
      t: n,
      an: o - 1
  }
}

function getFs1(backstr, ac_c, as, track_p){
    var rzData = getRzData(backstr, ac_c, track_p);
    var fs_1 = aesEncrypt(JSON.stringify(rzData), getNewKey(as));
    return fs_1;
}

function getFs2(backstr, ac_c, as){
    //var track_p = getTrackAn(an_spin_0, c_value);

    var fs_params = {
        common_en: getFs1(backstr, ac_c, as),
        backstr: backstr
    };
    var fs_2 = aesEncrypt(JSON.stringify(fs_params), getNewKey(as));
    return fs_2;
}

//var backstr = "2333-kNbq8pYj9qwXDEPyB/K7+sEOCG56rn6WxPRL5MVFf9wjuFjFY+v4eiP21GmXN0/ykYmC7H0irXZOYxsTwIARXTZHjZWVymoqqVA2nNL6/s7aAbbr0WPYCn40XHTCNOBMWONN760VzD2oj8NlJ4SpJz7TOKPeEguv0uvGa4i0v4vgYTSrdNZ2+wz5cZREo6Hf+Mk9zDsy20SEeqJ/9PAwDmH25TvAMQX7Pnb5aMxfHT7GX5nwMPFHlIJHJQ2qI7AwmBRiY0Ei3PoEj8auHGxd9UULDF39Zet+o3AGquoMPrsqD1wrS+Kdn0dp4Q6oxiy10pmVc3uKe+v0M2Nd0LRhbhaukkeTL9bNR4bHz+rpuia8Ki9u02RWPm6NySco6lJ9n/s0lZGDb0WcD6+yRpsdSbwTWCAmxsDA6aaYOsPa/T5tDcqm01XWgl/oir9fhiYNJ5Ys9AW4jKCp7U2r63CvftFhg7hR2gdzYgFwtVQvaSSp5cw5b5+T22k+1nPyvszKIQXgbsdf+pxhV4oy/3cKZgCWnOV16LxSd0QXJmZzGYxpHjJaJ6HHAmh8UhspFeWG8Hwu8WPwEfk/hyMYx8xygxknoYKlHny6FejCSqcfVyLPlmmgzsm48snssFwN/OEJnGODd23gPWReFX+VY4ceQN/zSC0VvPiEEhWEl0jAQ1lGsLm7M8eSALbFxp0IcD7tXBsieG0P8oYHm2BorLAe/Q==";
//var ac_c = 0.55;
//var as = "f25f1614";
//get_fs_2(as);