Route.waitView().then(function () {

    $('.modal').modal();

    const elementProperty = new ElementProperty();

    const driverController = new DriverController();

    const connection = new NetworkConnection();

    const KEY_ENTER = 13;

    const push = new Push();

    elementProperty.getElement(".title-menu", element => {
        element.innerHTML = 'Login'
    });

    elementProperty.addEventInElement("#authenticateUser", "onclick", authenticate);

    elementProperty.addEventInElement(".back-to", "onclick", function () {
        Route.redirect("Main");
    });

    window.addEventListener('keyboardDidShow', function () {
        elementProperty.getElement("#authenticateUser", element => {
            element.classList.add("active");
        });
    });

    window.addEventListener('keyboardDidHide', function () {
        elementProperty.getElement("#authenticateUser", element => {
            element.classList.remove("active");
        });
    });

    elementProperty.addEventInElement(".custom", "onkeypress", function (event) {
        if (event.keyCode === KEY_ENTER) {
            const inputs = elementProperty.getElements(".custom")

            for (let i in inputs) {
                const input = inputs[i];

                if (input.value.length === 0) {
                    input.focus();
                    return;
                }
            }
            authenticate();
        }
    });

    if(!Array.isArray(Session.get("lastAccessUser"))){
        const user = Session.get("lastAccessUser");
        document.getElementById("email").value = user.email;
        document.getElementById("password").value = user.password;
    }

    function authenticate() {

        const user = {
            email: document.getElementById("email").value,
            password: document.getElementById("password").value,
            deviceToken: push.token,
            connection: connection.existConnection
        };

        if (!ValidateForm.validateEmail(user.email)) {
            SwalCustom.messageDialog(getTextTranslate("login","message-invalid-email"), getTextTranslate("login","title-invalid-email"), () => {
            }, "warning");
            return
        }
        if (user.password.length <= 0) {
            document.getElementById("password").focus();
            return;
        }

        document.getElementById("form-login").style.display = "none";
        document.getElementById("preloadLogin").style.display = "block";

        driverController.authenticate(user).then(result => {
            document.getElementById("preloadLogin").style.display = "none";
            document.getElementById("form-login").style.display = "block";

            if (!result.status) {
                SwalCustom.messageDialog(result.message, getTextTranslate("login","title-error-auth"), () => {
                }, "info");
                return;
            }

            Session.set("lastAccessUser",user);

            Session.set('user', result.data);

            const permissions = cordova.plugins.permissions;

            new Promise((resolve, reject) => {
                permissions.requestPermission(permissions.RECORD_AUDIO, 
                    () => {
                        permissions.requestPermission(
                            permissions.WRITE_EXTERNAL_STORAGE,()=>{
                                resolve();
                            },
                            () => {
                                resolve();
                            }
                        );
                    },
                    () => {
                        resolve();
                    }
                );
            }).then(
                () => Route.redirect("Driver")
            );


        }).catch(error => {
            document.getElementById("form-login").style.display = "block";
            document.getElementById("preloadLogin").style.display = "none";
            SwalCustom.messageDialog(getTextTranslate("login","message-error-auth"), getTextTranslate("login","title-error-auth"));
        })
    }

    connection.observerConnection();

    Route.pageDynamic();

});

function preload(status) {
    if (status) {
        document.getElementById("preloadLogin").style.display = "block";
        return;
    }
    document.getElementById("preloadLogin").style.display = "none";
}

function forgetPassword() {
    Route.redirectDynamic("Main","ForgetPassword");
}
