$(document).ready(function () {

    setTimeout(function () {

        //effect for logo
        $("#animationLogo").removeClass("rollerLeft").addClass("zoomerOut")

    }, 2000)

    setTimeout(function () {

        //show homepage
        $("#welcomeLogo").hide();
        $("#homePage").show();

    }, 3000)
})

//For default, translator is disabled and hidden
var translateIsPresent = false;
$("#google_translate_element").hide();

//create translator
function googleTranslateElementInit() {
    new google.translate.TranslateElement({ pageLanguage: 'en' }, 'google_translate_element');
}

//when translator is used
$(".toggleTranslation").change(function () {
    if (translateIsPresent) {
        $(".translateScript").remove();
        $("#google_translate_element").hide();
        translateIsPresent = false;
        location.reload();
    } else {
        $("body").append("<script type='text/javascript' class='translateScript' src='//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit'></script>");
        $("#google_translate_element").show();
        translateIsPresent = true;
    }

})

//create qrcode reader
var html5QrcodeScanner = new Html5QrcodeScanner(
    "reader", { fps: 10, qrbox: 250 }
);

//call onscansuccess when qrcode is readed
html5QrcodeScanner.render(onScanSuccess);

//when qrcode is readed
function onScanSuccess(qrCodeMessage) {

    //get password
    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({ masterPassword: qrCodeMessage }),
        url: 'http://localhost:8080/checkQRCode',
        success: function (data) {

            if (data.error) {

                
                iziToast.warning({
                    title: 'Warning',
                    displayMode:2, // remove prevous toastr
                    message: data.error
                });
            } else {

                //hide first access button
                $("#firstAccess").hide();

                iziToast.success({
                    title: 'Success',
                    displayMode:2, // remove prevous toastr
                    message: 'QRCode correct! Loading panel control...'
                });

                setTimeout(function () {

                    //zoometOut page
                    $("#homePage").addClass("zoomerOut");
                    
                }, 2000)

                setTimeout(function () {

                    //load panel control
                    window.location.href = "http://localhost:8080/html/panelControl.html";
 
                }, 3000)
            }
        }, error: function (xhr, status, error) {

            iziToast.error({
                title: 'Error',
                message: error
            });
        }
    });
}

//if access from new user
$("#firstAccess").click(()  =>  {
    
    //delete content for security reason
    $.ajax({
        type: 'GET',
        contentType: 'application/json',
        url: 'http://localhost:8080/deleteForNewUser',
        success: function (res) {
            
            setLogin(true).then(function () {
                window.location.href = "http://localhost:8080/html/panelControl.html";
            })
        }
    });
})

//Set login status
async function setLogin(state) {
    return new Promise((resolve, reject) => {

        //check if is logged
        $.ajax({
            type: 'POST',
            data: JSON.stringify({ state: state }),
            contentType: 'application/json',
            url: 'http://localhost:8080/setLogin',
            success: function (data) {

              resolve();
            }
        });
    })
}