<!--

    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-aggregated-value-card-panel" [style.padding]="padding" [style]="backgroundStyle$ | async">
  <div class="tb-aggregated-value-card-overlay" [style]="overlayStyle"></div>
  <div class="tb-aggregated-value-card-title-panel">
    <ng-container *ngTemplateOutlet="widgetTitlePanel"></ng-container>
    <ng-container *ngTemplateOutlet="subtitleTpl"></ng-container>
  </div>
  @if (showValues) {
    <ng-container *ngTemplateOutlet="valuesTpl"></ng-container>
  }
  <ng-container *ngTemplateOutlet="chartTpl"></ng-container>
  <ng-container *ngTemplateOutlet="dateTpl"></ng-container>
</div>

<ng-template #subtitleTpl>
  @if (showSubtitle) {
    <div class="tb-aggregated-value-card-subtitle"
    [style]="subtitleStyle" [style.color]="subtitleColor">{{ subtitle$ | async }}</div>
  }
</ng-template>
<ng-template #valuesTpl>
  <div #valueCardValues class="tb-aggregated-value-card-values">
    <div #valueCardValueContainer class="tb-aggregated-value-card-values-container">
      <div class="tb-aggregated-value-card-values-section left">
        @if (values[aggregatedValueCardKeyPosition.leftTop]) {
          <ng-container *ngTemplateOutlet="valueTpl; context:{value: values[aggregatedValueCardKeyPosition.leftTop]}"></ng-container>
        }
        @if (values[aggregatedValueCardKeyPosition.leftBottom]) {
          <ng-container *ngTemplateOutlet="valueTpl; context:{value: values[aggregatedValueCardKeyPosition.leftBottom]}"></ng-container>
        }
      </div>
      <div class="tb-aggregated-value-card-values-section center">
        @if (values[aggregatedValueCardKeyPosition.center]) {
          <ng-container *ngTemplateOutlet="valueTpl; context:{value: values[aggregatedValueCardKeyPosition.center]}"></ng-container>
        }
      </div>
      <div class="tb-aggregated-value-card-values-section right">
        @if (values[aggregatedValueCardKeyPosition.rightTop]) {
          <ng-container *ngTemplateOutlet="valueTpl; context:{value: values[aggregatedValueCardKeyPosition.rightTop]}"></ng-container>
        }
        @if (values[aggregatedValueCardKeyPosition.rightBottom]) {
          <ng-container *ngTemplateOutlet="valueTpl; context:{value: values[aggregatedValueCardKeyPosition.rightBottom]}"></ng-container>
        }
      </div>
    </div>
  </div>
</ng-template>
<ng-template #chartTpl>
  @if (showChart) {
    <div class="tb-aggregated-value-card-chart">
      <div class="tb-aggregated-value-card-chart-ticks">
        <div>{{tickMax$ | async}}</div>
        <div>{{tickMin$ | async}}</div>
      </div>
      <div class="tb-aggregated-value-card-chart-container">
        <div class="tb-aggregated-value-card-chart-element" #chartElement></div>
        <div class="tb-aggregated-value-card-chart-boundary top left"></div>
        <div class="tb-aggregated-value-card-chart-boundary top right"></div>
        <div class="tb-aggregated-value-card-chart-boundary bottom left"></div>
        <div class="tb-aggregated-value-card-chart-boundary bottom right"></div>
      </div>
    </div>
  }
</ng-template>
<ng-template #dateTpl>
  @if (showDate) {
    <div [style]="dateStyle" [style.color]="dateColor" [innerHTML]="dateFormat.formatted"></div>
  }
</ng-template>
<ng-template #valueTpl let-value="value">
  <div class="tb-aggregated-value-card-value" [style]="value.style" [style.color]="value.color.color">
    @if (value.showArrow) {
      <div class="value-arrow-container">
        @if (!value.upArrow && !value.downArrow) {
          <span class="value-arrow"></span>
        }
        @if (value.upArrow) {
          <tb-icon class="value-arrow">arrow_upward</tb-icon>
        }
        @if (value.downArrow) {
          <tb-icon class="value-arrow">arrow_downward</tb-icon>
        }
      </div>
    }
    <div class="value-text">
      <span>{{ value.value }}</span>
      <span class="units" [class]="{'small': value.center}">{{ value.units }}</span>
    </div>
  </div>
</ng-template>
