function isAlghoPage() {
    let alghoPagesUrl = [
        "/banca-on-line/privati/conti/conto-start",
        "/banca-on-line/privati/conti/conto-premium"
    ];

    var algho_pages_check = false;
    alghoPagesUrl.forEach(function(p) {
        if(window.location.href.indexOf(p) != -1)
            algho_pages_check = true
    })

    return algho_pages_check
}

$(function() {
    
    


    if(isAlghoPage() && $("#chat-algho").length==0) {


        let algho_script_src = "https://virtualassistant.sella.tst/algho-viewer.min.js",
            algho_script_bot_id = "eb523431f6d13b550736ee4caf36eb69",
            algho_script_question_id = "308572";

        if(window.location.host.indexOf("sella.it") != -1) {
            algho_script_src = "https://virtualassistant.sella.it/algho-viewer.min.js",
            algho_script_bot_id = "92209468ad3a89c70897abc8af3a02c7",
            algho_script_question_id = "275512";

        } 
       

        //$("#chat").html('<algho-viewer allow-form-input="false" allow-input="true" bot-icon="true" bot-id="d3038311321f3974e4d423b63ff68e6e" disable-select="true" font="manrope" font-family="Manrope" height="100%" hide-menu="true" inline="true" input-placeholder="Scrivi qui un messaggio" link-auto-open="false" link-preview="true" link-text="VAI ALLA PAGINA" open="false" page-title="false" show-all-suggestion="true" sound="false" theme-style="light" voice="false" widget="false" width="100%"> </algho-viewer>');
        
        
        const algho_viewer_tag = document.createElement("algho-viewer"); 
        const algho_script_tag = document.createElement("script"); 
        const algho_chat_tag = document.createElement("div");
        const algho_widget_tag = document.createElement("div");

        //chat div
        algho_chat_tag.setAttribute("id","chat-algho");
        algho_chat_tag.setAttribute("style","display: none;");
        document.body.appendChild(algho_chat_tag);
        const chat = document.getElementById("chat-algho");
        
        //widget div
        algho_widget_tag.setAttribute("id","widget-algho");
        document.body.appendChild(algho_widget_tag);
        const widget = document.getElementById("widget-algho");

        //algho viewer tag
        algho_viewer_tag.setAttribute("id", "algho-viewer");
        algho_viewer_tag.setAttribute("bot-id", algho_script_bot_id);

        algho_viewer_tag.setAttribute("question-id", algho_script_question_id);
        algho_viewer_tag.setAttribute("question-repeat", "true");
        algho_viewer_tag.setAttribute("bot-icon", "true");
        algho_viewer_tag.setAttribute("disable-select", "true");
        algho_viewer_tag.setAttribute("page-title", "false");
        algho_viewer_tag.setAttribute("font", "manrope");
        algho_viewer_tag.setAttribute("font-family", "Manrope");
        algho_viewer_tag.setAttribute("voice", "false");
        algho_viewer_tag.setAttribute("widget", "false");
        algho_viewer_tag.setAttribute("sound", "false");
        algho_viewer_tag.setAttribute("theme-style", "light");
        algho_viewer_tag.setAttribute("show-all-suggestion", "true");
        algho_viewer_tag.setAttribute("link-preview", "true");
        algho_viewer_tag.setAttribute("link-auto-open", "false");
        algho_viewer_tag.setAttribute("link-text", "VAI ALLA PAGINA");
        algho_viewer_tag.setAttribute("hide-menu", "true");
        algho_viewer_tag.setAttribute("inline", "true");
        algho_viewer_tag.setAttribute("height", "100%");
        algho_viewer_tag.setAttribute("width", "100%");
        algho_viewer_tag.setAttribute("input-placeholder", "Scrivi qui un messaggio");
        algho_viewer_tag.setAttribute("open", "false");
        algho_viewer_tag.setAttribute("allow-input", "true");
        algho_viewer_tag.setAttribute("allow-form-input", "false");
        algho_viewer_tag.setAttribute("force-show-close", "true");
        algho_viewer_tag.setAttribute("widget-border-radius", "8px");
        //algho_viewer_tag.setAttribute("theme-style", "Light");
        //algho_viewer_tag.setAttribute("theme", '{"primary-color":"#31b3f4","primary-color-text":"#31b3f4","suggested-color":"#31b3f44d","fg-color":"#FFFFFF","fg-color-widget":"#FFFFFF","bg-color-widget":"#31b3f4","fg-color-bot":"#FFFFFF","fg-color-user":"#FFFFFF","bg-color-user":"#8181811D","bg-color-bot":"#8181811D","bg-color":"#40444d","bg-color-gradient":"#292b31","bg-color-overlay":"#00000000","bg-color-card":"#8181811D","fg-other-color":"#FFFFFF","bg-other-color":"#8181811D","border-other-color":"#FFFFFF50","fg-color-input":"#FFFFFF","border-color-input":"#FFFFFF50","border-color":"#6e7585","caption-color":"#74767c","timestamp-color-user":"#FFFFFF","timestamp-color-bot":"#FFFFFF","fg-button-color":"#FFFFFF","fg-button-color-active":"#31b3f4","fg-button-color-deactive":"#d93025","fg-button-color-hover":"#31b3f4","fg-button-color-disabled":"#70798f","bg-button-color":"#8181811D","bg-button-color-hover":"#8181811D","bg-keyboard-color":"#FFFFFF1D","bg-keyboard-other-color":"#FFFFFF30","fg-button-color-transparent":"#FFFFFF","bg-button-color-transparent":"#8181811D","fg-tooltip-color":"#FFFFFF","bg-tooltip-color":"#353840","scroll-color":"#FFFFFF","border-radius-frame":24,"border-radius":16,"border-radius-small":4,"backdrop-filter":"blur(16px)"}');
        
        //delay algho injecton
        setTimeout(function() {
            $("body").addClass("has-algho");
            //$(widget).addClass("active");
            chat.appendChild(algho_viewer_tag);
            //inserimento css custom
            setTimeout(function() {
                $(document.getElementById("algho-viewer").shadowRoot).append("<link href='/documents/d/guest/algho-inner' rel='stylesheet' />");
            }, 3000);
        }, 500);
        

        //algho script tag
        algho_script_tag.setAttribute("src", algho_script_src);
        algho_script_tag.setAttribute("defer", "");
        document.body.appendChild(algho_script_tag);

        //ga tracking function
        var DLAlghoTrack = function(action) {

            var data_layer_to_push = {}

            if(typeof(dataLayerUtility) != "undefined")
                data_layer_to_push = dataLayerUtility.getDataLayer()[0]

            data_layer_to_push["event"] = "elementClicked";
            data_layer_to_push["elementCategory"] = "button";
            data_layer_to_push["elementAction"] = "interazione algho";
            data_layer_to_push["elementLabel"] = action;

            //console.log(data_layer_to_push);
            dataLayer.push(data_layer_to_push);

        }
        
       
        //event
        widget.addEventListener("click", (event) => {
            if (chat.style.display == "none") {
                algho.open();
                chat.style.display = "block";

                $([document.documentElement, document.body]).animate({
                    scrollTop: $(".disclaimer").eq(0).offset().top - $(window).outerHeight() +100
                }, 1500);

                DLAlghoTrack("apertura");

            } else {
                chat.style.display = "none";
                algho.close();
                DLAlghoTrack("chiusura");
            }
        });




        $("body").on("click", ".acsb-trigger-icon", function(evt) {

            
        })







        algho_viewer_tag.addEventListener("alghoHide", function() {
            chat.style.display = "none";
        }, false);



        

        let liferay_spa_app_interval = setInterval(function(){
            //check se SPA app è già disponibile
            if(Liferay.SPA == undefined)
                return;

            clearInterval(liferay_spa_app_interval);

            Liferay.SPA.app.on('endNavigate', function() {

                if(isAlghoPage())
                    $("body").addClass("has-algho")
                else
                    $("body").removeClass("has-algho")
                
            });

        }, 3000);

        //fallback timeout dopo 20 secondi
        setTimeout(function() {
            clearInterval(liferay_spa_app_interval);
        }, 20000)



    }
    

    
});



