function easyInstallerNext(step) {
    switch (step){
        case 2:
            $('#easy-installer-step').attr('data-step',2);
            $('#section-brand').fadeOut(500).hide();
            $('#section-icon').fadeIn(500).show();
            $('#sv-brand').removeClass("esv-current").addClass("esv-past");
            $('#sv-brand').find('.esv-ic').removeClass("ph-circle").addClass("ph-fill ph-check-circle");
            $('#sv-icon').addClass("esv-current");
            $('#sv-icon').find('.esv-ic').addClass("ph-fill");
            $('#skip-ezi-step').show();
            $('#skip-ezi-step').attr('data-skip-step','skip-icon');
            break;
        case 3:
            $('#easy-installer-step').attr('data-step',3);
            $('#section-icon').fadeOut(500).hide();
            $('#section-logo').fadeIn(500).show();
            $('#sv-icon').removeClass("esv-current").addClass("esv-past");
            $('#sv-icon').find('.esv-ic').removeClass("ph-circle").addClass("ph-fill ph-check-circle");
            $('#sv-logo').addClass("esv-current");
            $('#sv-logo').find('.esv-ic').addClass("ph-fill");
            $('#skip-ezi-step').attr('data-skip-step','skip-logo');
            break;
        case 4:
            $('#easy-installer-step').attr('data-step',4);
            $('#section-logo').fadeOut(500).hide();
            $('#section-demo').fadeIn(500).show();
            $('.ezi-body').css('background-color','#ccd3d782');
            $('#sv-color').removeClass("esv-current").addClass("esv-past");
            $('#sv-color').find('.esv-ic').removeClass("ph-circle").addClass("ph-fill ph-check-circle");
            $('#sv-demo').addClass("esv-current");
            $('#sv-demo').find('.esv-ic').addClass("ph-fill");
            $('#easy-installer-next').find('.ein-text').text("تایید نهایی و راه‌اندازی");
            $('.ezi-header').hide();
            $('#skip-ezi-step').hide();
            break;
        case 5:
            $('#easy-installer-step').attr('data-step',5);
            $('#section-demo').fadeOut(500).hide();
            $('.ezi-body').css('background-color','#ffffff00');
            $('#section-end').fadeIn(500).show();
            $('#sv-demo').removeClass("esv-current").addClass("esv-past");
            $('#sv-demo').find('.esv-ic').removeClass("ph-circle").addClass("ph-fill ph-check-circle");
            $('.ezi-footer').hide();
            break;
    }
}

function easyInstallerPrev(step) {
    let stepInt = parseInt(step);
    switch (stepInt){
        case 2:
            $('#easy-installer-step').attr('data-step',1);
            $('#section-brand').fadeIn(500).show();
            $('#section-icon').fadeOut(500).hide();
            $('#sv-brand').removeClass("esv-past").addClass("esv-current");
            $('#sv-icon').removeClass("esv-current");
            $('#sv-icon').find('.esv-ic').removeClass("ph-fill");
            $('#sv-brand').find('.esv-ic').removeClass("ph-check-circle").addClass("ph-circle");
            $('#skip-ezi-step').hide();
            break;
        case 3:
            $('#easy-installer-step').attr('data-step',2);
            $('#section-icon').fadeIn(500).show();
            $('#section-logo').fadeOut(500).hide();
            $('#sv-icon').removeClass("esv-past").addClass("esv-current");
            $('#sv-logo').removeClass("esv-current");
            $('#sv-logo').find('.esv-ic').removeClass("ph-fill");
            $('#sv-icon').find('.esv-ic').removeClass("ph-check-circle").addClass("ph-circle");
            $('#skip-ezi-step').attr('data-skip-step','skip-icon');
            break;
        case 4:
            $('#easy-installer-step').attr('data-step',3);
            $('#section-logo').fadeIn(500).show();
            $('#section-demo').fadeOut(500).hide();
            $('.ezi-body').css('background-color','#ffffff00');
            $('#sv-color').removeClass("esv-past").addClass("esv-current");
            $('#sv-demo').removeClass("esv-current");
            $('#sv-demo').find('.esv-ic').removeClass("ph-fill");
            $('#sv-color').find('.esv-ic').removeClass("ph-check-circle").addClass("ph-circle");
            $('#easy-installer-next').find('.ein-text').text("تایید و مرحله بعد");
            $('#skip-ezi-step').attr('data-skip-step','skip-logo');
            $('.ezi-header').show();
            $('#skip-ezi-step').show();
            break;

    }
}

