<!--

    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.

-->
<div class="tb-time-series-chart-panel" [style.padding]="padding" [style]="backgroundStyle$ | async">
  <div class="tb-time-series-chart-overlay" [style]="overlayStyle"></div>
  @if (widgetComponent.dashboardWidget.showWidgetTitlePanel) {
    <div class="tb-widget-title-row flex justify-between">
      <ng-container *ngTemplateOutlet="widgetTitlePanel || widgetComponent.widgetTitlePanel"></ng-container>
      <ng-container *ngTemplateOutlet="widgetComponent.widgetHeaderActionsPanel"></ng-container>
    </div>
  } @else {
    <ng-container class="tb-widget-actions-absolute" *ngTemplateOutlet="widgetComponent.widgetHeaderActionsPanel; context: {absolute: true}"></ng-container>
  }
  <div class="tb-time-series-chart-content" [class]="legendClass">
    <div #chartShape class="tb-time-series-chart-shape">
    </div>
    @if (showLegend) {
      <div class="tb-time-series-chart-legend" [class.tb-simple-legend]="!displayLegendValues">
        @if (displayLegendValues) {
          @if (horizontalLegendPosition) {
            <ng-container *ngTemplateOutlet="horizontalLegend"></ng-container>
          } @else {
            <table class="tb-time-series-chart-legend-table vertical">
              <thead>
                <tr>
                  <th></th>
                  @if (legendConfig.showMin === true) {
                    <th class="tb-time-series-chart-legend-type-label right" [style]="legendColumnTitleStyle">{{ 'legend.Min' | translate }}</th>
                  }
                  @if (legendConfig.showMax === true) {
                    <th class="tb-time-series-chart-legend-type-label right" [style]="legendColumnTitleStyle">{{ 'legend.Max' | translate }}</th>
                  }
                  @if (legendConfig.showAvg === true) {
                    <th class="tb-time-series-chart-legend-type-label right" [style]="legendColumnTitleStyle">{{ 'legend.Avg' | translate }}</th>
                  }
                  @if (legendConfig.showTotal === true) {
                    <th class="tb-time-series-chart-legend-type-label right" [style]="legendColumnTitleStyle">{{ 'legend.Total' | translate }}</th>
                  }
                  @if (legendConfig.showLatest === true) {
                    <th class="tb-time-series-chart-legend-type-label right" [style]="legendColumnTitleStyle">{{ 'legend.Latest' | translate }}</th>
                  }
                </tr>
              </thead>
              <tbody>
                @for (legendKey of legendKeys; track legendKey) {
                  <tr>
                    <th>
                      <ng-container *ngTemplateOutlet="legendItem; context:{legendKey: legendKey, left: true}"></ng-container>
                    </th>
                    @if (legendConfig.showMin === true) {
                      <td class="tb-time-series-chart-legend-value" [style]="legendValueStyle"
                          [innerHTML]="legendData.data[legendKey.dataIndex].min | safe: 'html'">
                      </td>
                    }
                    @if (legendConfig.showMax === true) {
                      <td class="tb-time-series-chart-legend-value" [style]="legendValueStyle"
                          [innerHTML]="legendData.data[legendKey.dataIndex].max | safe: 'html'">
                      </td>
                    }
                    @if (legendConfig.showAvg === true) {
                      <td class="tb-time-series-chart-legend-value" [style]="legendValueStyle"
                          [innerHTML]="legendData.data[legendKey.dataIndex].avg | safe: 'html'">
                      </td>
                    }
                    @if (legendConfig.showTotal === true) {
                      <td class="tb-time-series-chart-legend-value" [style]="legendValueStyle"
                          [innerHTML]="legendData.data[legendKey.dataIndex].total | safe: 'html'">
                      </td>
                    }
                    @if (legendConfig.showLatest === true) {
                      <td class="tb-time-series-chart-legend-value" [style]="legendValueStyle"
                          [innerHTML]="legendData.data[legendKey.dataIndex].latest | safe: 'html'">
                      </td>
                    }
                  </tr>
                }
              </tbody>
            </table>
          }
        } @else {
          @for (legendKey of legendKeys; track legendKey) {
            <ng-container *ngTemplateOutlet="legendItem; context:{legendKey: legendKey}"></ng-container>
          }
        }
      </div>
    }
    <ng-template #horizontalLegend>
      <table class="tb-time-series-chart-legend-table">
        <thead>
          <tr>
            <th></th>
            @for (legendKey of legendKeys; track legendKey) {
              <th>
                <ng-container *ngTemplateOutlet="legendItem; context:{legendKey: legendKey}"></ng-container>
              </th>
            }
          </tr>
        </thead>
        <tbody>
          @if (legendConfig.showMin === true) {
            <ng-container *ngTemplateOutlet="legendDataRow; context:{label: 'legend.Min', type: 'min'}"></ng-container>
          }
          @if (legendConfig.showMax === true) {
            <ng-container *ngTemplateOutlet="legendDataRow; context:{label: 'legend.Max', type: 'max'}"></ng-container>
          }
          @if (legendConfig.showAvg === true) {
            <ng-container *ngTemplateOutlet="legendDataRow; context:{label: 'legend.Avg', type: 'avg'}"></ng-container>
          }
          @if (legendConfig.showTotal === true) {
            <ng-container *ngTemplateOutlet="legendDataRow; context:{label: 'legend.Total', type: 'total'}"></ng-container>
          }
          @if (legendConfig.showLatest === true) {
            <ng-container *ngTemplateOutlet="legendDataRow; context:{label: 'legend.Latest', type: 'latest'}"></ng-container>
          }
        </tbody>
      </table>
    </ng-template>
    <ng-template #legendItem let-legendKey="legendKey" let-left="left">
      <div class="tb-time-series-chart-legend-item"
           [class.left]="left">
        <div class="tb-time-series-chart-legend-item-label"
             (mouseenter)="onLegendKeyEnter(legendKey)"
             (mouseleave)="onLegendKeyLeave(legendKey)"
             (click)="toggleLegendKey(legendKey)">
          <div class="tb-time-series-chart-legend-item-label-circle" [style]="{background: !legendKey.dataKey.hidden ? legendKey.dataKey.color : null}"></div>
          <div [style]="!legendKey.dataKey.hidden ? legendLabelStyle : disabledLegendLabelStyle" [innerHTML]="legendKey.dataKey.label | safe: 'html'"></div>
        </div>
      </div>
    </ng-template>
    <ng-template #legendDataRow let-label="label" let-type="type">
      <tr>
        <th class="tb-time-series-chart-legend-type-label" [style]="legendColumnTitleStyle">{{ label | translate }}</th>
        @for (legendKey of legendKeys; track legendKey) {
          <td class="tb-time-series-chart-legend-value" [style]="legendValueStyle"
              [innerHTML]="legendData.data[legendKey.dataIndex][type] | safe: 'html'">
          </td>
        }
      </tr>
    </ng-template>
  </div>
</div>
