<%! from urllib import urlencode %>

<% 
  def merge(dic1, dic2):
    return dict(dic1.items() + dic2.items())

  def url_for_page(_page):
    return base_url + '?' + urlencode(merge(query_params, {'page': _page}))
%>

<%def name="link_to_page(_page, text)">
  <a class="discussion-page-link" href="javascript:void(0)" page-url="${url_for_page(_page)}">${text}</a>
</%def>

<%def name="div_page(_page)">
  % if _page != page:
    <div class="page-link">
      ${link_to_page(_page, str(_page))}
    </div>
  % else:
    <div class="page-link">${_page}</div>
  % endif
</%def>

<%def name="list_pages(*args)">
  % for arg in args:
    % if arg == 'dots':
      <div class="page-dots">...</div>
    % elif isinstance(arg, list):
      % for _page in arg:
        ${div_page(_page)}
      % endfor
    % else:
      ${div_page(arg)}
    % endif
  % endfor
</%def>

<div class="discussion-${discussion_type}-paginator discussion-paginator">
  <div class="prev-page">
    % if page > 1:
      ${link_to_page(page - 1, "&lt; Previous page")}
    % endif
  </div>

  % if num_pages <= 2 * pages_nearby_delta + 2:
    ${list_pages(range(1, num_pages + 1))}
  % else:
    % if page <= 2 * pages_nearby_delta:
      ${list_pages(range(1, 2 * pages_nearby_delta + 2), 'dots', num_pages)}
    % elif num_pages - page + 1 <= 2 * pages_nearby_delta:
      ${list_pages(1, 'dots', range(num_pages - 2 * pages_nearby_delta, num_pages + 1))}
    % else:
      ${list_pages(1, 'dots', range(page - pages_nearby_delta, page + pages_nearby_delta + 1), 'dots', num_pages)}
    % endif
  % endif
  <div class="next-page">
    % if page < num_pages:
      ${link_to_page(page + 1, "Next page &gt;")}
    % endif
  </div>
</div>
