// Android.forceLinkLaunch(false);
var params = [];

var wholeBinaryValue;
var decrypted;
var prf;
var crp;

var passthrough = "false";

// loadBackground();
decryptPath();
// loadParametersFromUrl();
// Android.showToast(Android.loadKey1(""));

Android.onData(passthrough);

function pad(n, width, z) {
    z = z || '0';
    n = n + '';
    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}

/*function loadBackground(){
    document.getElementById("bg").innerHTML = "<img src ='img/black_background.jpg'>";
}*/

//a terme le générer à partir d'un fichier
function checkDom(url){
    if (url != "jt.ag" && url != "xt.ag" && url != "zt.ag" && url != "ft.ag") {
        return false;
    }
    else{
        return true;
    };
}

function decryptPath() {
    var secretPassPhrase256 = "i'm a ninja";
    var url = Android.loadUrl(); //recupéré dans le code Android et injecté ici
    if (Android.isQR()) {
        if (Android.getQrResult()) {
            document.getElementById("img").innerHTML = "<img src ='svg/ic_42.svg'></br><p>"+Android.getQrSafeText()+"</p>"; 
            passthrough = "true"; 
        }else{
            document.getElementById("img").innerHTML = "<img src ='svg/ic_32.svg'></br><p>"+Android.getQrUnsafeText()+"</p>";
            passthrough = "false";
        };
        var forcetest = "Android.forceLinkLaunch(true)";
        document.getElementById("url").innerHTML = "<a href='javascript:"+forcetest+"'>"+url+ "</a>";
        return;
    };

    console.log("url :" + url);
    var dom = Android.loadDom(); //on prend le début 
    console.log("dom :" + Android.loadDom());
    console.log(checkDom(dom));
    if (checkDom(dom)) {
        var tab = url.split("/");
        var path64u = tab[tab.length-1];
        // var path64u = url.substring(7, url.length); //on enlève jt.ag/ pour avoir le path
        console.log("path64u: " + path64u);
        prf = path64u.substring(0, 2);
        if (prf != "u1" && prf != "u2" && prf != "u3" && prf != "u4" && prf!= "u5" ) {
            var undefinedDom = document.getElementById("img").innerHTML = "<img src ='svg/ic_12.svg'></br><p>"+Android.getCheckUnknownText()+"</p>";
            // document.getElementById("img").innerHTML = "<img src ='svg/ic_22.svg'></br><p>Empty Tag</p>";
            passthrough = "false";
            //c'est sale gros.
        }
        else {
            // decodeUid(path64u);
            var newBase;
            if (Android.loadAlg_crp() == "CryptoJS.AES") {
                newBase = changeBaseAlpha(path64u.substring(2), Android.loadBas_out(), "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
                console.log(newBase);
                drawFromUid(newBase);
            }else{
                drawFromUid(path64u.substring(2));
            };

            // drawFromUid(path64u);
       
        }
    }else{
        if (url == "empty") {
            document.getElementById("img").innerHTML = "<img src ='svg/ic_22.svg'></br><p>"+Android.getCheckEmptyText()+"</p>";
            // document.getElementById("img").innerHTML = "<img src ='svg/ic_22.svg'></br><p>ハンバーガー</p>"; Hanbaga
        }
        else{
            document.getElementById("img").innerHTML = "<img src ='svg/ic_12.svg'></br><p>"+Android.getCheckUnknownText()+"</p>";
        };
        // var undefinedDom = document.getElementById("img").innerHTML = "<img src ='svg/ic_22.svg'></br><p>Empty Tag</p>";
        passthrough = "false";
    };


    function decodeAES() {
        var key = CryptoJS.enc.Hex.parse(Android.loadKeyCrp()); //a terme faire varier la cle en fonction du loadParametersFromUrl
        var iv = CryptoJS.enc.Hex.parse(Android.loadInvCrp()); //meme chose pour le iv
        console.log("message chiffré :" + crp);
        var decrypt = CryptoJS.AES.decrypt(crp, key, {iv: iv});
        // console.log(decrypt);
        // console.log(decrypt.toString(CryptoJS.enc.Utf8));
        crp = decrypt.toString(CryptoJS.enc.Utf8); //decrypt par defaut en hexa, CryptoJS.enc.Utf8 pour le rendre lisible
        console.log("message déchiffré :" + crp);
    }

    function drawFromUid(path) {
        // crp = path.substring(2, path.length); //on retire les 2 caractères du prf
        crp = path;
        crp = checkUid(path); 
        console.log("crp " + crp);

    }

    //changer le nom uid64....
    function checkUid(uid64) {
        var check = Android.loadUid();
        if (Android.loadAlg_crp() == "CryptoJS.AES") {
            decodeAES();    
            var uidNewBase = changeBaseAlpha(check, "0123456789ABCDEF", Android.loadBas_cod());
        }else{
            console.log("je ne décrypte pas");
            var uidNewBase = changeBaseAlpha(check, "0123456789ABCDEF", Android.loadBas_out());
        };
        console.log("check :" + check);
        
        console.log("real uid in b64 "+ uidNewBase);
        console.log("to compare with what we've decrypted "+ crp);    

        if (crp != uidNewBase) {    
            // Android.showToast("OMG THIS IS A COPYY ! D:");
            var copy = document.getElementById("img").innerHTML = "<img src ='svg/ic_32.svg'></br><p>"+Android.getCheckInvalidText()+"</p>";
            passthrough = "false";
        } else {
            // Android.showToast("The check has been sucessful");
            // var success = document.getElementById("img").innerHTML = "<img src ='svg/ic_42.svg'></br><p>Valid Tag</p>";
            var success = document.getElementById("img").innerHTML = "<img src ='svg/ic_42.svg'></br><p>"+Android.getCheckValidText()+"</p>"; 
            passthrough = "true";       
        }
        var forcetest = "Android.forceLinkLaunch(true)";
        var blabla = "this is a test..";
        document.getElementById("url").innerHTML = "<a href='javascript:"+forcetest+"'>"+"http://"+url.substring(1,url.length)+ "</a>";
        return check;
    }
}