<!--

    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.

-->
<ng-container [formGroup]="digitalGaugeWidgetSettingsForm">
  <div class="tb-form-panel">
    <div class="tb-form-panel-title" translate>widgets.gauge.gauge-appearance</div>
    <tb-image-cards-select rowHeight="1:1"
                           [cols]="{columns: 4,
                                    breakpoints: {
                                      'lt-md': 2
                                    }}"
      label="{{ 'widgets.gauge.simple-gauge-type' | translate }}"
      formControlName="gaugeType">
      @for (layout of digitalGaugeLayouts; track layout) {
        <tb-image-cards-select-option
          [value]="layout"
          [image]="digitalGaugeLayoutImageMap.get(layout)">
          {{ digitalGaugeLayoutTranslationMap.get(layout) | translate }}
        </tb-image-cards-select-option>
      }
    </tb-image-cards-select>

    @if (digitalGaugeWidgetSettingsForm.get('gaugeType').value === digitalGaugeType.donut) {
      <div class="tb-form-row space-between">
        <div class="fixed-title-width">{{ 'widgets.gauge.donut-start-angle' | translate }}</div>
        <mat-form-field appearance="outline" subscriptSizing="dynamic">
          <input matInput formControlName="donutStartAngle" type="number" min="0" max="360" step="1" placeholder="{{ 'widget-config.set' | translate }}">
        </mat-form-field>
      </div>
    }

    <div class="tb-form-row space-between column-xs">
      <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showMinMax">
        {{ 'widgets.gauge.min-and-max-label' | translate }}
      </mat-slide-toggle>
      <div class="flex flex-row items-center justify-start gap-2">
        <tb-font-settings formControlName="minMaxFont"
                          disabledLineHeight
                          [previewText]="previewFn">
        </tb-font-settings>
        <tb-color-input asBoxInput
                        colorClearButton
                        formControlName="minMaxColor">
        </tb-color-input>
      </div>
    </div>

    <div class="tb-form-row space-between column-xs">
      <div class="fixed-title-width">{{ 'widgets.gauge.min-and-max-value' | translate }}</div>
      <div class="flex flex-row items-center justify-start gap-2">
        <div class="tb-small-label" translate>widgets.gauge.min-value-short</div>
        <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
          <input matInput formControlName="minValue" type="number" step="1" placeholder="{{ 'widget-config.set' | translate }}">
        </mat-form-field>
        <div class="tb-small-label" translate>widgets.gauge.max-value-short</div>
        <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
          <input matInput formControlName="maxValue" type="number" step="1" placeholder="{{ 'widget-config.set' | translate }}">
          @if (digitalGaugeWidgetSettingsForm.get('maxValue').hasError('maxValue')) {
            <mat-icon matSuffix
                      matTooltipPosition="above"
                      matTooltipClass="tb-error-tooltip"
                      [matTooltip]="'widgets.gauge.min-max-validation-text' | translate"
                      class="material-icons tb-suffix-show-always tb-error">
              warning
            </mat-icon>
          }
        </mat-form-field>
      </div>
    </div>

    <div class="tb-form-row space-between column-xs">
      <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showValue">
        {{ 'widgets.gauge.value' | translate }}
      </mat-slide-toggle>
      <div class="flex flex-row items-center justify-start gap-2">
        <tb-font-settings formControlName="valueFont"
                          disabledLineHeight
                          [previewText]="valuePreviewFn">
        </tb-font-settings>
        <tb-color-input asBoxInput
                        colorClearButton
                        formControlName="valueColor">
        </tb-color-input>
      </div>
    </div>

    <div class="tb-form-row">
      <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showTitle">
        {{ 'widgets.gauge.label' | translate }}
      </mat-slide-toggle>
      <div class="flex flex-1 flex-row items-center justify-start gap-2">
        <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
          <input matInput formControlName="title" placeholder="{{ 'widget-config.set' | translate }}">
        </mat-form-field>
        <tb-font-settings formControlName="titleFont"
                          [previewText]="digitalGaugeWidgetSettingsForm.get('title').value">
        </tb-font-settings>
        <tb-color-input formControlName="titleColor"
                        asBoxInput
                        colorClearButton>
        </tb-color-input>
      </div>
    </div>

    <div class="tb-form-panel stroked">
      <div class="tb-form-panel-title" translate>widgets.gauge.unit-title-and-timestamp-settings</div>
      <div class="tb-form-row">
        <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showUnitTitle">
          {{ 'widgets.gauge.show-unit-title' | translate }}
        </mat-slide-toggle>
        <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
          <input matInput formControlName="unitTitle" placeholder="{{ 'widget-config.set' | translate }}">
        </mat-form-field>
      </div>

      <div class="tb-form-row">
        <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="showTimestamp">
          {{ 'widgets.gauge.show-timestamp' | translate }}
        </mat-slide-toggle>
        <tb-date-format-select class="flex-1" excludeLastUpdateAgo formControlName="timestampFormat"></tb-date-format-select>
      </div>

      <div class="tb-form-row space-between column-xs">
        <div class="fixed-title-width">{{ 'widgets.gauge.font' | translate }}</div>
        <div class="flex flex-row items-center justify-start gap-2">
          <tb-font-settings formControlName="labelFont"
                            disabledLineHeight
                            [previewText]="valuePreviewFn">
          </tb-font-settings>
          <tb-color-input asBoxInput
                          colorClearButton
                          formControlName="labelColor">
          </tb-color-input>
        </div>
      </div>
    </div>
  </div>

  <div class="tb-form-panel">
    <div class="tb-form-panel-title" translate>widgets.gauge.bar-settings</div>
    <div class="tb-form-row space-between">
      <div class="fixed-title-width">{{ 'widgets.gauge.relative-bar-width' | translate }}</div>
      <mat-form-field appearance="outline" subscriptSizing="dynamic">
        <input matInput type="number" min="0" formControlName="gaugeWidthScale" placeholder="{{ 'widget-config.set' | translate }}">
      </mat-form-field>
    </div>

    <div class="tb-form-row space-between">
      <div tb-hint-tooltip-icon="{{'widgets.gauge.neon-glow-brightness-hint' | translate}}" class="fixed-title-width">{{ 'widgets.gauge.neon-glow-brightness' | translate }}</div>
      <mat-form-field appearance="outline" subscriptSizing="dynamic">
        <input matInput type="number" min="0" max="100" formControlName="neonGlowBrightness" placeholder="{{ 'widget-config.set' | translate }}">
      </mat-form-field>
    </div>

    <div class="tb-form-row space-between">
      <div tb-hint-tooltip-icon="{{'widgets.gauge.stripes-thickness-hint' | translate}}" class="fixed-title-width">{{ 'widgets.gauge.stripes-thickness' | translate }}</div>
      <mat-form-field appearance="outline" subscriptSizing="dynamic">
        <input matInput type="number" min="0" formControlName="dashThickness" placeholder="{{ 'widget-config.set' | translate }}">
      </mat-form-field>
    </div>

    <div class="tb-form-row">
      <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="roundedLineCap">
        {{ 'widgets.gauge.rounded-line-cap' | translate }}
      </mat-slide-toggle>
    </div>

    <div class="tb-form-row space-between">
      <div class="fixed-title-width">{{ 'widgets.gauge.gauge-bar-background' | translate }}</div>
      <tb-color-input asBoxInput
                      colorClearButton
                      formControlName="gaugeColor">
      </tb-color-input>
    </div>

    <div class="tb-form-row space-between">
      <div class="fixed-title-width">{{ 'widgets.gauge.bar-color' | translate }}</div>
      <tb-color-settings formControlName="barColor"
                         rangeAdvancedMode
                         gradientAdvancedMode
                         [minValue]="digitalGaugeWidgetSettingsForm.get('minValue').value"
                         [maxValue]="digitalGaugeWidgetSettingsForm.get('maxValue').value"
                         [aliasController]="aliasController"
                         [dataKeyCallbacks]="dataKeyCallbacks"
                         [datasource]="datasource"
                         settingsKey="{{'widgets.battery-level.value' | translate }}">
      </tb-color-settings>
    </div>

  </div>

  <div class="tb-form-panel tb-slide-toggle">
    <mat-expansion-panel class="tb-settings" [expanded]="digitalGaugeWidgetSettingsForm.get('showTicks').value">
      <mat-expansion-panel-header class="flex flex-row flex-wrap">
        <mat-panel-title>
          <mat-slide-toggle class="mat-slide flex items-stretch justify-center" formControlName="showTicks" (click)="$event.stopPropagation()">
            {{ 'widgets.gauge.ticks' | translate }}
          </mat-slide-toggle>
        </mat-panel-title>
        <mat-panel-description class="flex max-w-40% flex-full items-center justify-end xs:!hidden" translate>
          widget-config.advanced-settings
        </mat-panel-description>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <div class="tb-form-row space-between column-xs">
          <div class="fixed-title-width">{{ 'widgets.gauge.tick-width-and-color' | translate }}</div>
          <div class="flex flex-row items-center justify-start gap-2">
            <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
              <input matInput type="number" min="0" formControlName="tickWidth" placeholder="{{ 'widget-config.set' | translate }}">
            </mat-form-field>
            <tb-color-input asBoxInput
                            colorClearButton
                            formControlName="colorTicks">
            </tb-color-input>
          </div>
        </div>
        <div class="tb-form-panel stroked">
          <div class="tb-form-panel-title" translate>widgets.gauge.tick-values</div>
          <div class="tb-control-list tb-drop-list" cdkDropList cdkDropListOrientation="vertical"
               (cdkDropListDropped)="tickValueDrop($event)">
            @for (tickValueControl of tickValuesFormArray().controls; track trackByTickValue($index, tickValueControl); let isLast = $last) {
              <div cdkDrag class="tb-draggable flex flex-row" [class.pb-2]="!isLast">
                <tb-tick-value class="flex-1"
                               [formControl]="tickValueControl"
                               [aliasController]="aliasController"
                               [dataKeyCallbacks]="dataKeyCallbacks"
                               [datasource]="datasource"
                               (removeTickValue)="removeTickValue($index)">
                </tb-tick-value>
                <button class="tb-box-button lt-lg:!hidden"
                        mat-icon-button
                        type="button"
                        cdkDragHandle
                        matTooltip="{{ 'action.drag' | translate }}"
                        matTooltipPosition="above">
                  <mat-icon>drag_indicator</mat-icon>
                </button>
              </div>
            }
          </div>
          @if (!tickValuesFormArray().controls.length) {
            <div>
              <span translate class="tb-prompt flex items-center justify-center">widgets.gauge.no-tick-values</span>
            </div>
          }
          <div style="padding-top: 16px;">
            <button mat-stroked-button color="primary"
                    type="button"
                    [disabled]="!digitalGaugeWidgetSettingsForm.get('showTicks').value"
                    (click)="addTickValue()">
              <span translate>widgets.gauge.add-tick-value</span>
            </button>
          </div>
        </div>
      </ng-template>
    </mat-expansion-panel>
  </div>

  <div class="tb-form-panel tb-slide-toggle">
    <mat-expansion-panel class="tb-settings" [expanded]="digitalGaugeWidgetSettingsForm.get('animation').value">
      <mat-expansion-panel-header class="flex flex-row flex-wrap">
        <mat-panel-title>
          <mat-slide-toggle class="mat-slide flex items-stretch justify-center" formControlName="animation" (click)="$event.stopPropagation()">
            {{ 'widgets.gauge.enable-animation' | translate }}
          </mat-slide-toggle>
        </mat-panel-title>
        <mat-panel-description class="flex max-w-40% flex-full items-center justify-end xs:!hidden" translate>
          widget-config.advanced-settings
        </mat-panel-description>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <div class="tb-form-row space-between">
          <div translate>widgets.gauge.animation-duration-rule</div>
          <div class="flex flex-row items-center justify-start gap-2">
            <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
              <input matInput type="number" min="0" formControlName="animationDuration" placeholder="{{ 'widget-config.set' | translate }}">
            </mat-form-field>
            <mat-form-field appearance="outline" subscriptSizing="dynamic">
              <mat-select formControlName="animationRule">
                <mat-option [value]="'linear'">
                  {{ 'widgets.gauge.animation-linear' | translate }}
                </mat-option>
                <mat-option [value]="'quad'">
                  {{ 'widgets.gauge.animation-quad' | translate }}
                </mat-option>
                <mat-option [value]="'quint'">
                  {{ 'widgets.gauge.animation-quint' | translate }}
                </mat-option>
                <mat-option [value]="'cycle'">
                  {{ 'widgets.gauge.animation-cycle' | translate }}
                </mat-option>
                <mat-option [value]="'bounce'">
                  {{ 'widgets.gauge.animation-bounce' | translate }}
                </mat-option>
                <mat-option [value]="'elastic'">
                  {{ 'widgets.gauge.animation-elastic' | translate }}
                </mat-option>
                <mat-option [value]="'dequad'">
                  {{ 'widgets.gauge.animation-dequad' | translate }}
                </mat-option>
                <mat-option [value]="'dequint'">
                  {{ 'widgets.gauge.animation-dequint' | translate }}
                </mat-option>
                <mat-option [value]="'decycle'">
                  {{ 'widgets.gauge.animation-decycle' | translate }}
                </mat-option>
                <mat-option [value]="'debounce'">
                  {{ 'widgets.gauge.animation-debounce' | translate }}
                </mat-option>
                <mat-option [value]="'delastic'">
                  {{ 'widgets.gauge.animation-delastic' | translate }}
                </mat-option>
              </mat-select>
            </mat-form-field>
          </div>
        </div>
      </ng-template>
    </mat-expansion-panel>
  </div>
</ng-container>
