{% load bittersweet_querystring %}

{% comment %}
This template fragment assumes that you are using Bootstrap's default pagination
with a Django ListView CBV or equivalent which has the default is_paginated,
paginator, and page_obj variables defined.
{% endcomment %}

{% if is_paginated %}
    <nav class="w-100" aria-label="pagination">
        <ul class="pagination mx-auto justify-content-center">
            {% if page_obj.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?{% qs_alter request.GET page=page_obj.previous_page_number %}" aria-title="Previous Page">&larr;</a>
                </li>
            {% else %}
                <li class="page-item disabled" aria-hidden="true">
                    <span class="page-link">&larr;</span>
                </li>
            {% endif %}

            {% if page_obj.number > 1 %}
                <li class="page-item">
                    <a class="page-link" href="?{% qs_alter request.GET page=1 %}">1</a>
                </li>
            {% endif %}

            {% if page_obj.previous_page_number > 3 %}
                <li class="page-item disabled" aria-hidden="true"><span class="page-link">…</span></li>
            {% endif %}

            {% if page_obj.has_previous %}
                {% with page_obj.previous_page_number|add:-1 as second_previous_page %}
                    {% if second_previous_page > 1 %}
                        <li class="page-item">
                            <a class="page-link" href="?{% qs_alter request.GET page=second_previous_page %}">{{ second_previous_page }}</a>
                        </li>
                    {% endif %}
                {% endwith %}
            {% endif %}

            {% if page_obj.previous_page_number > 1 %}
                <li class="page-item">
                    <a class="page-link" href="?{% qs_alter request.GET page=page_obj.previous_page_number %}">{{ page_obj.previous_page_number }}</a>
                </li>
            {% endif %}

            <li class="page-item active">
                <a class="page-link" href="?{% qs_alter request.GET page=page_obj.number %}" aria-current="page">
                    {{ page_obj.number }}
                </a>
            </li>

            {% if page_obj.next_page_number < paginator.num_pages %}
                <li class="page-item">
                    <a class="page-link" href="?{% qs_alter request.GET page=page_obj.next_page_number %}">{{ page_obj.next_page_number }}</a>
                </li>
            {% endif %}

            {% if page_obj.has_next %}
                {% with page_obj.next_page_number|add:1 as second_next_page %}
                    {% if second_next_page < paginator.num_pages %}
                        <li class="page-item">
                            <a class="page-link" href="?{% qs_alter request.GET page=second_next_page %}">{{ second_next_page }}</a>
                        </li>
                    {% endif %}
                {% endwith %}
            {% endif %}

            {% if page_obj.next_page_number|add:2 < paginator.num_pages %}
                <li class="page-item disabled" aria-hidden="true"><span class="page-link">…</span></li>
            {% endif %}

            {% if page_obj.number < paginator.num_pages %}
                <li class="page-item">
                    <a class="page-link" href="?{% qs_alter request.GET page=paginator.num_pages %}">{{ paginator.num_pages }}</a>
                </li>
            {% endif %}

            {% if page_obj.has_next %}
                <li class="page-item">
                    <a class="page-link" href="?{% qs_alter request.GET page=page_obj.next_page_number %}">&rarr;</a>
                </li>
            {% else %}
                <li class="page-item disabled" aria-hidden="true">
                    <span class="page-link">&rarr;</span>
                </li>
            {% endif %}
        </ul>
    </nav>
{% endif %}
