{#
#  Parameters:
#    * pages (int) : represents the total number of existing pages
#
# Affiche le composant de pagination.
# L'avancée dans la pagination s'effectue via le paramètre de route 'page'. S'il n'existe pas, sa valeur est considérée comme égale à 1.
# Enfin, ce composant transmet toute variable contenue dans l'URL lors d'un changement de page. Ainsi,
# il est possible de naviguer parmi les pages de résultats d'une recherche si les paramètres de celle-ci sont présents
# dans l'URL.  
#}

{% if pages is defined and pages > 1 %}
    {% set pageCurrent = app.request.attributes.get('page') ?? 1 %}
    
    {# the number of first and last pages to be displayed #}
    {% set extremePagesLimit = 2 %}
    {# the number of pages that are displayed around the active page #}
    {% set nearbyPagesLimit = 1 %}

    {% spaceless %}
        {% set route = app.request.attributes.get('_route') %}
        {% set route_args = app.request.attributes.get('_route_params')|merge(app.request.query.all) %}

        <ul class="pagination">
            {# Première page #}
            <li class="pagination-first{% if pageCurrent == 1 %} pagination-disabled{% endif %}">
                {% if pageCurrent > 1 %}
                    <a class="bubble bubble-sm" href="{{ path(route, route_args|merge({page:1})) }}" title="Première page">
                {% else %}
                    <span class="bubble bubble-sm">
                {% endif %}

                <i class="icon-cgenial icon-arrow-end-left"></i>

                {% if pageCurrent > 1 %}
                    </a>
                {% else %}
                    </span>
                {% endif %}
            </li>

            {# Page précédente #}
            <li class="pagination-previous{% if pageCurrent == 1 %} pagination-disabled{% endif %}">
                {% if pageCurrent > 1 %}
                    <a class="bubble bubble-sm" href="{{ path(route, route_args|merge({page:pageCurrent - 1})) }}" title="Page précédente">
                {% else %}
                    <span class="bubble bubble-sm">
                {% endif %}

                <i class="icon-cgenial icon-arrow-left"></i>

                {% if pageCurrent > 1 %}
                    </a>
                {% else %}
                    </span>
                {% endif %}
            </li>

            {# List des pages #}

            {% if pageCurrent > 1 %}
                {% for i in range(1, extremePagesLimit) if ( i < pageCurrent - nearbyPagesLimit ) %}
                <li class="page-{{ i }}">
                    <a class="bubble bubble-sm" href="{{ path(route, route_args|merge({page:i})) }}" title="Page {{ i }}"><span>{{ i }}</span></a>
                </li>
                {% endfor %}

                {% if extremePagesLimit + 1 < pageCurrent - nearbyPagesLimit %}
                <li class="sep-dots">
                    <span class="bubble bubble-sm">       
                        <span>..</span>
                    </span>        
                </li>
                {% endif %}

                {% for i in range(pageCurrent-nearbyPagesLimit, pageCurrent-1) if ( i > 0 ) %}
                <li class="page-{{ i }}">
                    <a class="bubble bubble-sm" href="{{ path(route, route_args|merge({page:i})) }}" title="Page {{ i }}"><span>{{ i }}</span></a>
                </li>
                {% endfor %}
            {% endif %}

            <li class="active">
                <span class="bubble bubble-sm bubble-theme"><span>{{ pageCurrent }}</span></span>
            </li>

            {% if pageCurrent < pages %}
                {% for i in range(pageCurrent+1, pageCurrent + nearbyPagesLimit) if ( i <= pages ) %}
                <li class="page-{{ i }}">
                    <a class="bubble bubble-sm" href="{{ path(route, route_args|merge({page:i})) }}" title="Page {{ i }}"><span>{{ i }}</span></a>
                </li>
                {% endfor %}

                {% if  (pages - extremePagesLimit) > (pageCurrent + nearbyPagesLimit) %}
                <li class="sep-dots">
                    <span class="bubble bubble-sm">       
                        <span>..</span>
                    </span>        
                </li>
                {% endif %}

                {% for i in range(pages - extremePagesLimit+1, pages) if ( i > pageCurrent + nearbyPagesLimit ) %}
                <li class="page-{{ i }}">
                    <a class="bubble bubble-sm" href="{{ path(route, route_args|merge({page:i})) }}" title="Page {{ i }}"><span>{{ i }}</span></a>
                </li>
                {% endfor %}
            {% endif %}

            {# Page suivante #}
            <li class="pagination-next{% if pageCurrent == pages %} pagination-disabled{% endif %}">
                {% if pageCurrent < pages %}
                    <a class="bubble bubble-sm" href="{{ path(route, route_args|merge({page:pageCurrent + 1})) }}" title="Page suivante">
                {% else %}
                    <span class="bubble bubble-sm">
                {% endif %}

                <i class="icon-cgenial icon-arrow-right"></i>

                {% if pageCurrent < pages %}
                    </a>
                {% else %}
                    </span>
                {% endif %}
            </li>

            {# Dernière page #}
            <li class="pagination-last{% if pageCurrent == pages %} pagination-disabled{% endif %}">
                {% if pageCurrent < pages %}
                    <a class="bubble bubble-sm" href="{{ path(route, route_args|merge({page:pages})) }}" title="Dernière page">
                {% else %}
                    <span class="bubble bubble-sm">
                {% endif %}

                <i class="icon-cgenial icon-arrow-end-right"></i>
                
                {% if pageCurrent < pages %}
                    </a>
                {% else %}
                    </span>
                {% endif %}
            </li>
        </ul>
    {% endspaceless %}
{% endif %}