{#
    Macros used by multiple templates.

    Original Moin code should be added here. Macros derived from Flatland should be added to forms.html.
#}

{% import "forms.html" as forms with context %}

{# "item" must be passed because of flakey jinja2 context passing #}
{% macro rev_navigation(rev_navigation_ids_dates, fqname, view='frontend.show_item', item=item) %}
    {% set prior_rev, current_rev, next_rev, prior_mtime, current_mtime, next_mtime = rev_navigation_ids_dates %}
    {% if not current_rev == None %}
        <ul id="moin-rev-navigation">
            <li class="moin-rev-navigation-older">
                {% if prior_mtime -%}
                    <a href="{{ url_for(view, item_name=fqname, rev=prior_rev) }}">
                        <span class="fa fa-arrow-left"></span>
                        {{ _('older') }} {{ prior_mtime|time_datetime }}
                    </a>
                {%- else -%}
                    <span class="moin-disabled">{{ _('no older revision') }}</span>
                {%- endif %}
            </li>
            <li class="moin-rev-navigation-current">
                {{ _('Revision ') }}
                {{ item.meta.rev_number or current_rev | shorten_id }}:
                {{ current_mtime|time_datetime }}
            </li>
            <li class="moin-rev-navigation-newer">
                {% if next_mtime -%}
                    <a href="{{ url_for(view, item_name=fqname, rev=next_rev) }}">
                        {{ _('newer') }} {{ next_mtime|time_datetime }}
                        <span class="fa fa-arrow-right"></span>
                    </a>
                {%- else -%}
                    <span class="moin-disabled">{{ _('no newer revision') }}</span>
                {%- endif %}
            </li>
        </ul>
        {%- if next_mtime and not ('trash' in item.meta and item.meta.trash) %}
            <div class="moin-revert">
                <a class="moin-button" href="{{ url_for('frontend.revert_item', item_name=fqname, rev=current_rev) }}">
                    {{ _("Revert to this revision") }}
                </a>
            </div>
        {%- endif %}
    {% endif %}
{% endmacro %}

{% macro show_editor_info(info) %}
    {%- if info.uri -%}
        <a href="{{ info.uri }}" class="{{ info.css }}" title="{{ info.title }}">
            {{ info.text }}
        </a>
    {%- elif info.email -%}
        <a href="mailto:{{ info.email }}" class="{{ info.css }}" title="{{ info.title }}">
            {{ info.text }}
        </a>
    {%- else -%}
        <span class="{{ info.css }}" title="{{ info.title }}">
            {{ info.text }}
        </span>
    {%- endif -%}
{% endmacro %}

{% macro editor_info(meta) %}
    {%- set info = get_editor_info(meta) -%}
    {{ show_editor_info(info) }}
{% endmacro %}

{% macro table(headings, rows, itemname_cols=[], url_cols=[]) %}
    <table class="zebra table table-hover">
        <thead>
            <tr>
                {% for heading in headings %}
                    {% if rows and rows[0][loop.index0] is number %}
                        <th class="moin-integer">{{ heading }}</th>
                    {% else %}
                        <th>{{ heading }}</th>
                    {% endif %}
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for row in rows %}
                <tr>
                    {% for col in row %}
                        {% if col is number %}
                            <td class="moin-integer">{{ col }}</td>
                        {% elif loop.index in itemname_cols %}
                            <td><a href="{{ url_for('frontend.show_item', item_name=col) }}">{{ col }}</a></td>
                        {% elif loop.index in url_cols %}
                            <td><a href="{{ col }}">{{ col }}</a></td>
                        {%- else -%}
                            <td>{{ col }}</td>
                        {% endif %}
                    {% endfor %}
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endmacro %}

{% macro header_search(form) %}
    {{ gen.form.open(form, id='moin-searchform', method='get', action=url_for('frontend.search')) }}
            {{ forms.render(form['q']) }}
            {{ forms.render_submit(form, id='moin-search-submit') }}
            {{ forms.render_errors(form) }}
    {{ gen.form.close() }}
{% endmacro %}

