<!--

    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]="axisSettingsFormGroup">
  <div class="tb-form-panel stroked tb-slide-toggle">
    <mat-expansion-panel class="tb-settings" [expanded]="alwaysExpanded || settingsExpanded"
                         [disabled]="alwaysExpanded || !axisSettingsFormGroup.get('show').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="show" (click)="$event.stopPropagation()">
            {{ 'widgets.time-series-chart.axis.show' | translate }}
          </mat-slide-toggle>
        </mat-panel-title>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <div class="tb-form-row column-xs">
          <div class="fixed-title-width" translate>widgets.time-series-chart.axis.label</div>
          <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="label" placeholder="{{ 'widget-config.set' | translate }}">
            </mat-form-field>
            <tb-font-settings formControlName="labelFont"
                              clearButton
                              disabledLineHeight
                              forceSizeUnit="px"
                              [previewText]="axisSettingsFormGroup.get('label').value">
            </tb-font-settings>
            <tb-color-input asBoxInput
                            colorClearButton
                            formControlName="labelColor">
            </tb-color-input>
          </div>
        </div>
        <div class="tb-form-row space-between column-xs">
          <div translate>widgets.time-series-chart.axis.position</div>
          <mat-form-field class="medium-width" appearance="outline" subscriptSizing="dynamic">
            <mat-select formControlName="position">
              @for (position of axisPositions; track position) {
                <mat-option [value]="position">
                  {{ timeSeriesAxisPositionTranslations.get(position) | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </div>
        @if (axisType === 'yAxis' && !hideUnits) {
          <div class="tb-form-row space-between">
            <div translate>widget-config.units-short</div>
            <tb-unit-input
              supportsUnitConversion
              formControlName="units">
            </tb-unit-input>
          </div>
        }
        @if (axisType === 'yAxis' && !hideDecimals) {
          <div class="tb-form-row space-between">
            <div translate>widget-config.decimals-short</div>
            <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
              <input matInput formControlName="decimals" type="number" min="0" max="15" step="1" placeholder="{{ 'widget-config.set' | translate }}">
            </mat-form-field>
          </div>
        }
        @if (axisType === 'yAxis' && advanced) {
          <div class="tb-form-row">
            <tb-js-func class="flex-1"
                        formControlName="ticksGenerator"
                        [globalVariables]="functionScopeVariables"
                        [functionArgs]="['extent']"
                        functionTitle="{{ 'widgets.time-series-chart.axis.ticks-generator-function' | translate }}"
                        helpId="widget/lib/chart/ticks_generator_fn">
            </tb-js-func>
          </div>
        }
        <div class="tb-form-panel no-padding stroked">
          <div class="tb-form-row no-border space-between column-xs">
            <mat-slide-toggle class="mat-slide" formControlName="showTickLabels">
              <div translate>widgets.time-series-chart.axis.tick-labels</div>
            </mat-slide-toggle>
            <div class="flex flex-row items-center justify-start gap-2">
              @if (axisType === 'xAxis') {
                <tb-auto-date-format-settings
                  formControlName="ticksFormat"
                  [defaultValues]="defaultXAxisTicksFormat">
                </tb-auto-date-format-settings>
              }
              <tb-font-settings formControlName="tickLabelFont"
                                clearButton
                                disabledLineHeight
                                forceSizeUnit="px"
                                previewText="100">
              </tb-font-settings>
              <tb-color-input asBoxInput
                              colorClearButton
                              formControlName="tickLabelColor">
              </tb-color-input>
            </div>
          </div>
          @if (axisType === 'yAxis' && advanced) {
            <div class="tb-form-row no-border">
              <tb-js-func class="flex-1"
                          formControlName="ticksFormatter"
                          [globalVariables]="functionScopeVariables"
                          [functionArgs]="['value']"
                          functionTitle="{{ 'widgets.time-series-chart.axis.ticks-formatter-function' | translate }}"
                          helpId="widget/lib/flot/ticks_formatter_fn">
              </tb-js-func>
            </div>
          }
        </div>
        <div class="tb-form-row space-between">
          <mat-slide-toggle class="mat-slide" formControlName="showTicks">
            {{ 'widgets.time-series-chart.axis.show-ticks' | translate }}
          </mat-slide-toggle>
          <tb-color-input asBoxInput
                          colorClearButton
                          formControlName="ticksColor">
          </tb-color-input>
        </div>
        <div class="tb-form-row space-between">
          <mat-slide-toggle class="mat-slide" formControlName="showLine">
            {{ 'widgets.time-series-chart.axis.show-line' | translate }}
          </mat-slide-toggle>
          <tb-color-input asBoxInput
                          colorClearButton
                          formControlName="lineColor">
          </tb-color-input>
        </div>
        <div class="tb-form-row space-between">
          <mat-slide-toggle class="mat-slide" formControlName="showSplitLines">
            <div tb-hint-tooltip-icon="{{ (axisType === 'xAxis' ? 'widgets.time-series-chart.axis.show-split-lines-x-axis-hint'
                                                                : 'widgets.time-series-chart.axis.show-split-lines-y-axis-hint') | translate}}">
              {{ 'widgets.time-series-chart.axis.show-split-lines' | translate }}
            </div>
          </mat-slide-toggle>
          <tb-color-input asBoxInput
                          colorClearButton
                          formControlName="splitLinesColor">
          </tb-color-input>
        </div>
        @if (axisType === 'yAxis') {
          <div class="tb-form-row space-between">
            <div tb-hint-tooltip-icon="{{ 'widgets.time-series-chart.axis.ticks-interval-hint' | translate }}">
              {{ 'widgets.time-series-chart.axis.ticks-interval' | translate }}
            </div>
            <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
              <input matInput formControlName="interval"
                     type="number" min="0" placeholder="{{ 'widgets.chart.chart-axis.scale-auto' | translate }}">
            </mat-form-field>
          </div>
        }
        @if (axisType === 'yAxis') {
          <div class="tb-form-row space-between">
            <div tb-hint-tooltip-icon="{{ 'widgets.time-series-chart.axis.split-number-hint' | translate }}">
              {{ 'widgets.time-series-chart.axis.split-number' | translate }}
            </div>
            <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
              <input matInput formControlName="splitNumber"
                     type="number" min="1" placeholder="{{ 'widgets.chart.chart-axis.scale-auto' | translate }}">
            </mat-form-field>
          </div>
        }
        @if (axisType === 'yAxis') {
          <div class="tb-form-panel stroked">
            <div class="flex flex-col gap-2">
              <div translate>widgets.chart.chart-axis.scale-limits</div>
              <div class="tb-form-table">
                <div class="tb-form-table-header">
                  <div class="tb-form-table-header-cell" [class]="alwaysExpanded ? 'basis-10' : 'basis-1/5'" translate>widgets.chart.chart-axis.limit</div>
                  <div class="tb-form-table-header-cell flex-grow flex-shrink" [class]="alwaysExpanded ? 'basis-1/2' : 'basis-40%'" translate>widgets.chart.chart-axis.source</div>
                  <div class="tb-form-table-header-cell flex-grow flex-shrink" [class]="alwaysExpanded ? 'basis-1/2' : 'basis-40%'" translate>widgets.chart.chart-axis.key-value</div>
                </div>
                <div class="tb-form-table-body">
                  <tb-axis-scale-row
                    [isPanelView]="alwaysExpanded"
                    [datasource]="datasource"
                    [callbacks]="dataKeyCallbacks"
                    [aliasController]="aliasController"
                    formControlName="min"
                    labelKey="widgets.chart.chart-axis.scale-min">
                  </tb-axis-scale-row>
                  <mat-divider class="mt-2 gt-sm:hidden"></mat-divider>
                  <tb-axis-scale-row
                    [isPanelView]="alwaysExpanded"
                    [datasource]="datasource"
                    [callbacks]="dataKeyCallbacks"
                    [aliasController]="aliasController"
                    formControlName="max"
                    labelKey="widgets.chart.chart-axis.scale-max">
                  </tb-axis-scale-row>
                </div>
              </div>
            </div>
          </div>
        }
      </ng-template>
    </mat-expansion-panel>
  </div>
</ng-container>
