{% macro status_badge(thing) -%}
    {% if not thing or thing.ident == 'unknown' %}
        <span class="badge"
            style="border: 1px solid #CCCCCC; background-color: transparent; color: #CCCCCC;"
            title="Unknown"
        >
            ?
        </span>
    {% else %}
        <span class="badge"
            style="background-color: #{{ thing.color }} !important;"
            title="{{ thing.name }}"
        >
            {{ thing.abbrev }}
        </span>
    {% endif %}
{%- endmacro %}

{% macro naming_status_badge(thing) -%}
    {% if not thing or thing.ident == 'unknown' %}
        <span class="naming-badge"
            style="border: 1px solid #CCCCCC; background-color: transparent; color: #CCCCCC;"
            title="Unknown"
        >
        </span>
    {% else %}
        <span class="naming-badge"
            style="background-color: #{{ thing.color }} !important;"
            title="{{ thing.name }}"
        >
        </span>
    {% endif %}
{%- endmacro %}

{% macro pkglink(
    pkg, ignore_misnamed=False, ignore_unversioned_requires=False, after_name='')
-%}
    {{- pkglink_icon(pkg) -}}
    &nbsp;<a href="{{ url_for('package', pkg=pkg.name) }}">
        {{- pkg.name -}}
    </a>
    {{- after_name -}}
    {{- pkg_naming_badge(
        pkg,
        ignore_misnamed=ignore_misnamed,
        ignore_unversioned_requires=ignore_unversioned_requires,
    ) -}}
    {{- ftbfs_badge(pkg) -}}
    {{- orphan_badge(pkg) -}}
    {{- exception_badge(pkg) -}}
{%- endmacro %}

{% macro orphan_badge(pkg) -%}
    {%- if 'orphan' in pkg['maintainers'] -%}
        <sup>
            <i class="fa fa-power-off" title="The package is orphaned."></i>
            {{ orphan_clock_badge(pkg) }}
        </sup>
    {%- endif -%}
{%- endmacro -%}

{% macro orphan_clock_badge(pkg) -%}
    {%- set change = pkg.get('last_repo_metadata_change') -%}
    {%- if change -%}
        {%- if (now - change).days >= 6*7 -%}
            {{ _orphan_clock_badge_i("hourglass-o", change) }}
        {%- elif (now - change).days > 5*7 -%}
            {{ _orphan_clock_badge_i("hourglass-3", change) }}
        {%- elif (now - change).days > 3*7 -%}
            {{ _orphan_clock_badge_i("hourglass-2", change) }}
        {%- elif (now - change).days >= 0 -%}
            {{ _orphan_clock_badge_i("hourglass-1", change) }}
        {%- else -%}
            {{ _orphan_clock_badge_i("hourglass", change) }}
        {%- endif -%}
    {%- endif -%}
{%- endmacro -%}

{% macro _orphan_clock_badge_i(icon, change) -%}
    <i class="fa fa-{{ icon }}"
       title="Orphaned since {{ change | format_time_ago }},
       {{- "\n" }}{{ change  }} ({{ (now - change).days }} days)."></i>
{%- endmacro %}

{% macro exception_badge(pkg) -%}
    {%- for grp in pkg['groups'].values() -%}
        {%- if 'exception' in grp and not grp['hidden'] and pkg['name'] in grp['seed_packages'] %}
                <a href="{{ url_for('group', grp=grp['ident']) }}" title="FESCo exception for {{ grp['name'] }}">
                    <i class="fa fa-thumb-tack"></i>
                </a>
        {%- endif -%}
    {%- endfor -%}
{%- endmacro %}

{% macro pkglink_icon(pkg) -%}
    <span class="pkgstatus-icon" style="background-color: {{ pkg.status_obj.color }}">&nbsp;</span>
{%- endmacro %}

{% macro ftbfs_badge(pkg) -%}
    {% if pkg.ftbfs_age %}
        <span
            class="ftbfs-badge"
            title="FTBFS alert! {#
                   -#} Built package appears to be from {#
                   -#} Fedora {{ pkg.last_build_releasever }}."
        >
            {% for i in range(pkg.ftbfs_age) %}!{% endfor %}
        </span>
    {%- endif -%}
{%- endmacro %}

{% macro pkglink_text(pkg) -%}
    <a href="{{ url_for('package', pkg=pkg.name) }}">{{ pkg.name }}</a>
{%- endmacro %}