{# return help for editors based upon current item's contenttype #}
{% macro help_on_editing(help) %}
    {% if help %}
        {% if isinstance(help, tuple) %}
            {# display nice button with link to internal help: moinwiki, creole, markdown... #}
            <span class="moin-edit-help"><a href="{{ help[0] }}">{{ help[1] }}</a></span>
        {% else %}
            {# no help, one sentence help, or link to help in external site: CSV, images, binary... #}
            <span class="moin-edit-no-help"> {{ help|safe }} </span>
        {% endif %}
    {% endif %}
{% endmacro %}

{# display meta info in delete, destroy, rename, revert and meta view. #}
{% macro meta_info(meta) %}
    {#- Bootstrap classes list-group and list-group-item used to display the metadata info in an unordered list #}
    <table class="moin-item-metadata">
        <tr><th>{{ _('Key') }}</th><th>{{ _('Value') }}</th></tr>
        {%- if user.may.admin(fqname) %}
            {%- set acl = meta['acl'] or '(None)' %}
            <tr><td>ACL</td><td>{{ acl }}</td></tr>
        {%- endif %}
        <tr><td>Action</td><td>{{ meta['action'] }}</td></tr>
        <tr><td>Address</td><td>{{ meta['address'] }}</td></tr>
        <tr><td>Comment</td><td>{{ meta['comment'] }}</td></tr>
        <tr><td>Content Type</td><td>{{ meta['contenttype']|shorten_ctype }} [{{ meta['contenttype'] }}]</td></tr>
        <tr><td>Data ID</td><td>{{ meta['dataid'] }}</td></tr>
        <tr><td>External Links</td><td>
            {%- if meta['externallinks'] -%}
                {%- for item in meta['externallinks']|sort -%}
                    <a href="{{ item|safe }}">{{ item }}</a>
                    {%- if not loop.last %}, {% endif -%}
                {%- endfor -%}
            {%- else -%}
                (None)
            {%- endif -%}
        </td></tr>
        {% if meta.get('has_tag') %}
            {# has_tag should not exist in metadata file, belongs only in whoosh index #}
            <tr><td>Has Tag ERROR</td><td>{{ meta['has_tag'] }}</td></tr>
        {% endif %}
        <tr><td>Item ID</td><td>{{ meta['itemid'] }}</td></tr>
        <tr>
            <td>Item Links</td>
            <td>
                {%- if meta['itemlinks'] -%}
                    {%- for item in meta['itemlinks']|sort -%}
                        <a href="{{ url_for('frontend.show_item', item_name=item) }}" {% if not theme_supp.itemlink_exists(item) %}class="moin-nonexistent"{% endif %}>{{ item }}</a>
                        {%- if not loop.last %}, {% endif -%}
                    {%- endfor -%}
                {%- else -%}
                    (None)
                {%- endif -%}
            </td>
        </tr>
        <tr>
            <td>Item Transclusions</td>
            <td>
                {%- if meta['itemtransclusions'] -%}
                    {%- for item in meta['itemtransclusions']|sort -%}
                        <a href="{{ url_for('frontend.show_item', item_name=item) }}" {% if not theme_supp.item_exists(item) %}class="moin-nonexistent"{% endif %}>{{ item }}</a>
                        {%- if not loop.last %}, {% endif -%}
                    {%- endfor -%}
                {%- else -%}
                    (None)
                {%- endif -%}
            </td>
        </tr>
        <tr><td>Item Type</td><td>{{ meta['itemtype'] }}</td></tr>
        <tr><td>Language</td><td>{{ meta['language'] }}</td></tr>
        <tr><td>Modified Time</td><td>{{ meta['mtime']|time_datetime }}</td></tr>
        <tr>
            <td>Name</td>
            <td>
                {%- if meta['name'] -%}
                    {%- set names = ' <i class="fa fa-circle moin-tiny"></i> '.join(meta['name']) -%}
                    {{ names|safe }}
                {%- else -%}
                    (None)
                {%- endif -%}
            </td>
        </tr>
        <tr>
            <td>Old Name</td>
            <td>
                {%- if meta['name_old'] -%}
                    {%- set names = ' <i class="fa fa-circle moin-tiny"></i> '.join(meta['name_old']) -%}
                    {{ names|safe }}
                {%- else -%}
                    (None)
                {%- endif -%}
            </td>
        </tr>
        <tr><td>Namespace</td><td>{{ meta['namespace'] }}</td></tr>
        <tr><td>Parent Revision ID</td><td>{{ meta['parentid'] }}</td></tr>
        <tr><td>Revision ID</td><td>{{ meta['revid'] }}</tr>
        <tr><td>Revision Number</td><td>{{ meta['rev_number'] }}</td></tr>
        <tr><td>SHA1</td><td>{{ meta['sha1'] }}</td></tr>
        <tr><td>Size</td><td>{{ meta['size']|filesizeformat }}</td></tr>
        <tr><td>Summary</td><td>{{ meta['summary'] }}</td></tr>
        <tr><td>Tags</td><td>
            {%- if meta['tags'] -%}
                {%- for tag in meta['tags'] -%}
                    {{ tag }} {%- if not loop.last %}, {% endif -%}
                {%- endfor -%}
            {%- else -%}
                (None)
            {%- endif -%}
        </td></tr>
        <tr><td>Trash</td><td>{{ meta['trash'] or False }}</td></tr>
        {% if 'usergroup' in meta %}
            <tr><td>User Group</td><td>{{ meta['usergroup'] }}</td></tr>
        {% endif %}
        <tr><td>User ID</td><td>{{ meta['userid'] }}</td></tr>
        {% if 'wikidict' in meta %}
            <tr><td>Wiki Dict</td><td>{{ meta['wikidict'] }}</td></tr>
        {% endif %}
    </table>
{% endmacro %}

{%- macro alias_list(alias) -%}
    {%- if alias %}
        <i class="fa fa-circle moin-tiny"> </i>
        <ul class="moin-alias">
            <li> {{ _('Alias Names') }} </li>
            {%- for fq_name in alias -%}
                <li>
                    <a href="{{ url_for('frontend.show_item', item_name=fq_name) }}">
                        {{ fq_name|shorten_fqname(length=25) }}
                    </a>
                </li>
            {%- endfor %}
        </ul>
    {%- endif %}
{%- endmacro -%}

{% macro page_trail_link(wiki_name, fqname) %}
    {%- if wiki_name -%}
        <a href="{{ item_href }}"{{ " " }} title="{{ wiki_name }}" class="{% if err %}moin-badinterwiki{% else %}moin-interwiki{% endif %}">
            {{ fqname|shorten_fqname }}
        </a>
    {%- else -%}
        <a href="{{ url_for('frontend.show_item', item_name=fqname) }}"{{ " " }}>
            {{ fqname|shorten_fqname }}
        </a>
    {%- endif %}
{% endmacro %}

{% macro breadcrumbs(breadcrumbs, title_name) %}
    {#
    Produce a page trail given a list of recently visited items,
    and an optional title_name (means current view is a Navigation Link: History, Index, Tags...).
        - if an item has alias names, all alias names must be provided (themes can show, suppress or provide a rollover action)
        - if an item is not in the default namespace, the namespace will be prefixed to the item name
        - last item on page trail must show individual links to all parent items
    #}
    <ul class="moin-breadcrumb">
        <li class="moin-panel-heading">{{ _('Page Trail') }}</li>
        {%- set trail_items = breadcrumbs() %}
        {%- if trail_items %}
            {%- for wiki_name, fqname, item_href, aliases, err in trail_items %}
                {%- if not loop.last %}
                    <li>
                        {{ page_trail_link(wiki_name, fqname) }}
                        {{ alias_list(aliases) }}
                        <i class="fa fa-angle-double-right fa-fw"></i>
                    </li>
                {%- endif %}
                {%- if loop.last %}
                    <li>
                        <span class="moin-big">
                            {%- for segment_name, segment_path in theme_supp.location_breadcrumbs(fqname) -%}
                                {%- if not loop.last %}
                                    <a href="{{ url_for('frontend.show_item', item_name=segment_path) }}">
                                        {{ segment_name|shorten_fqname }}
                                    </a>
                                {%- else %}
                                    <a href="{{ url_for('frontend.show_item', item_name=segment_path) }}">
                                        {{ segment_name|shorten_fqname }}
                                    </a>
                                {%- endif %}
                            {%- endfor %}
                        </span>
                        {{ alias_list(aliases) }}
                    </li>
                    {%- if title_name %}
                        <li>
                            <i class="fa fa-angle-double-right fa-fw"></i>
                            {{ title_name }}
                        </li>
                    {%- endif %}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
    </ul>
{% endmacro %}

{# Given an fqname, create a link to the item. The full name is displayed, including the namespace. #}
{% macro make_item_link(fq_name) %}
    <li>
        <a class="moin-fqname"
           href="{{ url_for('frontend.show_item', item_name=fq_name) }}"
           {% if fq_name.value != fq_name|string %}
               title="{{ fq_name }}"
           {% endif %}>
           {{ fq_name.fullname }}
       </a>
    </li>
{% endmacro %}

{# called by diff to provide revision navigation on right column:  <  revision: 5  >  >> #}
{% macro show_newer_rev_links(item_name, rev, rev_links) %}
    <div class="moin-diff-revision-caption">
        {% if rev_links['r2_older'] %}
            <a href="{{ url_for('frontend.diff', item_name=item_name, rev1=rev_links['revid1'], rev2=rev_links['r2_older']) }}" title="{{ _('older') }}">
                <i class="fa fa-backward"> </i>
            </a>
        {% endif %}
        <span class="moin-diff-info-caption">
            {{ _('Revision') }}:
        </span>
        <span class="moin-diff-info-value">
            {{ rev.meta['rev_number'] or rev.revid|shorten_id }}
        </span>
        {% if rev_links['r2_newer'] %}
            <a href="{{ url_for('frontend.diff', item_name=item_name, rev1=rev_links['revid1'], rev2=rev_links['r2_newer']) }}" title="{{ _('newer') }}">
                <i class="fa fa-forward"> </i>
            </a>
        {% endif %}
        {% if rev_links['r2_newest'] %}
            <a href="{{ url_for('frontend.diff', item_name=item_name, rev1=rev_links['revid1'], rev2=rev_links['r2_newest']) }}" title="{{ _('newest') }}">
                <i class="fa fa-fast-forward"> </i>
            </a>
        {% endif %}
    </div>
{% endmacro %}

{# called by diff to provide revision navigation on left column:  <<  <  revision: 5  >  #}
{% macro show_older_rev_links(item_name, rev, rev_links) %}
    <div class="moin-diff-revision-caption">
        {% if rev_links['r1_oldest'] %}
            <a href="{{ url_for('frontend.diff', item_name=item_name, rev2=rev_links['revid2'], rev1=rev_links['r1_oldest']) }}" title="{{ _('oldest') }}">
                <i class="fa fa-fast-backward"> </i>
            </a>
        {% endif %}
        {% if rev_links['r1_older'] %}
            <a href="{{ url_for('frontend.diff', item_name=item_name, rev2=rev_links['revid2'], rev1=rev_links['r1_older']) }}" title="{{ _('older') }}">
                <i class="fa fa-backward"> </i>
            </a>
        {% endif %}
        <span class="moin-diff-info-caption">
            {{ _('Revision') }}:
        </span>
        <span class="moin-diff-info-value">
            {{ rev.meta['rev_number'] or rev.revid|shorten_id }}
        </span>
        {% if rev_links['r1_newer'] %}
            <a href="{{ url_for('frontend.diff', item_name=item_name, rev2=rev_links['revid2'], rev1=rev_links['r1_newer']) }}" title="{{ _('newer') }}">
                <i class="fa fa-forward"> </i>
            </a>
        {% endif %}
    </div>
{% endmacro %}

{# create a header for a diff revision #}
{% macro show_meta(rev, do_rev=True) %}
    {% if do_rev %}
        <div>
            <span class="moin-diff-info-caption">
                {{ _('Revision') }}:
            </span>
            <span class="moin-diff-info-value">
                {{ rev.meta['rev_number'] or rev.revid|shorten_id }}
            </span>
        </div>
    {% endif %}
    <div>
        <span class="moin-diff-info-caption">
            {{ _('Editor') }}:
        </span>
        <span class="moin-diff-info-value">
            {{ editor_info(rev.meta) }}
        </span>
    </div>
    <div>
        <span class="moin-diff-info-caption">
            {{ _('Size') }}:
        </span>
        <span class="moin-diff-info-value">
            {{ rev.meta['size'] }}:
        </span>
    </div>
    <div>
        <span class="moin-diff-info-caption">
            {{ _('Time') }}:
        </span>
        <span class="moin-diff-info-value">
            {{ rev.meta.mtime|time_datetime }}
        </span>
    </div>
    <div>
        <span class="moin-diff-info-caption">
            {{ _('Comment') }}:
        </span>
        <span class="moin-diff-info-value">
            {{ rev.meta['comment'] }}
        </span>
    </div>
{% endmacro %}

{# Render a table showing diffs for two text item revisions. #}
{% macro diff_table(diffs) %}
    <table id="moin-preview-diff", class="moin-diff">
        {% if rev_links %}
            {# caller is from Item History, show links to older/newer revisions. #}
            <tr>
                <td class="moin-diff-header">
                    {{ show_older_rev_links(item_name, oldrev, rev_links) }}
                    {{ show_meta(oldrev, do_rev=False) }}
                </td>
                <td class="moin-diff-header">
                    {{ show_newer_rev_links(item_name, newrev, rev_links) }}
                    {{ show_meta(newrev, do_rev=False) }}
                </td>
            </tr>
        {% endif %}
        <tr>
            <td class="moin-diff-removed"><span>{{ _('Deletions are marked like this.') }}</span></td>
            <td class="moin-diff-added"><span>{{ _('Additions are marked like this.') }}</span></td>
        </tr>
        {% for llineno, lcontent, rlineno, rcontent in diffs %}
            <tr class="moin-diff-line-number">
                {# these links are to non-existent anchors, see diffScroll in common.js #}
                <td><a href="#{{ llineno }}">{{ _('Line') }} {{ llineno }}:</a></td>
                <td><a href="#{{ rlineno }}">{{ _('Line') }} {{ rlineno }}:</a></td>
            </tr>
            <tr>
                <td class="moin-diff-removed">{{ lcontent }}</td>
                <td class="moin-diff-added">{{ rcontent }}</td>
            </tr>
        {% endfor %}
    </table>
{% endmacro %}

{# Return links to Login or User/Settings/Logout; callers must provide an enclosing UL tag #}
{%- macro user_login_logoff() %}
    {%- if user.valid -%}
        {%- set avatar = user.avatar(20) %}
        {%- if avatar %}
            <li>
                <img id="moin-avatar" src="{{ avatar }}">
            </li>
        {%- endif %}
        {% if user.name -%}
            {%- set wiki_href, display_name, title, exists = theme_supp.userhome() %}
            <li>
                <a href="{{ wiki_href }}" {% if not exists %}class="moin-nonexistent"{% endif %} rel="nofollow" title="{{ title }}">
                    <i class="fa fa-user"> </i>
                    <span>{{ display_name }}</span>
                </a>
            </li>
            {%- if 'frontend.usersettings' not in cfg.endpoints_excluded -%}
                <li>
                    <a href="{{ url_for('frontend.usersettings') }}" class="moin-usersettings" rel="nofollow" title="{{ _('Settings') }}">
                        <i class="fa fa-cog"> </i>
                        <span>{{ _('Settings') }}</span>
                    </a>
                </li>
            {%- endif %}
        {%- endif %}
        {%- if user.auth_method in cfg.auth_can_logout %}
            <li>
                <a href="{{ url_for('frontend.logout', logout_submit=1) }}" class="moin-logout" rel="nofollow" title="{{ _('Logout') }}">
                    <i class="fa fa-sign-out"> </i>
                    <span>{{ _('Logout') }}</span>
                </a>
            </li>
        {%- endif %}
    {%- else %}
        {%- set login_url = theme_supp.login_url() %}
        {%- if login_url %}
            <li>
                <a href="{{ login_url }}" class="moin-login" rel="nofollow">{{ _('Login') }}</a>
            </li>
        {%- endif %}
    {%- endif %}
{% endmacro %}

{# create an icon with a link to revert or diff, or an icon based on contenttype of item #}
{% macro item_status_icon(fqname, meta, bookmark_time) %}
    {% if meta.trash %}
        {% set trash_class = "fa fa-undo fa-lg" if meta.parentid else "fa fa-trash-o" %}
        <a href="{{ url_for('frontend.revert_item', item_name=fqname, rev=meta.parentid) }}" title="{{ _('revert') }}">
            <span class="{{ trash_class }}" title="{{ _('Revert deleted item') }} - {{ meta.contenttype|shorten_ctype }}"></span>
        </a>
    {% elif meta.rev_number > 1 %}
        {% if bookmark_time -%}
            <a href="{{ url_for('frontend.diff', item_name=fqname, bookmark=bookmark_time) }}">
                <span class="fa fa-copy fa-lg" title="{{ _('show diff based on bookmark') }}"></span>
            </a>
        {% else %}
            <a href="{{ url_for('frontend.diff', item_name=fqname, rev1=meta.parentid, rev2=meta.revid) }}">
                <span class="fa fa-copy fa-lg" title="{{ _('show diff with prior revision') }} - {{ meta.contenttype|shorten_ctype }}"></span>
            </a>
        {%- endif %}
    {% else %}
        <span class="{{ meta.contenttype|contenttype_to_class }}" title="{{ meta.contenttype|shorten_ctype }}"></span>
    {% endif %}
{% endmacro %}

{# Display an item name, item alias names, item summary or item ID and old names #}
{%- macro item_moniker(meta, fqnames) %}
    {%- if meta.name %}
        {#- if there are alias names, create a link to each alias #}
        {%- for name in fqnames -%}
            {%- if not loop.first %}
                <i class="fa fa-circle moin-tiny"></i>
            {% endif -%}
            {% if name.field == 'itemid' %}
                {# an item revision of a deleted item is being destroyed #}
                <a href="{{ url_for('frontend.show_item', item_name=name) }}">{{ name|shorten_fqname }}</a> ~( {{ meta.name[0] }})
            {% else %}
                <a href="{{ url_for('frontend.show_item', item_name=name) }}">{{ name }}</a>
            {% endif %}
        {%- endfor -%}
    {%- else %}
        {% if meta.summary %}
            <a class="moin-nameless-item" href="{{ url_for('frontend.show_item', item_name=fqnames[0]) }}" title="{{ meta.summary }}">{{ fqnames[0]|shorten_fqname }}</a>
        {% else %}
            <a class="moin-nameless-item" href="{{ url_for('frontend.show_item', item_name=fqnames[0]) }}">{{ fqnames[0]|shorten_fqname }}</a>
        {% endif %}
    {%- endif %}
    {%- if not meta.name and meta.name_old %}
        {#- this is a deleted item, show old name(s) #}
        <span class="moin-history-name-old" title="{{ meta.name_old[0] }} was deleted">~({{ meta.name_old|join(' | ') }})</span>
    {%- endif %}
{%- endmacro %}

{# Create next page, prior page links for multi-page reports. #}
{% macro page_links(page_num, pages, url) %}
    {# if history is long and displayed as several pages, create links to show next and/or prior pages #}
    <div class="moin-offset-links">
        {% if page_num > 1 %}
            <a class="moin-prior-page" href="{{ url }}?page_num={{ page_num-1 }}" title="{{ _("Previous") }}">&laquo;</a>
        {% endif %}
        {% if pages > 1 %}
            {% set page_number = _("Page {page_num} of {pages}").format(page_num=page_num, pages=pages) %}
            <span class="moin-page-num"> {{ page_number }} </span>
        {% endif %}
        {% if page_num < pages %}
            <a class="moin-next-page" href="{{ url }}?page_num={{ page_num+1 }}" title="{{ _("Next") }}">&raquo;</a>
        {% endif %}
    </div>
{% endmacro %}
