{#
/**
 * Copyright © 2003-2024 The Galette Team
 *
 * This file is part of Galette (https://galette.eu).
 *
 * Galette is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Galette is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
 */
#}
{# Enable dismissable messages #}
$('.message .close')
  .on('click', function() {
    $(this)
      .closest('.message')
      .transition('fade')
    ;
  })
;

{# Apply transitions on inline messages #}
$('.message.with-transition')
  .transition('flash')
;

{# Let's see if there are success messages to show #}
{% set success = flash.getMessage('success_detected') ?? [] %}
{% set success = success|merge(flash.getMessage('success') ?? []) %}
{% if success_detected is defined and success_detected is iterable %}
    {% for entry in success_detected %}
        {% set success = success|merge([entry]) %}
    {% endfor %}
{% endif %}
{% if success is iterable and success|length > 0 %}
    {% for entry in success %}
        $('body')
          .toast({
            displayTime: 'auto',
            minDisplayTime: 5000,
            wordsPerMinute: 80,
            showProgress: 'bottom',
            closeIcon: true,
            position: 'top attached',
            message: '{{ entry|e('js') }}',
            showIcon: 'check circle outline',
            class: 'success'
          })
        ;
    {% endfor %}
{% endif %}

{# Let's see if there are info messages to show #}
{% set info = flash.getMessage('info_detected') ?? [] %}
{% set info = info|merge(flash.getMessage('info') ?? []) %}
{% if info_detected is defined and info_detected is iterable %}
    {% for entry in info_detected %}
        {% set info = info|merge([entry]) %}
    {% endfor %}
{% endif %}
{% if info is iterable and info|length > 0 %}
    {% for entry in info %}
        {% set info_title = '' %}
        {% set info_message = entry %}
        {% if entry is iterable %}
            {% set info_title = entry.title %}
            {% set info_message = entry.message %}
        {% endif %}
        $('body')
          .toast({
            displayTime: 'auto',
            minDisplayTime: 5000,
            wordsPerMinute: 80,
            showProgress: 'bottom',
            closeIcon: true,
            position: 'top attached',
            title: '{{ info_title|e('js') }}',
            message: '{{ info_message|e('js') }}',
            showIcon: 'info',
            class: 'info'
          })
        ;
    {% endfor %}
{% endif %}

{# Let's see if there are loginfault messages to show #}
{% set loginfaults = flash.getMessage('loginfault') %}
{% if loginfault_detected is defined and loginfault_detected is iterable %}
    {% for l in loginfault_detected %}
        {% set loginfaults = loginfaults|merge([l]) %}
    {% endfor %}
{% endif %}

{# Let's see if there are error messages to show #}
{% set errors = flash.getMessage('error_detected') ?? [] %}
{% set errors = errors|merge(flash.getMessage('error') ?? []) %}
{% set errors = errors|merge(loginfaults ?? []) %}
{% if error_detected is defined and error_detected is iterable %}
    {% for e in error_detected %}
        {% set errors = errors|merge([e]) %}
    {% endfor %}
{% endif %}
{% if errors is iterable and errors|length > 0 %}
    {% for error in errors %}
        $('body')
          .toast({
            displayTime: 0,
            closeIcon: true,
            position: 'top attached',
            message: '{{ error|e('js') }}',
            showIcon: 'exclamation circle',
            class: 'error'
          })
        ;
    {% endfor %}
{% endif %}

{# Let's see if there are warning messages to show #}
{% set warnings = flash.getMessage('warning_detected') ?? [] %}
{% set warnings = warnings|merge(flash.getMessage('warning') ?? []) %}
{% if warning_detected is defined and warning_detected is iterable %}
    {% for w in warning_detected %}
        {% set warnings = warnings|merge([w]) %}
    {% endfor %}
{% endif %}
{% if warnings is iterable and warnings|length > 0 %}
    {% for warning in warnings %}
        $('body')
          .toast({
            displayTime: 0,
            closeIcon: true,
            position: 'top attached',
            message: '{{ warning|e('js') }}',
            showIcon: 'exclamation triangle',
            class: 'warning'
          })
        ;
    {% endfor %}
{% endif %}

{# Renew telemetry #}
{% if renew_telemetry is defined and renew_telemetry %}
    $('body')
      .toast({
        displayTime: 0,
        position: 'bottom right',
        message: "{{ _T("Your telemetry data are more than one year old.")|e('js') }} {{ _T("Do you want to send it again?")|e('js') }}",
        classActions: 'bottom attached',
        actions:    [{
          text: "{{ _T("Yes")|e('js') }}",
          class: 'green',
          click: function() {
              $.ajax({
                  url:  '{{ url_for('telemetryInfos') }}',
                  success: function(data) {
                      $('#telemetry_panel .tdata').append(data);
                      $('#telemetry_panel').modal('show');
                  }
              });
          }
        },{
          text: "{{ _T("No") }}",
          class: 'red',
          click: function() {
              Cookies.set(
                  'renew_telemetry',
                  1,
                  {
                      expires: 365,
                      path: '/'
                  }
              );
              $(this).toast('close');
          }
        },{
          text: "{{ _T("Later") }}",
          class: 'blue',
          click: function() {
              Cookies.set(
                  'renew_telemetry',
                  1,
                  {
                      expires: 182,
                      path: '/'
                  }
              );
              $(this).toast('close');
          }
        }]
      })
    ;
{% endif %}
