{% set LAYOUT = 'full-width' %}
{% extends "base.html" %}


{# Map URL substring matches to link's icon, then label.
Sorted by priority: the first items takes precedence over those at the end. #}
{% set ICONS = [
    ('github.com',                'fab fa-github-alt',     'Git repository'),
    ('bitbucket.org',             'fab fa-bitbucket',      'Mercurial repository'),
    ('youtube.com',               'fas fa-play',           'Play video'),
    ('ohloh.net',                 'fas fa-bar-chart-o',    'Contribution statistics'),
    ('stackoverflow.com',         'fab fa-stack-overflow', 'Community support'),
    ('stackexchange.com',         'fab fa-stack-exchange', 'Community support'),
    ('twitter.com',               'fab fa-twitter',        'Social media activities'),
    ('bugs.launchpad.net',        'fas fa-bug',            'Reported bugs'),
    ('trac.wordpress.org',        'fas fa-bug',            'Reported bugs'),
    ('sourceforge.net/apps/trac', 'fas fa-bug',            'Reported bugs'),
    ('bugs.debian.org',           'fas fa-bug',            'Reported bugs'),
    ('bugs.kde.org',              'fas fa-bug',            'Reported bugs'),
    ('mantis',                    'fas fa-bug',            'Reported bugs'),
    ('ticket=on',                 'fas fa-bug',            'Reported bugs'),
    ('gmane.org',                 'fas fa-envelope-o',     'Mailing-list participation'),
    ('type_of_search=mlists',     'fas fa-envelope-o',     'Mailing-list participation'),
    (SITEURL,                     'fas fa-bullhorn',       'Announcement on this blog'),
    ]
%}


{% macro render_card(all_tags, project) %}
<div class="card">
    {% if project.inactive %}
        <div class="corner-banner shadow-sm bg-danger text-white font-weight-bold">Inactive</div>
    {% endif %}
    {% if project.thumb %}
        {% if project.thumb_link %}
            <a href="{{ project.thumb_link }}">
        {% endif %}
        <img class="card-img-top" alt="{{ project.name }} thumbnail" src="{{ project.thumb }}"/>
        {% if project.thumb_link %}
            </a>
        {% endif %}
    {% endif %}
    <div class="card-body">
        <h4 class="card-title">{{ project.name }}</h4>
        {% if project.roles or project.tools %}
            <p class="card-text">
                {% for role in project.roles %}
                    <span class="badge badge-success">{{ role }}</span>
                {% endfor %}
                {# Sort by tag frequency, then alphabetically #}
                {% for tag in all_tags|sort(attribute='name')|sort(reverse=True, attribute='articles') %}
                    {% if tag.name in project.tools %}
                        {{ m.render_tag(tag) }}
                    {% endif %}
                {% endfor %}
                {% set tag_names = [] %}
                {% for tag in all_tags %}
                    {% if tag_names.append(tag.name) %}{% endif %}
                {% endfor %}
                {% for tool in project.tools %}
                    {% if tool not in tag_names %}
                        <span class="badge badge-secondary">{{ tool }}</span>
                    {% endif %}
                {% endfor %}
            </p>
        {% endif %}
        {% if project.desc %}
            <p class="card-text">{{ project.desc }}</p>
        {% endif %}
        {% if project.links %}
            <p class="card-text">
                {% for link in project.links %}
                    {# Default icon in case of no match #}
                    {% set matches = [('', 'fas fa-home', 'Project home')] %}
                    {% for rule in ICONS|reverse %}
                        {% if rule[0] and link.find(rule[0]) != -1 %}
                            {% if matches.append(rule) %}{% endif %}
                        {% endif %}
                    {% endfor %}
                    <a href="{{ link }}" title="{{ matches[-1][2]|e }}">
                        <span class="fa-stack" style="vertical-align: top;">
                            <i class="fas fa-circle fa-stack-2x"></i>
                            <i class="{{ matches[-1][1] }} fa-stack-1x fa-inverse"></i>
                        </span>
                    </a>
                {% endfor %}
            </p>
        {% endif %}
    </div>
</div>
{% endmacro %}


{% macro render_projects(projects) %}

{% set all_tags = [] %}
{% for tag, articles in tags %}
    {% if all_tags.append({'name': tag.name, 'url': tag.url, 'articles': articles|length()}) %}{% endif %}
{% endfor %}

{% if projects is defined %}
    <div class="card-columns">
        {% block project_pre_content %}{% endblock %}

        {% set active_projects = [] %}
        {% set inactive_projects = [] %}
        {% for project in projects %}
            {% if project.inactive %}
                {% if inactive_projects.append(project) %}{% endif %}
            {% else %}
                {% if active_projects.append(project) %}{% endif %}
            {% endif %}
        {% endfor %}

        {% for project_list in [active_projects, inactive_projects] %}
            {% for project in project_list %}
                {{ render_card(all_tags, project) }}
            {% endfor %}
        {% endfor %}

        {% block project_post_content %}{% endblock %}
    </div>
{% endif %}

{% endmacro %}


{% macro render_project_group(group, projects) %}
{{ group.pre }}
{{ render_projects(projects) }}
{{ group.post }}
{% endmacro %}


{% block content %}
    {% if PROJECT_GROUPS is defined %}
        {% for group in PROJECT_GROUPS %}
            {{ render_project_group(group, PROJECTS|selectattr("group", "sameas", group.id)) }}
        {% endfor %}

        {{ render_projects(PROJECTS|rejectattr("group", "defined")) }}

    {% else %}
        {{ render_projects(PROJECTS) }}
    {% endif %}
{% endblock %}
