var modalDisplayed = false;
var methodToCall = {
    name: null,
    id: null
};

const overlay = $('#overlay');
const savedModal = $('#modal-confirm');
const dataLossModal = $('#modal-query');
const uuidChangeModal = $('#modal-uuidChanged')
const dataLossModalYes = dataLossModal.children().eq(2).children().eq(0);
const dataLossModalNo = dataLossModal.children().eq(2).children().eq(1);
const allModals = $('.modal');
const btnModalClose = $('.modal button');
const closeModals = function() {
    hideModal(allModals);
}
const savedModalDisplay = function() {
    
    showModal(savedModal);
    setTimeout(closeModals, 2000);
}
const dataLossModalDisplay = function(){
    showModal(dataLossModal);
}

const uuidChangeModalDisplay = function(addidionalMessage){

    switch(addidionalMessage){
        case "success":{
            
            $(".uuid-modal-body").removeClass("active");
            $("#changeUUIDSuccess").addClass("active");
            break;
        }
        case "new":{
            
            $(".uuid-modal-body").removeClass("active");
            $("#newUUID").addClass("active");
            break;
        }
        case "restore":{
            
            $(".uuid-modal-body").removeClass("active");
            $("#restoredUUID").addClass("active");
            break;
        }
        case "noChg":{
            
            $(".uuid-modal-body").removeClass("active");
            $("#noChgUUID").addClass("active");
            break;
        }
    }
    showModal(uuidChangeModal);
}

function setMethodToCall(name, id){
    methodToCall.name = name;
    methodToCall.id = id;
}

const dropChangesAndClose = function(){
    callMethodByName(methodToCall)
    hideModal(dataLossModal);
}

function showModal(jmodal){
    if(modalDisplayed) return;
    overlay.addClass('active');
    jmodal.addClass('active');
    modalDisplayed = true;
}

function hideModal(jmodal){
    if(!modalDisplayed) return;
    if ($(uuidChangeModal).hasClass('active')) window.location.reload();
    overlay.removeClass('active');
    jmodal.removeClass('active');
    modalDisplayed = false;
}

btnModalClose.click(closeModals);

overlay.click(closeModals);

dataLossModalNo.click(closeModals);
dataLossModalYes.click(dropChangesAndClose);