function doTheDemoInstallRequest(param) {

    $('#install-template-loading-overlay').show();
    $.ajax({
        type: "POST",
        url: mpPanelBaseUrl + "/install_template",
        data: {
            main_brand: param['main_brand'],
            main_color: param['main_color'],
            main_icon: param['main_icon'],
            main_logo: param['main_logo'],
            template: param['demo_code'],
            mode: param['mode']
        },
        cache: false,
        success: function (data) {
            
            let success = false;
            if (param['mode'] == "normal" && data == "ok") success = true;
            if (param['mode'] == "force" && data == "okok") success = true; //two ok becaue it do factory reset and install together

            if (success) {

                //sometime we forgot to update templates after adding some configs or texts to appconfig table so
                //we do the dashbaord_task with ignoring data only for appconfig and apptext and webconfig check and update
                //UPDATE 2024: we dont need do the dashboard ajax here,
                //in newer version we do it after closing installer dialog
                $('#install-template-loading-overlay').hide();
                easyInstallerNext(5);
                
            } else {
                notyf.error(data);
                $('#install-template-loading-overlay').hide();
            }

        }
    });

}

function showConfirmDialogAndResetDemo (param) {

    $('#dialog-holder').append(dialogHtmlGenerator("small","switch_demo_dialog"));

    let dialog_id = "#switch_demo_dialog";
    let contentBox = $(dialog_id + " .majorisbox-final-content");
    let closeBtn = $(dialog_id + " .majorisbox-close");
    let closeLayout = $(dialog_id+" .majorisbox-close-layout");
    let loadingOverlay = $(dialog_id).find('.majorisbox-loading');
    contentBox.hide();

    $(dialog_id).fadeIn('fast');
    $("html").css("overflow", "hidden");
    
    closeBtn.add(closeLayout).click(function () {
        contentBox.html("");
        $(dialog_id).fadeOut('fast');
        $('#dialog-holder').find(dialog_id).remove();
        $("html").css("overflow", "auto");
    });

    loadingOverlay.show();
    contentBox.html("");

    //confirm reset and swtich demo dialog
    setTimeout(function() {
        $.ajax({
            type: "POST",
            url: mpPanelBaseUrl + "/dialog-html-loader/confirm_reset_and_switch_demo",
            cache: false,
            timeout: 25000,
            success: function (data) {
                contentBox.html(data);
                contentBox.show();
                
                loadingOverlay.hide();

                $('#switch-demo-do').click(function(){
                    if($('#sd-check').is(":checked")){ 
                        contentBox.html("");
                        $(dialog_id).fadeOut('fast');
                        $("html").css("overflow", "auto");
                        doTheDemoInstallRequest(param);
                    }else{
                        notyf.error("تیک گزینه مطالعه کردم و مطمئن هستم را بزنید");
                    }
                });

                $("#sd-check").change(function() {
                    if(this.checked) {
                        $('#switch-demo-do').removeClass('disabled');
                    }else{
                        $('#switch-demo-do').addClass('disabled');
                    }
                });

                $('#sd-cancel').click(function(){
                    contentBox.html("");
                    $(dialog_id).fadeOut('fast');
                    $('#dialog-holder').find(dialog_id).remove();
                    $("html").css("overflow", "auto");
                });
                
            },
            error: function (xhr, status, error) {
                notyf.error("error");
                $(dialog_id).fadeOut('fast');
                $('#dialog-holder').find(dialog_id).remove();
                $("html").css("overflow", "auto");
            }
        });
    }, 200);

}

function showPreviewDemoDialog(code) {

    $('#dialog-holder').append(dialogHtmlGenerator("medium","preview_demo_dialog"));

    let dialog_id = "#preview_demo_dialog";
    let contentBox = $(dialog_id + " .majorisbox-final-content");
    let closeBtn = $(dialog_id + " .majorisbox-close");
    let closeLayout = $(dialog_id+" .majorisbox-close-layout");
    let loadingOverlay = $(dialog_id).find('.majorisbox-loading');
    contentBox.hide();

    $(dialog_id).fadeIn('fast');
    $("html").css("overflow", "hidden");
    
    closeBtn.add(closeLayout).click(function () {
        contentBox.html("");
        $(dialog_id).fadeOut('fast');
        $('#dialog-holder').find(dialog_id).remove();
        $("html").css("overflow", "auto");
    });

    loadingOverlay.show();
    contentBox.html("");

    //confirm reset and swtich demo dialog
    setTimeout(function() {
        $.ajax({
            type: "POST",
            url: mpPanelBaseUrl + "/dialog-html-loader/preview_demo_dialog?code="+code,
            cache: false,
            timeout: 25000,
            success: function (data) {
                contentBox.html(data);
                contentBox.show();
                loadingOverlay.hide();
                $('#pd-done').click(function(){
                    contentBox.html("");
                    $(dialog_id).fadeOut('fast');
                    $('#dialog-holder').find(dialog_id).remove();
                    $("html").css("overflow", "auto");
                });
                
            },
            error: function (xhr, status, error) {
                notyf.error("error");
                $(dialog_id).fadeOut('fast');
                $('#dialog-holder').find(dialog_id).remove();
                $("html").css("overflow", "auto");
            }
        });
    }, 200);

}

