{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% macro operationLine(key, operation, actualOperationName, n) %}
    {% set context_id = 'operation-' ~ n %}
    <tr>
        <th style="width: 40%" scope="row"{% if key == actualOperationName %} class="status-success"{% endif %}>{{ key }}</th>
        <td style="width: 60%" {% if key == actualOperationName %} class="status-success"{% endif %}>
          <a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide context">Show context</a>
          <div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
              {{- profiler_dump(operation, 2) -}}
          </div>
        </td>
    </tr>
{% endmacro %}

{% macro operationTable(object, actualOperationName) %}
    {% import _self as apiPlatform %}
    <table>
        <thead>
            <tr>
                <th scope="col" class="key">{% if name is defined %}{{ name|capitalize }}{% endif %}Name</th>
                <th scope="col">Operation</th>
            </tr>
        </thead>

        <tbody>
        {% set n = 0 %}
        {% for key, itemOperation in object %}
          {% set n = n + 1 %}
          {{ apiPlatform.operationLine(key, itemOperation, actualOperationName, n) }}
        {% else %}
            <tr>
                <td colspan="2" class="text-muted">
                    No available {{ name|lower }} operation for this resource.
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endmacro %}

{% macro providerTable(object, name) %}
    {% if object.responses is empty %}
        <div class="empty">
            <p>No calls to {{ name }} have been recorded.</p>
        </div>
    {% else %}
        <table>
            <thead>
            <tr>
                <th>#</th>
                <th>Used</th>
                <th>{{ name|capitalize }}</th>
            </tr>
            </thead>
            <tbody>
            {% for class, used in object.responses %}
                <tr>
                    <td class="font-normal text-small text-muted nowrap">{{ loop.index }}</td>
                    <td class="font-normal">
                        {% if used is same as(true) %}
                            <span class="label status-success same-width">TRUE</span>
                        {% elseif used is same as(false) %}
                            <span class="label status-error same-width">FALSE</span>
                        {% else %}
                            <span class="label status-info same-width">NOT USED</span>
                        {% endif %}
                    </td>
                    <td class="font-normal">{{ class }}</td>
                </tr>
            {% endfor %}
            </tbody>
            {% if object.context is defined %}
                <tfoot>
                    <tr>
                        <td class="font-normal" colspan="2"></td>
                        <td class="font-normal">Context{{ profiler_dump(object.context, 2) }}</td>
                    </tr>
                </tfoot>
            {% endif %}
        </table>
    {% endif %}
{% endmacro %}

{% import _self as apiPlatform %}

{% block toolbar %}
    {% set icon %}
        {% set status_color = collector.counters.ignored_filters|default(false) ? 'yellow' : 'default' %}
        {{ include('@ApiPlatform/DataCollector/api-platform-icon.svg') }}
    {% endset %}

    {% set text %}
        {% if collector.resources|length == 0 %}
            <div class="sf-toolbar-info-piece">
                <b>Resource Class</b>
                <span>Not an API Platform resource</span>
            </div>
        {% endif %}
        {% for resource in collector.resources %}
            <div class="sf-toolbar-info-piece">
                <b>Resource Class</b>
                <span>{{ resource.resourceClass }}</span>
            </div>
            {% if resource.counters.ignored_filters|default(false) %}
                <div class="sf-toolbar-info-piece">
                    <b>Ignored Filters</b>
                    <span class="sf-toolbar-status sf-toolbar-status-yellow">{{ collector.counters.ignored_filters }}</span>
                </div>
            {% endif %}
        {% endfor %}
    {% endset %}

    {{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { 'link': true, status: status_color }) }}
{% endblock %}