{% macro pkg_naming_badge(pkg, ignore_misnamed=False, ignore_unversioned_requires=False) -%}
    {%- if pkg['is_misnamed'] and not ignore_misnamed %}
        {{ misnamed_badge() }}
    {% endif -%}
    {%- if pkg['unversioned_requires'] and not ignore_unversioned_requires %}
        {{ unversioned_requires_badge( pkg['blocked_requires']) }}
    {% endif -%}
{%- endmacro %}

{% macro _naming_badge(color, text) -%}
    <span
        class="naming-badge"
        style="background-color: #{{ color }} !important;"
        title="{{ text }}"
    ></span>
{%- endmacro %}

{% macro misnamed_badge() -%}
    {{ _naming_badge('F0AD4E', 'Misnamed package') }}
{%- endmacro %}

{% macro unversioned_requires_badge(blocked) -%}
    {%- if blocked -%}
        {{ _naming_badge('a65959', 'Ambiguous Requires (blocked)') }}
    {%- else -%}
        {{ _naming_badge('8c6699', 'Ambiguous Requires') }}
    {%- endif -%}
{%- endmacro %}

{% macro print_deptree(nodes) -%}
    <ul class="simple-pkg-list">
        {{ sub_deptree(nodes) }}
    </ul>
{%- endmacro %}

{% macro sub_deptree(nodes) -%}
    {%- for node in nodes -%}
        <li title="{{node.path}}">
            {{ pkglink(node.package, after_name=':' if node.children else '') }}
            {% if 'run' in node.kinds -%}
                <i class="dep-kind fa fa-rocket" title="Run-time"></i>
            {%- endif -%}
            {%- if 'build' in node.kinds %}
                <i class="dep-kind fa fa-wrench" title="Build-time"></i>
            {%- endif -%}
            {%- if 'elided' in node.kinds -%}
                <span title="Subtree elided"> ⋯</span>
            {%- endif -%}
            {%- if node.children -%}
                <ul class="simple-pkg-list deptree-level">
                    {{- sub_deptree(node.children) -}}
                </ul>
            {%- elif 'too-big' in node.kinds -%}
                <div class="deptree-level deptree-elided" title="Subtree elided (graph too large)">⋯</div>
            {%- endif -%}
        </li>
    {%- endfor -%}
{%- endmacro %}

{% macro pkglist_table_head() -%}
    <thead>
        <tr>
            <th>Package</th>
            <th data-x-colspan="2" class="th-collection">Status</th>
        </tr>
    </thead>
{%- endmacro %}

{% macro pkglist_table_content(packages, show_nonblocking=False) -%}
    {{ pkglist_table_head() }}
    <tbody>
        {% for pkg in packages|sort_by_status %}
            <tr>
                {{ package_tr_content(cname, pkg, show_nonblocking=show_nonblocking) }}
            </tr>
        {% endfor %}
    </tbody>
{%- endmacro %}

{% macro package_tr_content(category_name, pkg, show_nonblocking=False) -%}
    <td>
        {{ pkglink(pkg) }}
        {% if show_nonblocking and pkg.nonblocking %}
            <small>(nonblocking)</small>
        {% endif %}
    </td>
    <td>
        {{ status_badge(pkg.status_obj) }}
        {% for link in pkg.links %}
            {{ iconlink(link) }}
        {% endfor %}
    </td>
{%- endmacro %}

{% macro iconlink_class(link) -%}
    {% if link.type == 'repo' %}
        iconlink fa fa-code
    {% elif link.type == 'homepage' %}
        iconlink fa fa-home
    {% endif %}
{%- endmacro %}

{% macro iconlink_alt(link) -%}
    {{ link.type }}
    {%- if link.note -%}
        {{" "}}({{ link.note }})
    {%- endif -%}
{%- endmacro %}

{% macro bugicon(bug, extra_class='') %}
    <i class="iconlink fa fa-bug {{ extra_class }} iconlink-bug-{{ bug.status }}
    {% if bug.resolution %}iconlink-bug-{{ bug.status }}-{{ bug.resolution }}{% endif %}
    "
    title="{{ bug.short_desc }} ({{ bug.status }}
            {%- if bug.resolution %} {{ bug.resolution }}{% endif -%}
            ; updated {{ bug.last_change.date() }})">
    </i>
{% endmacro %}

{% macro iconlink(link) -%}
    <a href="{{ link.url }}" title="{{ iconlink_alt(link) }}" class="{{ iconlink_class(link) }}">
        <span class="type-alt">{{ iconlink_alt(link) }}</span>
    </a>
{%- endmacro %}

