var newCodeTimerInSec = 60;
var counter = setInterval(timer, 1000); // 1 second

function timer() {
    newCodeTimerInSec = newCodeTimerInSec - 1;
    if (newCodeTimerInSec <= 0) {
        clearInterval(counter);
        $('#timer').hide();
        $('#GetNewCodeLink').removeClass('get-new-code-disabled');
        return;
    }

    $('#GetNewCodeLink').addClass('get-new-code-disabled');
    document.getElementById("timer").innerHTML = "(" + newCodeTimerInSec + ")";
}

function handleGetNewCodeClick() {
    if (isGetNewCodeEnabled() == false) {
        return false;
    }

    openModalSpinner();
    return true;
}

function isGetNewCodeEnabled() {
    if ($('#GetNewCodeLink').hasClass('get-new-code-disabled')) {
        return false;
    }
}

function validateOneTimeCode() {
    const inputs = document.querySelectorAll(".otp-field > input");

    if (!$('#lastOtpField').is('[disabled=disabled]') && $('#lastOtpField').val() != "") {

        let oneTimeCodeUser = "";
        for (let inputIndex = 0; inputIndex < inputs.length; inputIndex++) {
            oneTimeCodeUser = oneTimeCodeUser + inputs[inputIndex].value;
        }

        if ($('#HdnOneTimeCodeUser').val() != oneTimeCodeUser) {
            $('#HdnOneTimeCodeUser').val(oneTimeCodeUser);
            $('#HdnSubmitBtn').click();
        }
    }
}


jQuery(document).ready(function () {
    closeModalSpinner();

    let otp1 = document.getElementById('otp-1');
    let otpInputs = document.querySelectorAll('input[type="number"]');

    const arrowLeftKey = 37;
    const tabKey = 9;
    const shiftKey = 16;
    const metaKey = 224;
    const altKey = 18;
    const controlKey = 17;
    const backspaceKey = 8;

    otpInputs.forEach(function (input) {
        input.addEventListener('keyup', function (e) {

            if (e.keyCode === shiftKey || e.keyCode == tabKey || e.keyCode == metaKey || e.keyCode == altKey || e.keyCode == controlKey) {
                return;
            }

            if ((e.keyCode === backspaceKey || e.keyCode === arrowLeftKey) && this.previousElementSibling && this.previousElementSibling.tagName === "INPUT") {
                this.previousElementSibling.select();
            } else if (e.keyCode !== backspaceKey && this.nextElementSibling) {
                this.nextElementSibling.select();
            }
        });

        input.addEventListener('focus', function (e) {
            if (this === otp1) {
                return;
            }

            if (otp1.value == '') {
                otp1.focus();
            }

            if (this.previousElementSibling.value == '') {
                this.previousElementSibling.focus();
            }
        });
    });

    // handle copy/paste
    otp1.addEventListener('input', function (e) {

        let data = e.data || this.value;

        if (!data || data.length === 1) {
            return;
        }
        
        for (i = 0; i < data.length; i++) {
            otpInputs[i].value = data[i];

            const isLast = i == otpInputs.length - 1;
            if (isLast) {
                validateOneTimeCode();
            }
        }
    });

    $('#otp-6').on('input', function (e) {
        validateOneTimeCode();
    });
});