var crp;
var passthrough = "false";
var url;
analyzePath();
Android.onData(passthrough);

function checkDom(url){
    if (url != "jt.ag" && url != "xt.ag" && url != "zt.ag" && url != "ft.ag") {
        return false;
    }
    else{
        return true;
    };
}

function analyzePath(){
    url = Android.loadUrl(); //recupéré dans le code Android et injecté ici
    console.log("url :" + url);
    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;
    };
    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 path = tab[tab.length-1];
        console.log("path: " + path.substring(2));
        var prf = path.substring(0, 2);
        var unknown = true;
        for (var i = 0; i < Android.getPrfCount(); i++) {
            if (Android.getValidPrf(i) == prf) {
                console.log(Android.getValidPrf(i));
                unknown = false;
                break;
            };
        };

        if (unknown) {
            var undefinedDom = document.getElementById("img").innerHTML = "<img src ='svg/ic_12.svg'></br><p>"+Android.getCheckUnknownText()+"</p>";
            passthrough = "false";
        }
        else {
            var newBase;
            if (Android.loadAlg_crp() == "CryptoJS.AES") {
                console.log("il y a de la crypto, out : "+ path.substring(2));
                newBase = changeBaseAlpha(path.substring(2), Android.loadBas_out(), "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
                console.log("nouvelle base out => crp" + newBase);
                crp = newBase;
                check();
            }else{
                crp = path.substring(2);
                check();
            };
        }
    }else{
        if (url == "empty") {
            document.getElementById("img").innerHTML = "<img src ='svg/ic_22.svg'></br><p>"+Android.getCheckEmptyText()+"</p>";
        }
        else{
            document.getElementById("img").innerHTML = "<img src ='svg/ic_12.svg'></br><p>"+Android.getCheckUnknownText()+"</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});
    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 check(){
    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) {    
        var copy = document.getElementById("img").innerHTML = "<img src ='svg/ic_32.svg'></br><p>"+Android.getCheckInvalidText()+"</p>";
        passthrough = "false";
    } else {
        var success = document.getElementById("img").innerHTML = "<img src ='svg/ic_42.svg'></br><p>"+Android.getCheckValidText()+"</p>"; 
        passthrough = "true";       
    }
    var forcetest = "Android.forceLinkLaunch(true)";
//    document.getElementById("url").innerHTML = "<a href='javascript:"+forcetest+"'>"+"http://"+url.substring(1,url.length)+ "</a>";
    document.getElementById("url").innerHTML = "<a href='javascript:"+forcetest+"'>"+"http://"+url+ "</a>";
}

