{% macro forecast_route(forecast) %}
{% if forecast.constant_values is defined %}
forecasts/cdf/{{ forecast.forecast_id }}
{% elif forecast.constant_value is defined %}
forecasts/cdf/single/{{ forecast.forecast_id }}
{% else %}
forecasts/single/{{ forecast.forecast_id }}
{% endif %}
{% endmacro %}

{% macro metric_table_fx_vert(report_metrics, category, metric_ordering) %}
<table class="table table-striped metric-table-fx-vert" style="width:100%;">
  <caption style="caption-side:top; text-align:center">
    Table of {{ category }} metrics
  </caption>
  <thead>
    <tr class="header">
      <th style="text-align: left;">Forecast</th>
      {% for metric in metric_ordering %}
        <th style="text-align: left;">{{ human_metrics[metric] }}</th>
      {% endfor %}
    </tr>
  </thead>
  <tbody>
    {% for fx_result in report_metrics %}
      <tr>
        <td>{{ fx_result.name }}</td>
        {% for metric in metric_ordering %}
          {% set ns = namespace(found=false) %}
          {% for mval in fx_result.values %}
            {% if mval.category == category  and mval.metric == metric %}
              {% set ns.found = true %}
              {% if -0.01 < mval.value < 0.01 %}
                <td>{{ '%.2e' | format(mval.value) }}</td>
              {% else %}
                <td>{{ '%0.3g' | format(mval.value) }}</td>
              {% endif %}
            {% endif %}
          {% endfor %}
          {% if not ns.found %}
            <td></td>
          {% endif %}
        {% endfor %}
      </tr>
    {% endfor %}
  </tbody>
</table>
{% endmacro %}


{% macro metric_table_fx_horz(report_metrics, category, metric_ordering) %}
{% set metric_indexes = [] %}
{% for mresult in report_metrics %}
  {% for mval in mresult.values %}
    {% if mval.category == category and mval.index not in metric_indexes %}
      {% set metric_indexes = metric_indexes.append(mval.index) %}
    {% endif %}
  {% endfor %}
{% endfor %}
<table class="table table-striped" style="width:100%;">
  <caption style="caption-side:top; text-align:center">
    Table of {{ category }} metrics
  </caption>
  <thead>
    <tr class="header">
      <th></th>
      {% for mresult in report_metrics %}
        <th colspan="{{metric_ordering|length}}" style="text-align: center;">{{ mresult.name }}</th>
      {% endfor %}
    </tr>
    <tr class="header">
      <th style="text-align: left;">{{ category }} Value</th>
      {% for mresult in report_metrics %}
        {% for metric in metric_ordering %}
          <th style="test-align: center;">{{ human_metrics[metric] }}</th>
        {% endfor %}
      {% endfor %}
    </tr>
  </thead>
  <tbody>
    {% for mindex in metric_indexes %}
      <tr>
        <td>{{ mindex }}</td>
        {% for mresult in report_metrics %}
          {% for metric in metric_ordering %}
            {% set ns = namespace(found=false) %}
            {% for mval in mresult.values %}
              {% if mval.category == category  and mval.metric == metric and mval.index == mindex %}
                {% set ns.found = true %}
                {% if -1 < mval.value < 1 %}
                  <td>{{ '%.2e' | format(mval.value) }}</td>
                {% else %}
                  <td>{{ '%0.3g' | format(mval.value) }}</td>
                {% endif %}
              {% endif %}
            {% endfor %}
            {% if not ns.found %}
              <td></td>
            {% endif %}
          {% endfor %}
        {% endfor %}
      </tr>
    {% endfor %}
  </tbody>
</table>
{% endmacro %}


