{% load i18n gdpr_solution %}
{% get_current_language as LANGUAGE_CODE %}


{% for script in cookie_scripts %}
    {% if cookie_settings.banner_is_active %}
        {% if script.cookie_type == 1 %}
            {{script.cookie_script|regex_script:"analytics"|safe}}
        {% elif script.cookie_type == 2 %}
            {{script.cookie_script|regex_script:"targeting"|safe}}
        {% endif %}
    {% endif %}

    {% if script.cookie_type == 0 %}
        {{script.cookie_script|safe}}
    {% endif %}
{% endfor %}


<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@v2.8.0/dist/cookieconsent.css">
<script defer src="https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@v2.8.0/dist/cookieconsent.js"></script>

{% if not cookie_settings.banner_is_active %}
<script defer>
    window.addEventListener('load', function() {
        var buttonSettingsModal = document.querySelectorAll('[data-cc="c-settings"]')
        buttonSettingsModal.forEach(function(btn){
            btn.style.display = 'none';
        })
    });
</script>
{% endif %}

<script defer>
    window.addEventListener('load', function () {
        function fetchCreateCookiePost(url, csrf_token, body, cc) {
            var dataResponse = null

            fetch(url, {
                method: 'POST',
                credentials: 'same-origin',
                headers: {
                    'Accept': 'application/json',
                    'X-Requested-With': 'XMLHttpRequest',
                    'X-CSRFToken': csrf_token,
                },
                body: body
            }).then(response => {
                return response.json();
            }).then(data => {
                cc.set('data', {
                    value: {
                        token: data['consent_token']
                    }
                });
            })
        }

        // obtain plugin
        var cc = initCookieConsent();

        // run plugin with your configuration
        cc.run({
            current_lang: '{{LANGUAGE_CODE}}',
            autoclear_cookies: true,
            page_scripts: true,
            cookie_name: 'cookieConsent',
            cookie_path: '{{request.path}}',
            revision: {{cookie_settings.revision_banner|default:"0"}},

            gui_options: {
                consent_modal: {
                    layout: '{{cookie_settings.layout_banner}}',
                    position: '{{cookie_settings.position_y_banner}} {{cookie_settings.position_x_banner}}',
                    transition: 'slide',
                    swap_buttons: false
                },
                settings_modal: {
                    layout: '{{cookie_settings.layout_modal}}',
                    position: '{{cookie_settings.position_modal}}',
                    transition: 'slide'
                }
            },

            onFirstAction: function (user_preferences, cookie) {
                var dataform = new FormData();
                dataform.append('user_preferences', cookie['level']);
                dataform.append('request_url', window.location.href);

                fetchCreateCookiePost('{% url "create_cookie_view" %}', '{{csrf_token}}', dataform, cc)
            },

            onChange: function (cookie, changed_categories) {
                var dataform = new FormData();
                dataform.append('user_preferences', cookie['level']);
                dataform.append('request_url', window.location.href);

                fetchCreateCookiePost('{% url "create_cookie_view" %}', '{{csrf_token}}', dataform, cc)
            },

            languages: {
                '{{LANGUAGE_CODE}}': {
                    consent_modal: {
                        title: '{{cookie_settings.title_banner|default:"Questo sito web utilizza i cookie"}}',
                        description: '{{cookie_settings.description_banner|safe|default:"Ciao, questo sito web utilizza cookie necessari per garantire il suo corretto funzionamento."}} {% if cookie_settings.banner_is_active %}<button type="button" data-cc="c-settings" class="cc-link">{% trans "Modifica preferenze" %}</button>{% endif %}',
                        revision_message: '',
                        primary_btn: {
                            {% if cookie_settings.banner_is_active %}
                            text: '{% trans "Accetta tutti" %}',
                            {% else %}
                            text: '{% trans "Ok, ho capito" %}',
                            {% endif %}
                            role: 'accept_all'
                        },
                        {% if cookie_settings.banner_is_active %}
                        secondary_btn: {
                            text: '{% trans "Rifiuta" %}',
                            role: 'accept_necessary'
                        }
                        {% endif %}
                    },
                    settings_modal: {
                        title: '{% trans "Preferenze cookie" %}',
                        save_settings_btn: '{% trans "Salva preferenze" %}',
                        accept_all_btn: '{% trans "Accetta tutti" %}',
                        reject_all_btn: '{% trans "Rifiuta" %}',
                        close_btn_label: 'Close',
                        blocks: [
                            {
                                title: '{{cookie_settings.title_modal|default:"Utilizzo dei cookie"}}',
                                description: '{{cookie_settings.description_modal|safe|default:"Usiamo i cookie per garantire le funzionalità di base del sito web e per migliorare la tua esperienza online. Puoi scegliere per ogni categoria cosa attivare e disattivare quando vuoi. Per ulteriori dettagli relativi ai cookie e ad altri dati sensibili, si prega di leggere la Privacy Policy e la Cookie Policy."}}'
                            },
                            {
                                title: '{% trans "Tecnici e necessari" %}',
                                description: '{{cookie_settings.description_technical|safe}}',
                                toggle: {
                                    value: 'necessary',
                                    enabled: true,
                                    readonly: true
                                }
                            },
                            {% if cookie_settings.banner_is_active %}
                            {% if cookie_settings.description_analytics %}
                            {
                                title: '{% trans "Performance e analisi" %}',
                                description: '{{cookie_settings.description_analytics|safe}}',
                                toggle: {
                                    value: 'analytics',
                                    enabled: false,
                                    readonly: false
                                },
                            },
                            {% endif %}
                            {% if cookie_settings.description_marketing %}
                            {
                                title: '{% trans "Pubblicitari e di marketing" %}',
                                description: '{{cookie_settings.description_marketing|safe}}',
                                toggle: {
                                    value: 'targeting',
                                    enabled: false,
                                    readonly: false
                                }
                            },
                            {% endif %}
                            {% endif %}
                            {% if cookie_settings.description_information %}
                            {
                                title: '{% trans "Maggiori informazioni" %}',
                                description: '{{cookie_settings.description_information|safe}}'
                            }
                            {% endif %}
                        ]
                    }
                }
            }
        });
    });
</script>
