{#
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#}

{% from "mediagoblin/utils/pagination.html" import render_pagination %}

{% macro media_grid(request, collection_items, col_number=5) %}
  <div class="thumb_gallery">
    {% for row in collection_items|batch(col_number) %}
      <div class="row thumb_row
                 {%- if loop.first %} thumb_row_first
                 {%- elif loop.last %} thumb_row_last{% endif %}">
        {% for item in row %}
          {% set obj = item.get_object() %}
          {% set obj_url = obj.url_for_self(request.urlgen) %}
          <div class="three columns media_thumbnail thumb_entry
                     {%- if loop.first %} thumb_entry_first
                     {%- elif loop.last %} thumb_entry_last{% endif %}">
            <a href="{{ obj_url }}">
              <img src="{{ obj.thumb_url }}" />
            </a>

            {% if item.note %}
              <a href="{{ obj_url }}">{{ item.note }}</a>
            {% endif %}
	    {% if request.user and
                  (item.in_collection.actor == request.user.id or
                  request.user.has_privilege('admin')) %}
	      {%- set remove_url=request.urlgen(
                                 'mediagoblin.user_pages.collection_item_confirm_remove',
                                 user=item.in_collection.get_actor.username,
                                 collection=item.in_collection.slug,
                                 collection_item=item.id) -%}
	      <a href="{{ remove_url }}" class="remove">
	        {%- trans %}(remove){% endtrans -%}
	      </a>
	    {% endif %}
          </div>
        {% endfor %}
      </div>
    {% endfor %}
  </div>
{%- endmacro %}

{#
  Render a media gallery with pagination.

  Args:
   - request: Request
   - collection_items: cursor of collection items
   - pagination: Paginator object
   - pagination_base_url: If you want the pagination to point to a
     different URL, point it here
   - col_number: How many columns per row (default 5)
#}
{% macro collection_gallery(request, collection_items, pagination,
                        pagination_base_url=None, col_number=5) %}
  {% if collection_items and collection_items.count() %}
    {{ media_grid(request, collection_items, col_number=col_number) }}
    <div class="clear"></div>
    {% if pagination_base_url %}
      {# different url, so set that and don't keep the get params #}
      {{ render_pagination(request, pagination, pagination_base_url, False) }}
    {% else %}
      {{ render_pagination(request, pagination) }}
    {% endif %}
  {% else %}
    <p>
      <i>
        {%- trans -%}
          There doesn't seem to be any media here yet...
        {%- endtrans -%}
      </i>
    </p>
  {% endif %}
{% endmacro %}
