{% macro git_material_link(material) -%}
    <a href="{{ material.link }}">{{ material.name }}</a>
{%- endmacro %}

{% macro git_modification_link(modification, material) -%}
    <small>(<a href="{{ modification.gh_link(material) }}"><code>{{ modification.revision|truncate(7, true, '') }}</code></a>)</small>
{%- endmacro %}

{% macro pipeline_material_link(pipeline_material) -%}
    <a href="{{ pipeline_material.pipeline.value_stream_map }}">{{ pipeline_material.pipeline.title }}</a>
{%- endmacro %}

{% macro pipeline_stage_link(pipeline, stage) -%}
    <a href="{{ stage.link(pipeline) }}" class="stage">{{ stage.name }}/{{ stage.counter }}</a>
{%- endmacro %}

{% macro pipeline(pipeline, group_passed=False) %}
    <div class="card {% if pipeline.failed %}red{% elif pipeline.running %}yellow{% elif group_passed %}green{% endif %} lighten-4">
        <div class="card-content">
            <span class="card-title">
                <a href="{{ pipeline.value_stream_map }}">{{ pipeline.title }}</a>
            </span>

            <p>
            {%- if pipeline.git_material and not pipeline.pipeline_material %}
                Built changes from {{ git_material_link(pipeline.git_material) }} {{ git_modification_link(pipeline.git_material.modifications[0], pipeline.git_material) }}.
            {%- elif pipeline.pipeline_material and not pipeline.git_material %}
                Triggered by {{ pipeline_material_link(pipeline.pipeline_material) }}.
            {%- elif pipeline.pipeline_materials %}
                Triggered by <a href="{{ pipeline.value_stream_map }}">{{ pipeline.pipeline_materials|length }} pipelines</a>.
            {% else %}all
                No materials changed.
            {%- endif %}
            </p>

            <p>
            {%- if pipeline.passed %}
                All stages passed.
            {%- elif pipeline.running %}
                Currently running stage {{ pipeline_stage_link(pipeline, pipeline.running_stage) }}.
            {%- elif pipeline.failed %}
                Failed on stage {{ pipeline_stage_link(pipeline, pipeline.failed_stage) }}.
            {%- endif %}
            </p>
        </div>

        <div class="card-action">
            {% for name, email in pipeline.all_commit_authors()|sort %}
                <div class="chip">
                    <img src="{{ email | gravatar }}" alt="{{ email }}" title="{{ name }} <{{ email }}>">
                    {{ name }}
                </div>
            {% else %}
                <div class="chip">
                    <img src="{{ url_for('static', filename='logos/gocd-avatar.png') }}" alt="GoCD">
                    Trigged manually
                </div>
            {% endfor %}
        </div>
    </div>
{% endmacro %}
