<!DOCTYPE html>
<html>
<body>
    <div class="navbar-header" style = " background-color: rgb(255, 255, 255); margin:0; padding:0;">
        <img src = "https://github.com/eclipse/kiso-testing/raw/master/docs/images/pykiso_logo.png" alt = "Pykiso report" width="15%" height="15%">
    </div>
    <div class="button-container">
        <button id="failButton">Toggle failed tests</button>
        <button id="allButton">Toggle all tests</button>
    </div>
    {# For each TestClass -#}
    {% for class_name, class_content in ALL_STEP_REPORT.items() -%}
        <hr>
        {% if class_content["succeed"] -%}
                <details class="hide-marker col-12 text-left">
        {% else -%}
                <details open class="hide-marker col-12 text-left failed-test-details">
        {%- endif %}
            {# Add Title and description -#}
            <summary style="list-style: none;"><h2>ITF Test Report for:  <a href="{{class_content["file_path"]}}">{{class_name}}</a>
            {% if class_content["succeed"] -%}
                -> [<span style="color:green;">Success</span>]
            {% else -%}
                -> [<span style="color:red;">Fail</span>]
            {%- endif %}
            </h2></summary>
            <h3>Test Description:</h3>
            <p>{{class_content["description"] | replace("\n", "<br/>\n") }}</p>
            <h3>Date, Time, Software versions:</h3>
            <p>
            {# Add additional information from header key -#}
            {% for data in [class_content["time_result"], class_content["header"]] -%}
                {% for key, value in data.items() -%}
                    <u>{{key}}</u>: {{value}}</br>
                {%- endfor %}
            {%- endfor %}
            </p>
            {#- For each test (setUp, run, tearDown) -#}
            {% for test_name, test_content in class_content["test_list"].items() %}
                {% set test_success = is_test_success(test_content) -%}
                <details {% if not test_success %}open class="failed-test-details"{% endif %}><summary><h3>{{test_name}}
                {% if test_success -%}
                    <span style="color:green;">Success
                    {% if test_content.get("number_try") -%}
                        (attempt: {{test_content.get("number_try")}}/{{test_content.get("max_try")}})
                    {%- endif %}
                    </span>
                {% else %}
                    <span style="color:red;">Fail
                    {% if test_content.get("number_try") -%}
                        ({{test_content.get("number_try")}} attempts made)
                    {%- endif %}
                    </span>
                {%- endif %}
                </h3></summary>
                <p>{{test_content["description"] | replace("\n", "<br/>\n") }}</p>
                {# Loop over the number of try of a test -#}
                {% for index in range(test_content["steps"] |length) -%}
                    {# Check if we are in a case of a retry -#}
                    {% if test_content["steps"] | length >1 and index == 0 -%}
                       <details><summary><b>Attempt details</b></summary>
                    {%- endif %}
                    {% if test_content["steps"] | length >1 -%}
                        <b>Attempt {{index+1}} :</b>
                        <br>
                    {%- endif %}
                    <table>
                        <thead>
                            <tr>
                                {% if test_content["steps"][index] | length >0 -%}
                                    {# Set Columns name from the first row, excluding succeed flag -#}
                                    <th scope="col" style="width: 3%">Step</th>
                                    {% for column_name in test_content["steps"][index][0].keys() if column_name != "succeed" -%}
                                        <th scope="col">{{column_name}}</th>
                                    {%- endfor %}
                                {%- endif %}
                            </tr>
                        </thead>
                        <tbody>
                            {#- Loop over each assert method called (step-report) -#}
                            {% for row in test_content["steps"][index] -%}
                                {# Set cell color variable according to the assert result and emptiness -#}
                                {% if row.pop("succeed") == true -%}
                                    {% set color_cell = "background-color: rgb(196, 243, 196);" -%}
                                    {% set color_empty_cell = "background-color: rgb(250, 250, 128);" -%}
                                {% else -%}
                                    {% set color_cell = "background-color: rgb(236, 160, 160);" -%}
                                    {% set color_empty_cell = "background-color: rgb(236, 160, 160);" -%}
                                {%- endif %}
                                {# Needed for setting unique class per row in html in inner loop -#}
                                {% set row_index = loop.index -%}
                                <tr>
                                    <th scope="row" style="{{color_cell}}" >{{loop.index}}</th>
                                    {#- Loop over each cell of the row -#}
                                    {% for col_value in row.values() -%}
                                        {#- Set the value and apply colors to the cell #}
                                        <td {% if (col_value or col_value == False) %} style="{{color_cell}}" {% else %} style="{{color_empty_cell}}" {% endif %}>
                                            {# Use a <details> if content is too long for better results overview and synchronize toggling them per row -#}
                                            {% if col_value | string | length > 100 -%}
                                            <details class="{{class_name}}{{test_name}}row_{{row_index}}" ontoggle="toggleDetailsInRow(this, '{{class_name}}{{test_name}}row_{{row_index}}')">
                                                <summary>{{col_value | string | truncate(53, true, leeway = 0)}}</summary>
                                                <br>
                                                <div>
                                                    {{col_value}}
                                                </div>
                                            </details>
                                            {%- else -%}
                                            <div>
                                                {{col_value}}
                                            </div>
                                            {%- endif %}
                                        </td>
                                    {%- endfor %}
                                </tr>
                            {%- endfor %}

                        </tbody>
                    </table>
                    {% if test_content["unexpected_errors"][index] -%}
                    <b style='color:red;'><strong>Unexpected errors happened :</strong> <br></b>
                        {% for error in test_content.get("unexpected_errors")[index] -%}
                            <pre>{{error}}</pre>
                        {%- endfor %}

                    {%- endif %}
                    {% if (test_content["steps"] | length >1 and (index+2 == (test_content["steps"] | length))) -%}
                            </details>
                    {%- endif %}
                {%- endfor %}

                </details>
            {%- endfor %}

        </details>
    {%- endfor %}
</body>
</html>

<style type="text/css">
{% include "templates/report_template.css" %}
</style>

<script>
{% include "templates/report_template_script.js" %}
</script>