{% macro progress_summary(status_counts, total) -%}
    <div class="progress-with-legend">
        <div class="progress">
            {% for status, num in status_counts %}
                {% if num %}
                    <div
                        class="progress-bar" role="progressbar"
                        aria-valuenow="{{ num }}"
                        aria-valuemin="0" aria-valuemax="{{ total }}"
                        style="width: {{ 100 * num / total }}%;
                               background-color: #{{ status.color }} !important;
                               color: black;"
                        title="{{ num }} {{ status.name }}"
                    >
                    </div>
                {% endif %}
            {% endfor %}
        </div>
        {{ progress_summary_legend(status_counts, total) }}
    </div>
{%- endmacro %}

{% macro progress_summary_legend(status_counts, total=None, kind='') -%}
    <div>
        {% if total != None %}
            {{ total }} {{ kind }} packages:
        {% endif %}
        {% for status, num in status_counts %}
            <span class="progress-legend-item">
            {{ status_badge(status) }}
            {{ num }} {{ status.name }}{% if not loop.last %};{% endif %}
            </span>
        {% endfor %}
    </div>
{%- endmacro %}

{% macro progress_legend(packages, kind) -%}
    {{ progress_summary_legend(packages|summarize_statuses, packages|length, kind=kind) }}
{%- endmacro %}

{% macro loc_chart(pkg) -%}
    {% if pkg['loc_total'] %}
        {% set max_size = 20000000  %} {# (a bit bigger than the biggest repo - kernel) #}
        {% set width = ((log(pkg['loc_total'], max_size))) %}
        <div class="progress" style="width: {{ 100*width }}%">
            {% if pkg['loc_python'] and pkg['loc_python'] > 0.0001 %}
                <div
                    class="progress-bar progress-bar-info"
                    style="width: {{ 100 * ((pkg['loc_python'] / pkg['loc_total'])) }}%;"
                    title="Python"
                >
                </div>
            {% endif %}
            {% if pkg['loc_capi'] and pkg['loc_capi'] > 0.0001 %}
                <div
                    class="progress-bar progress-bar-warning"
                    style="width: {{ 100 * ((pkg['loc_capi'] / pkg['loc_total'])) }}%;"
                    title="C/C++ with CPython API"
                >
                </div>
            {% endif %}
        </div>
    {% endif %}
{%- endmacro %}

{%- macro maintainer_link(maintainer) -%}
    {% if maintainer.packages | length > 1 %}
        <a href="{{ url_for('maintainer', name=maintainer.name) }}">
            {{- maintainer.name -}}
        </a>
    {% else %}
        {{ maintainer.name }}
    {% endif %}
    ({{ maintainer.packages | length }})
    <a href="https://src.fedoraproject.org/user/{{ maintainer.name }}">
        <i class="fa fa-th-list"></i>
    </a>
{%- endmacro -%}

{%- macro nonpython_package_links(name) -%}
    <a href="https://src.fedoraproject.org/rpms/{{ name }}/"
        >Pagure</a>,
    <a href="https://src.fedoraproject.org/rpms/{{ name }}/blob/rawhide/f/{{ name }}.spec"
        >spec</a>
{%- endmacro -%}

<html xmlns:xlink="http://www.w3.org/1999/xlink">
    <head>
        <title>{% block titlecontent %}Python 2 Dropping Database{% endblock titlecontent %}</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        {% block favicon %}
        <link rel="icon" type="image/svg+xml" href="{% block favicon_url %}{% endblock favicon_url %}">
        {% endblock favicon %}
        <link rel="stylesheet" href="{{ url_for('static', filename='bootstrap.min.css') }}">
        <link rel="stylesheet" href="{{ url_for('static', filename='font-awesome/css/font-awesome.min.css') }}">
        <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
        <style>
            {% block style %}
            {% if 'extra-css' in config %}
                {{ config['extra-css'] }}
            {% endif %}

            {% endblock style %}
        </style>
    </head>
    <body>
        {% block breadcrumbs %}
            <nav>
                <div class="container">
                    <ol class="breadcrumb">
                        {% for url, name in breadcrumbs %}
                            {% if loop.last %}
                                <li class="active">{{ name }}</li>
                            {% else %}
                                <li>
                                    <a href="{{ url }}">{{ name }}</a>
                                </li>
                            {% endif %}
                        {% endfor %}
                    </ol>
                </div>
            </nav>
        {% endblock breadcrumbs %}
        {% block bodycontent %}
        {% endblock bodycontent %}
        {% if 'message' in config %}
            <div class="global-message">
                {{ config['message'] }}
            </div>
        {% endif %}
    </body>
</html>