$(function () { // start of document ready:


    let mode = $('#easy-setup-mode').attr('data-mode');
    if(mode == "force"){
        easyInstallerNext(4);
    }

    $('#easy-installer-next').click(function(){
        let current_step = $('#easy-installer-step').attr('data-step');
        if(current_step == 1){

            let main_brand = $('#main_brand').val().trim();
            if(main_brand == ""){
                notyf.open({type: 'info',message: "لطفا نام برند اپ خود را وارد نمایید"});
                return;
            }

            easyInstallerNext(2);
        
        }else if(current_step == 2){

            let main_icon = $('#main_icon').val().trim();
            if(main_icon == ""){
                notyf.open({type: 'info',message: "لطفا آیکون برنامه خود را انتخاب کرده یا از گزینه رد شدن از این مرحله استفاده کنید"});
                return;
            }

            easyInstallerNext(3);

        }else if(current_step == 3){

            let main_logo = $('#main_logo').val().trim();
            if(main_logo == ""){
                notyf.open({type: 'info',message: "لطفا لوگوی برند خود را انتخاب کرده یا از گزینه رد شدن از این مرحله استفاده کنید"});
                return;
            }

            easyInstallerNext(4);

        }else if(current_step == 4){

            //install demo:
            let main_brand = $('#main_brand').val().trim();
            let main_logo = $('#main_logo').val().trim();
            let main_icon = $('#main_icon').val().trim();
            let main_color = "";
            var demo_code = $("#prebuilt-demos").attr('data-democode');
            var is_demo_published = $("#prebuilt-demos").attr('data-demopublish');

            if(demo_code == ""){
                notyf.open({
                    type: 'info',
                    message: "لطفا ابتدا یک دمو پیش‌ساخته را انتخاب کنید"
                });
                return;
            }
    
            if(is_demo_published != "1"){
                notyf.open({
                    type: 'info',
                    message: "این دمو درحال حاضر آماده نمی‌باشد و بزودی اضافه خواهد شد"
                });
                return;
            }

            let param = {
                "main_brand" : main_brand,
                "main_logo" : main_logo,
                "main_icon" : main_icon,
                "main_color" : main_color,
                "demo_code" : demo_code,
            };
    
            if(mode == "normal"){
                param['mode'] = "normal";
                doTheDemoInstallRequest(param);
            }else{
                param['mode'] = "force";
                showConfirmDialogAndResetDemo(param);
            }

        }

    });

    $('#easy-installer-prev').click(function(){
        let current_step = $('#easy-installer-step').attr('data-step');
        easyInstallerPrev(current_step);
    });

    $('.template-info-btn').click(function() {
        event.stopPropagation();
        let code = $(this).attr('data-code');
        showPreviewDemoDialog(code);
    });

    $('.tifb-details').click(function() {
        let code = $(this).attr('data-code');
        showPreviewDemoDialog(code);
    });

    $('.template-item').click(function() {
        let code = $(this).data('code');
        let publish = $(this).data('publish');
        $('.template-item').removeClass('template-selected');
        $(this).addClass('template-selected');
        $('#prebuilt-demos').attr("data-democode",code);
        $('#prebuilt-demos').attr("data-demopublish",publish);
        $('.template-inner-footer').css('display','none');
        $(this).find('.template-inner-footer').css('display','flex');
    });

    $('#skip-ezi-step').click(function() {
        let data_step = $(this).attr('data-skip-step');
        if(data_step == "skip-icon") easyInstallerNext(3);
        else if(data_step == "skip-logo") easyInstallerNext(4);
    });

    // $('#skip-icon').click(function() {
    //     easyInstallerNext(3);
    // });
    // $('#skip-logo').click(function() {
    //     easyInstallerNext(4);
    // });
    // $('#skip-color').click(function() {
    //     easyInstallerNext(5);
    // });


    //new btns UI click redirectors:
    $('#next-btn-step1').click(function() {
        $('#easy-installer-next').trigger('click');
    });
    $('#next-btn-step2').click(function() {
        $('#easy-installer-next').trigger('click');
    });
    $('#skip-btn-step2').click(function() {
        $('#skip-ezi-step').trigger('click');
    });
    $('#next-btn-step3').click(function() {
        $('#easy-installer-next').trigger('click');
    });
    $('#skip-btn-step3').click(function() {
        $('#skip-ezi-step').trigger('click');
    });
    $('#next-btn-step4').click(function() {
        $('#easy-installer-next').trigger('click');
    });
    $('#skip-btn-step4').click(function() {
        $('#skip-ezi-step').trigger('click');
    });
    $('.tifb-install').click(function() {
        $('#easy-installer-next').trigger('click');
    });

    $('.prev-btn-step').click(function() {
        $('#easy-installer-prev').trigger('click');
    });

}); //end of jqury doc ready