<!--

    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-widget-config tb-absolute-fill">
  @if (!hideHeader) {
    <div class="tb-widget-config-header">
      @if (!hideToggleHeader && widgetConfigMode === widgetConfigModes.advanced) {
        <tb-toggle-select
          appearance="fill" [options]="headerOptions" [(ngModel)]="selectedOption">
        </tb-toggle-select>
      }
      <div class="tb-widget-config-header-components">
        <ng-content select=".tb-widget-config-header-prefix"></ng-content>
        <ng-content select=".tb-widget-config-header-suffix"></ng-content>
      </div>
    </div>
  }
  @if (widgetConfigMode === widgetConfigModes.advanced) {
    <div class="tb-widget-config-content">
      @if (displayData) {
        <div [class.!hidden]="selectedOption !== 'data'" class="mat-content">
          <ng-container *ngTemplateOutlet="data"></ng-container>
        </div>
      }
      <div [class.!hidden]="selectedOption !== 'appearance'" class="mat-content">
        <ng-container *ngTemplateOutlet="appearance"></ng-container>
      </div>
      <div [class.!hidden]="selectedOption !== 'card'" [formGroup]="widgetSettings" class="mat-content">
        <div class="tb-form-panel">
          <div class="tb-form-panel-title" translate>widget-config.card-title</div>
          <mat-slide-toggle class="mat-slide" formControlName="showTitle">
            {{ 'widget-config.display-title' | translate }}
          </mat-slide-toggle>
          <div class="tb-form-row">
            <div class="fixed-title-width" translate>widget-config.title</div>
            <div class="flex flex-1 flex-row items-center justify-start gap-2">
              @if (widgetType === widgetTypes.static || widgetType === widgetTypes.alarm) {
                <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
                  <input matInput formControlName="title" placeholder="{{ 'widget-config.set' | translate }}">
                </mat-form-field>
              } @else {
                <tb-string-pattern-autocomplete class="flex-1"
                                                appearance="outline"
                                                brackets="curly"
                                                subscriptSizing="dynamic"
                                                [predefinedValuesButton]="true"
                                                [disabled]="!widgetSettings.get('showTitle').value"
                                                [predefinedValues]="predefinedValues"
                                                placeholderText="{{ 'widget-config.set' | translate }}"
                                                formControlName="title"
                ></tb-string-pattern-autocomplete>
              }
              <tb-font-settings formControlName="titleFont"
                                clearButton
                                [previewText]="widgetSettings.get('title').value"
                                [initialPreviewStyle]="widgetSettings.get('titleStyle').value">
              </tb-font-settings>
              <tb-color-input asBoxInput
                              colorClearButton
                              formControlName="titleColor">
              </tb-color-input>
            </div>
          </div>
          <div class="tb-form-row">
            <div class="fixed-title-width" translate>widget-config.title-tooltip</div>
            <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
              <input matInput formControlName="titleTooltip" placeholder="{{ 'widget-config.set' | translate }}">
            </mat-form-field>
          </div>
          <div class="tb-form-row space-between column-xs">
            <mat-slide-toggle class="mat-slide"  formControlName="showTitleIcon">
              {{ 'widget-config.display-icon' | translate }}
            </mat-slide-toggle>
            <div class="flex flex-row items-center justify-start gap-2">
              <mat-form-field appearance="outline" subscriptSizing="dynamic">
                <input matInput formControlName="iconSize">
              </mat-form-field>
              <tb-material-icon-select asBoxInput
                                       iconClearButton
                                       allowedCustomIcon
                                       [color]="widgetSettings.get('iconColor').value"
                                       formControlName="titleIcon">
              </tb-material-icon-select>
              <tb-color-input asBoxInput
                              colorClearButton
                              formControlName="iconColor">
              </tb-color-input>
            </div>
          </div>
          <mat-expansion-panel class="tb-settings">
            <mat-expansion-panel-header>
              <mat-panel-description class="flex items-stretch justify-start" translate>
                widget-config.advanced-title-style
              </mat-panel-description>
            </mat-expansion-panel-header>
            <ng-template matExpansionPanelContent>
              <tb-json-object-edit
                [editorStyle]="{minHeight: '100px'}"
                label="{{ 'widget-config.title-style' | translate }}"
                formControlName="titleStyle"
              ></tb-json-object-edit>
            </ng-template>
          </mat-expansion-panel>
        </div>
        <div class="tb-form-panel">
          <div class="tb-form-panel-title" translate>widget-config.card-style</div>
          <div class="tb-form-row space-between">
            <div>{{ 'widget-config.text-color' | translate }}</div>
            <tb-color-input asBoxInput
                            colorClearButton
                            formControlName="color">
            </tb-color-input>
          </div>
          <div class="tb-form-row space-between">
            <div>{{ 'widget-config.background-color' | translate }}</div>
            <tb-color-input asBoxInput
                            colorClearButton
                            formControlName="backgroundColor">
            </tb-color-input>
          </div>
          <div class="tb-form-row space-between">
            <div>{{ 'widget-config.padding' | translate }}</div>
            <mat-form-field appearance="outline" subscriptSizing="dynamic">
              <input matInput formControlName="padding" placeholder="{{ 'widget-config.set' | translate }}">
            </mat-form-field>
          </div>
          <div class="tb-form-row space-between">
            <div>{{ 'widget-config.margin' | translate }}</div>
            <mat-form-field appearance="outline" subscriptSizing="dynamic">
              <input matInput formControlName="margin" placeholder="{{ 'widget-config.set' | translate }}">
            </mat-form-field>
          </div>
          <div class="tb-form-row space-between">
            <div>{{ 'widget-config.border-radius' | translate }}</div>
            <mat-form-field appearance="outline" subscriptSizing="dynamic">
              <input matInput formControlName="borderRadius" placeholder="{{ 'widget-config.set' | translate }}">
            </mat-form-field>
          </div>
          <mat-slide-toggle class="mat-slide slide-block" formControlName="dropShadow">
            {{ 'widget-config.drop-shadow' | translate }}
          </mat-slide-toggle>
          <mat-expansion-panel class="tb-settings">
            <mat-expansion-panel-header>
              <mat-panel-description class="flex items-stretch justify-start" translate>
                widget-config.advanced-widget-style
              </mat-panel-description>
            </mat-expansion-panel-header>
            <ng-template matExpansionPanelContent>
              <tb-json-object-edit
                [editorStyle]="{minHeight: '100px'}"
                label="{{ 'widget-config.widget-style' | translate }}"
                formControlName="widgetStyle"
              ></tb-json-object-edit>
              <tb-css
                label="{{ 'widget-config.widget-css' | translate }}"
                formControlName="widgetCss"
              ></tb-css>
            </ng-template>
          </mat-expansion-panel>
        </div>
        <div class="tb-form-panel">
          <div class="tb-form-panel-title" translate>widget-config.card-buttons</div>
          <mat-slide-toggle class="mat-slide slide-block" formControlName="enableFullscreen">
            {{ 'widget-config.enable-fullscreen' | translate }}
          </mat-slide-toggle>
        </div>
      </div>
      <div [class.!hidden]="selectedOption !== 'actions'" [formGroup]="actionsSettings" class="mat-content" style="height: 100%;">
        <div class="tb-form-panel" style="height: 100%;">
          <tb-manage-widget-actions
            [callbacks]="widgetConfigCallbacks"
            [widgetType] = "modelValue.widgetType"
            [defaultIconColor]="widgetSettings.get('color').value"
            [actionSources]="modelValue.actionSources"
            [additionalWidgetActionTypes]="modelValue.typeParameters?.additionalWidgetActionTypes"
            formControlName="actions">
          </tb-manage-widget-actions>
        </div>
      </div>
      <div [class.!hidden]="selectedOption !== 'layout'" [formGroup]="layoutSettings" class="mat-content">
        @if (!showLayoutConfig || isDefaultBreakpoint) {
          <div class="tb-form-panel">
            <div class="tb-form-panel-title" translate>widget-config.resize-options</div>
            <div class="tb-form-row">
              <mat-slide-toggle class="mat-slide" formControlName="resizable">
                {{ 'widget-config.resizable' | translate }}
              </mat-slide-toggle>
            </div>
            <div class="tb-form-row">
              <mat-slide-toggle class="mat-slide" formControlName="preserveAspectRatio">
                {{ 'widget-config.preserve-aspect-ratio' | translate }}
              </mat-slide-toggle>
            </div>
          </div>
        }
        @if (showLayoutConfig) {
          <div class="tb-form-panel">
            <div class="tb-form-panel-title">{{ (isDefaultBreakpoint ? 'widget-config.mobile' : 'widget-config.list-layout') | translate }}</div>
            @if (isDefaultBreakpoint) {
              <div class="tb-form-row">
                <mat-slide-toggle class="mat-slide" formControlName="mobileHide">
                  {{ 'widget-config.mobile-hide' | translate }}
                </mat-slide-toggle>
              </div>
            }
            @if (isDefaultBreakpoint) {
              <div class="tb-form-row">
                <mat-slide-toggle class="mat-slide" formControlName="desktopHide">
                  {{ 'widget-config.desktop-hide' | translate }}
                </mat-slide-toggle>
              </div>
            }
            <div class="tb-form-row space-between">
              <div translate>widget-config.order</div>
              <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
                <input matInput formControlName="mobileOrder" type="number" step="1" placeholder="{{ 'widget-config.set' | translate }}">
              </mat-form-field>
            </div>
            <div class="tb-form-row space-between">
              <div translate>widget-config.height</div>
              <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
                <input matInput formControlName="mobileHeight" type="number" min="1" step="1" placeholder="{{ 'widget-config.set' | translate }}">
              </mat-form-field>
            </div>
          </div>
        }
      </div>
    </div>
  } @else {
    <div class="tb-widget-config-content">
      @if (hasBasicModeDirective) {
        <div class="mat-content">
          <ng-container #basicModeContainer></ng-container>
          @if (basicModeDirectiveError) {
            <div class="tb-basic-mode-directive-error">{{basicModeDirectiveError}}</div>
          }
        </div>
      } @else {
        <div class="mat-content">
          <ng-container *ngTemplateOutlet="data"></ng-container>
          <ng-container *ngTemplateOutlet="appearance"></ng-container>
          <ng-container [formGroup]="actionsSettings">
            <tb-widget-actions-panel
              formControlName="actions">
            </tb-widget-actions-panel>
          </ng-container>
        </div>
      }
    </div>
  }
  <ng-template #data>
    @if (displayTimewindowConfig) {
      <ng-container [formGroup]="dataSettings">
        <tb-timewindow-config-panel
          [onlyHistoryTimewindow]="onlyHistoryTimewindow()"
          formControlName="timewindowConfig">
        </tb-timewindow-config-panel>
      </ng-container>
    }
    @if (widgetType === widgetTypes.alarm) {
      <div [formGroup]="dataSettings" class="tb-form-panel">
        <div class="flex flex-row items-center justify-between">
          <div class="tb-form-panel-title" translate>alarm.filter</div>
          <button mat-button color="primary"
                  (click)="alarmFilterConfig.reset()">
            {{ 'action.reset' | translate }}
          </button>
        </div>
        <tb-alarm-filter-config #alarmFilterConfig buttonMode="false"
        formControlName="alarmFilterConfig" [initialAlarmFilterConfig]="{}"></tb-alarm-filter-config>
      </div>
    }
    @if (widgetType !== widgetTypes.rpc &&
      widgetType !== widgetTypes.alarm &&
      modelValue?.isDataEnabled) {
      <div
        [formGroup]="dataSettings">
        <tb-datasources
          [configMode]="widgetConfigMode"
          formControlName="datasources">
        </tb-datasources>
      </div>
    }
    @if (widgetType === widgetTypes.rpc &&
      modelValue?.isDataEnabled) {
      <ng-container [formGroup]="targetDeviceSettings">
        <tb-target-device formControlName="targetDevice" appearance="outline">
        </tb-target-device>
      </ng-container>
    }
    @if (widgetType === widgetTypes.alarm && modelValue?.isDataEnabled) {
      <div
        [formGroup]="dataSettings" class="tb-form-panel">
        <div class="tb-form-panel-title" translate>widget-config.alarm-source</div>
        <tb-datasource
          formControlName="alarmSource">
        </tb-datasource>
      </div>
    }
    @if (displayLimits) {
      <div class="tb-form-panel" [formGroup]="widgetSettings">
        <div class="tb-form-panel-title" translate>widget-config.limits</div>
        <div class="tb-form-row space-between column-xs">
          <div translate>widget-config.data-page-size</div>
          <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
            <input matInput formControlName="pageSize" type="number" min="1" step="1">
          </mat-form-field>
        </div>
      </div>
    }
  </ng-template>
  <ng-template #appearance>
    @if (!displayData && displayTimewindowConfig) {
      <ng-container [formGroup]="dataSettings">
        <tb-timewindow-config-panel
          [onlyHistoryTimewindow]="onlyHistoryTimewindow()"
          formControlName="timewindowConfig">
        </tb-timewindow-config-panel>
      </ng-container>
    }
    @if (displayAppearanceDataSettings) {
      <div class="tb-form-panel" [formGroup]="widgetSettings">
        <div class="tb-form-panel-title" translate>widget-config.data-settings</div>
        @if (displayUnitsConfig) {
          <div class="tb-form-row space-between">
            <div tb-hint-tooltip-icon="{{'widget-config.default-data-key-parameter-hint' | translate}}" translate>widget-config.units-by-default</div>
            <tb-unit-input
              [supportsUnitConversion]="modelValue.typeParameters.supportsUnitConversion"
              formControlName="units">
            </tb-unit-input>
          </div>
        }
        @if (displayUnitsConfig) {
          <div class="tb-form-row space-between">
            <div tb-hint-tooltip-icon="{{'widget-config.default-data-key-parameter-hint' | translate}}" translate>widget-config.decimals-by-default</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 (displayNoDataDisplayMessageConfig) {
          <div class="tb-form-row column-xs">
            <div class="fixed-title-width" translate>widget-config.no-data-display-message</div>
            <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
              <input matInput formControlName="noDataDisplayMessage" placeholder="{{ 'widget-config.set-message' | translate }}">
            </mat-form-field>
          </div>
        }
      </div>
    }
    @if (displayAdvancedAppearance) {
      <div class="flex-1" [formGroup]="advancedSettings">
        <tb-widget-settings
            [aliasController]="aliasController"
            [callbacks]="widgetConfigCallbacks"
            [functionsOnly]="functionsOnly"
            [dashboard]="dashboard"
            [widget]="widget"
            [widgetConfig]="modelValue"
            formControlName="settings">
        </tb-widget-settings>
      </div>
    }
  </ng-template>
</div>