{% macro validation_table(proc_fxobs_list, quality_filters) %}
<table class="table table-striped validation-table" style="width:100%;">
  <caption style="caption-side:top; text-align:center">
    Table of data validation results
  </caption>
  <thead>
    <tr class="header">
      <th style="text-align: left;">Aligned Pair</th>
      {% for proc_fxobs in proc_fxobs_list %}
      <th style="text-align: center;">
        {{ proc_fxobs.name }}
      </th>
      {% endfor %}
    </tr>
    <tr class="header">
      <th style="text-align: left;">Observation</th>
      {% for proc_fxobs in proc_fxobs_list %}
      <th style="text-align: center;">
        {{ proc_fxobs.original.data_object.name }}
      </th>
      {% endfor %}
    </tr>
  </thead>
  <tbody>
    {% for qfilter in quality_filters %}
      <tr>
      <td style="test-align: left">{{ qfilter }}</td>
      {% for proc_fxobs in proc_fxobs_list %}
        {% set ns = namespace(found=false) %}
        {% for res in proc_fxobs.validation_results %}
          {% if res.flag == qfilter %}
            {% set ns.found = true %}
            <td style="text-align: center">{{ res.count }}</td>
          {% endif %}
        {% endfor %}
        {% if not ns.found %}
          <td></td>
        {% endif %}
      {% endfor %}
      </tr>
    {% endfor %}
  </tbody>
</table>
{% endmacro %}


{% macro preprocessing_table(proc_fxobs_list) %}
{% set preproc_steps = [] %}
{% for proc_fxobs in proc_fxobs_list %}
  {% for res in proc_fxobs.preprocessing_results %}
    {% if res.name not in preproc_steps %}
      {% set preproc_steps = preproc_steps.append(res.name) %}
    {% endif %}
  {% endfor %}
{% endfor %}
<table class="table table-striped preprocessing-table" style="width:100%;">
  <caption style="caption-side:top; text-align:center">
    Table of data preprocessing results
  </caption>
  <thead>
    <tr class="header">
      <th style="text-align: left;">Preprocessing Description</th>
      {% for proc_fxobs in proc_fxobs_list %}
      <th style="text-align: center;">
        {{ proc_fxobs.name }} <br>Number of Points
      </th>
      {% endfor %}
    </tr>
  </thead>
  <tbody>
    {% for pstep in preproc_steps %}
      <tr>
      <td style="test-align: left">{{ pstep }}</td>
      {% for proc_fxobs in proc_fxobs_list %}
        {% set ns = namespace(found=false) %}
        {% for res in proc_fxobs.preprocessing_results %}
          {% if res.name == pstep %}
            {% set ns.found = true %}
            <td style="text-align: center">{{ res.count }}</td>
          {% endif %}
        {% endfor %}
        {% if not ns.found %}
          <td></td>
        {% endif %}
      {% endfor %}
      </tr>
    {% endfor %}
  </tbody>
</table>
{% endmacro %}


{% macro download_csv_script() %}
<script>
    function json_to_csv(source) {
        const columns = Object.getOwnPropertyNames(source[0])
        const nrows = source.length
        const lines = [columns.join(',')]

        for (let i = 0; i < nrows; i++) {
            let row = [];
            for (let j = 0; j < columns.length; j++) {
                const column = columns[j]
                var val = source[i][column]
                if (column == 'index' && typeof val == 'number' && val > 100000000) {
                    val = new Date(val).toISOString().split('T')[0]
                } else {
                    if(val == null){
                        val = 'nan';
                    }else{
                        val = val.toString()
                    }
                }
                row.push(val)
            }
            lines.push(row.join(','))
        }
        return lines.join('\n').concat('\n')
    }

    function download_metrics_as_csv(report_name) {
        const filename = report_name.replace(/ /g, '_') + '_metrics.csv'
        filetext = json_to_csv(metrics_json)
        const blob = new Blob([filetext], { type: 'text/csv;charset=utf-8;' })

        //addresses IE
        if (navigator.msSaveBlob) {
            navigator.msSaveBlob(blob, filename)
        } else {
            const link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.download = filename
            link.target = '_blank'
            link.style.visibility = 'hidden'
            link.dispatchEvent(new MouseEvent('click'))
        }
    }
</script>
{% endmacro %}
