<!--

    Copyright © 2016-2026 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<table class="tb-legend" [class.tb-legend-row]="isRowDirection" [class.tb-legend-column]="!isRowDirection">
  <thead>
    @if (!isRowDirection) {
      <tr class="tb-legend-header">
        <th colspan="2"></th>
        @if (legendConfig.showMin === true) {
          <th>{{ 'legend.min' | translate }}</th>
        }
        @if (legendConfig.showMax === true) {
          <th>{{ 'legend.max' | translate }}</th>
        }
        @if (legendConfig.showAvg === true) {
          <th>{{ 'legend.avg' | translate }}</th>
        }
        @if (legendConfig.showTotal === true) {
          <th>{{ 'legend.total' | translate }}</th>
        }
        @if (legendConfig.showLatest === true) {
          <th>{{ 'legend.latest' | translate }}</th>
        }
      </tr>
    }
  </thead>
  <tbody>
    @if (!isRowDirection) {
      @for (legendKey of legendKeys(); track legendKey) {
        <tr class="tb-legend-keys">
          <td><span class="tb-legend-line" [style.background-color]="legendKey.dataKey.color"></span></td>
          <td class="tb-legend-label"
              (click)="toggleHideData(legendKey.dataIndex)"
              [innerHTML]="legendKey.dataKey.label | safe: 'html'"
              [class.tb-hidden-label]="legendKey.dataKey.hidden"
              [class.tb-horizontal]="isHorizontal">
          </td>
          @if (legendConfig.showMin === true) {
            <td class="tb-legend-value"
            [innerHTML]="legendData.data[legendKey.dataIndex].min | safe: 'html'"></td>
          }
          @if (legendConfig.showMax === true) {
            <td class="tb-legend-value"
            [innerHTML]="legendData.data[legendKey.dataIndex].max | safe: 'html'"></td>
          }
          @if (legendConfig.showAvg === true) {
            <td class="tb-legend-value"
            [innerHTML]="legendData.data[legendKey.dataIndex].avg | safe: 'html'"></td>
          }
          @if (legendConfig.showTotal === true) {
            <td class="tb-legend-value"
            [innerHTML]="legendData.data[legendKey.dataIndex].total | safe: 'html'"></td>
          }
          @if (legendConfig.showLatest === true) {
            <td class="tb-legend-value"
            [innerHTML]="legendData.data[legendKey.dataIndex].latest | safe: 'html'"></td>
          }
        </tr>
      }
    }
    @if (isRowDirection) {
      <tr class="tb-legend-keys" [class.tb-row-direction]="!displayHeader">
        <td></td>
        @for (legendKey of legendKeys(); track legendKey) {
          <td>
            <span class="tb-legend-line" [style.background-color]="legendKey.dataKey.color"></span>
            <span class="tb-legend-label"
                  [innerHTML]="legendKey.dataKey.label | safe: 'html'"
                  (click)="toggleHideData(legendKey.dataIndex)"
                  [class.tb-hidden-label]="legendKey.dataKey.hidden">
            </span>
          </td>
        }
      </tr>
    }
    @if (isRowDirection && legendConfig.showMin === true) {
      <tr class="tb-legend-keys">
        <td class="tb-legend-type">{{ 'legend.min' | translate }}</td>
        @for (legendKey of legendKeys(); track legendKey) {
          <td class="tb-legend-value"
          [innerHTML]="legendData.data[legendKey.dataIndex].min | safe: 'html'"></td>
        }
      </tr>
    }
    @if (isRowDirection && legendConfig.showMax === true) {
      <tr class="tb-legend-keys">
        <td class="tb-legend-type">{{ 'legend.max' | translate }}</td>
        @for (legendKey of legendKeys(); track legendKey) {
          <td class="tb-legend-value"
          [innerHTML]="legendData.data[legendKey.dataIndex].max | safe: 'html'"></td>
        }
      </tr>
    }
    @if (isRowDirection && legendConfig.showAvg === true) {
      <tr class="tb-legend-keys">
        <td class="tb-legend-type">{{ 'legend.avg' | translate }}</td>
        @for (legendKey of legendKeys(); track legendKey) {
          <td class="tb-legend-value"
          [innerHTML]="legendData.data[legendKey.dataIndex].avg | safe: 'html'"></td>
        }
      </tr>
    }
    @if (isRowDirection && legendConfig.showTotal === true) {
      <tr class="tb-legend-keys">
        <td class="tb-legend-type">{{ 'legend.total' | translate }}</td>
        @for (legendKey of legendKeys(); track legendKey) {
          <td class="tb-legend-value"
          [innerHTML]="legendData.data[legendKey.dataIndex].total | safe: 'html'"></td>
        }
      </tr>
    }
    @if (isRowDirection && legendConfig.showLatest === true) {
      <tr class="tb-legend-keys">
        <td class="tb-legend-type">{{ 'legend.latest' | translate }}</td>
        @for (legendKey of legendKeys(); track legendKey) {
          <td class="tb-legend-value"
          [innerHTML]="legendData.data[legendKey.dataIndex].latest | safe: 'html'"></td>
        }
      </tr>
    }
  </tbody>
</table>
