{# Default way of showing errors #}
{% macro render_error(message) -%}
    <div class="ui icon message error">
        <i class="times circle icon"></i>
        <div class="content">
            <div class="header">
                Error
            </div>
            <p>{{ message }}</p>
        </div>
    </div>
{%- endmacro %}

{# Default macro for showing/wrapping the fetch results #}
{% macro render_fetch_results(content, error) -%}
    {% if error %}
        {{ render_error(error) }}
    {% else %}
        {% if content is undefined %}
            {{ render_error("Some content expected but none was found.") }}
        {% else %}
            {{ caller() }}
        {% endif %}
    {% endif %}
{%- endmacro %}

{# draw a generic pandas table#}
{% macro render_pandas_table(df, caption, column_labels={}) -%}
    {% if (df is defined) and (df is not none) %}
        <table class="display">
            <thead class="center aligned">
            <tr>
                {% for column in df.columns %}
                    {% if column in column_labels %}
                        <th>{{ column_labels[column] }}</th>
                    {% else %}
                        <th>{{ column }}</th>
                    {% endif %}
                {% endfor %}
            </tr>
            </thead>
            <tbody>
            {% for idx, row in df.iterrows() %}
                <tr>
                    {% for colname in df.columns %}
                        {# handle decimal format: float, float64, float32 #}
                        {% if 'float' in (df.dtypes[colname] | string) %}
                            <td class="center aligned collapsing">{{ row[colname] | round(precision=2) }}</td>
                        {% else %}
                            <td class="center aligned collapsing">{{ row[colname]|e }}</td>
                        {% endif %}
                    {% endfor %}
                </tr>
            {% endfor %}
            </tbody>
            <caption>{{ caption }}</caption>
        </table>
    {% else %}
        {{ render_error("How did you get here? did you forget to use 'render_fetch_results' macro?") }}
    {% endif %}
{%- endmacro %}


{% macro render_class_fingerprint(content, class_uri, graph_uri, namespaces) -%}
    {# fingerprint resultset signature #}
    {% set labels =
    {
    "subject_class" : "Subject class",
    "subject_count": "Subjects using property",
    "object_count": "Distinct values used",
    "property": "Property URI",
    "property_types": "Property kind",
    "object_types": "Value type",
    "min": "Min",
    "max": "Max",
    "avg": "Avg",} %}

{#    {% set substituted_content = replace_strings_in_tabular(content,#}
{#                                    target_columns=['property','property_types','object_types','subject_class'],#}
{#                                    value_mapping_dict=namespaces,#}
{#                                    mark_touched_rows=False ) %}#}

    {% set compress_uris = simplify_uri_columns_in_tabular(data_frame=content,namespace_inventory=namespaces,error_fail=False) %}

    {% set table_title = "The shape of " + class_uri|safe + " class instances" %}
    {{ render_pandas_table(compress_uris[["property","subject_count","min", "max", "avg", "property_types", "object_count","object_types"]], table_title, column_labels=labels) }}
{%- endmacro %}


{% macro render_namespaces(namesapces_dist) %}
    <table class="display">
        <thead class="center aligned">
        <tr>
            <th>Namespace</th>
            <th>URI</th>
        </tr>
        </thead>
        <tbody>
        {% for prefix, uri in namesapces_dist|dictsort %}
            <tr>
                <td>{{ prefix }}</td>
                <td>{{ uri }}</td>
            </tr>
        {% endfor %}
        </tbody>
        <caption>Prefixes</caption>
    </table>
{% endmacro %}