{% block menu %}
    {# This left-hand menu appears when using the full-screen profiler. #}
    <span class="label{{ collector.resources ? '' : ' disabled' }}">
        <span class="icon">
            {{ include('@ApiPlatform/DataCollector/api-platform.svg') }}
        </span>
        <strong>API Platform</strong>
    </span>
{% endblock %}

{% block panel %}
    {% if collector.resources|length == 0 %}
        <div class="metrics">
            <div class="metric">
                <span class="value">Not an API Platform resource</span>
                <span class="label">Resource Class</span>
            </div>
        </div>
    {% endif %}
    {% for dataCollected in collector.resources %}
        <div class="metrics">
            <div class="metric">
                <span class="value">{{ dataCollected.resourceClass|default('Not an API Platform resource') }}</span>
                <span class="label">Resource Class</span>
            </div>
        </div>
        {% if dataCollected.resourceMetadataCollection is not empty %}
            <h2>Resources</h2>
            <div class="tab-content metadata-tab-content">
                <div class="sf-tabs">
                    {% for index, resourceMetadata in dataCollected.resourceMetadataCollection %}
                        {% set context_id = 'context-' ~ resourceMetadata.resource.shortName ~ index %}
                        <div class="tab">
                            <h3 class="tab-title">
                                {{ resourceMetadata.resource.uriTemplate ?: resourceMetadata.resource.shortName }}
                            </h3>
                            <div class="tab-content">
                                <table>
                                    <thead>
                                        <tr>
                                            <th scope="col" class="key">Name</th>
                                            <th>ApiResource</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                          <td>{{ resourceMetadata.resource.shortName }}</td>
                                            <td>
                                              <div>
                                                  <a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide context">Show context</a>
                                                  <div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
                                                      {{- profiler_dump(resourceMetadata.resource, 2) -}}
                                                  </div>
                                              </div>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                                <h2>Operations</h2>
                                {{ apiPlatform.operationTable(resourceMetadata.operations, collector.operationName) }}
                                <h2>Parameters</h2>
                                <table>
                                    <thead>
                                        <tr>
                                            <th scope="col" class="key">Key</th>
                                            <th scope="col">Value</th>
                                            <th scope="col">Parameter</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% if collector.parameters %}
                                            {% for key, parameter in collector.parameters %}
                                                {% set context_id = 'parameter-' ~ key %}
                                                <tr>
                                                    <td>
                                                        {{ key }}
                                                    </td>
                                                    <td>
                                                        {% if parameter.extraProperties['_api_values'] is defined %}
                                                            {{ dump(parameter.extraProperties['_api_values']) }}
                                                        {% else %}
                                                            ∅
                                                        {% endif %}
                                                    </td>
                                                    <td>
                                                        <a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide context">Show context</a>
                                                        <div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
                                                          {{- profiler_dump(parameter, 3) -}}
                                                        </div>

                                                    </td>
                                                </tr>
                                            {% endfor %}
                                        {% else %}
                                            <tr>
                                                <td class="text-muted" colspan="2">
                                                    No available parameters declared for this resource.
                                                </td>
                                            </tr>
                                        {% endif %}
                                    </tbody>
                                </table>
                                <h2>Filters</h2>
                                <table>
                                    <thead>
                                        <tr>
                                            <th scope="col" class="key">Filters</th>
                                            <th scope="col"></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% if dataCollected.filters and loop.index0 in dataCollected.filters|keys %}
                                            {% for id, filter in dataCollected.filters[loop.index0] %}
                                                {% set ignored_filter = filter is same as(null) %}
                                                <tr{% if ignored_filter %} class="status-warning"{% endif %}>
                                                    <td>
                                                        {{ id }}
                                                        {% if ignored_filter %}
                                                            <span class="newline text-muted">ignored filter</span>
                                                        {% else %}
                                                            {{ filter }}
                                                        {% endif %}
                                                    </td>
                                                </tr>
                                            {% endfor %}
                                        {% else %}
                                            <tr>
                                                <td class="text-muted" colspan="2">
                                                    No available filter declared for this resource.
                                                </td>
                                            </tr>
                                        {% endif %}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    {% endfor %}
                </div>
            </div>
        {% endif %}
    {% endfor %}
{% endblock %}
