<!--

    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-action-settings-panel" [formGroup]="getValueSettingsFormGroup">
  <div class="tb-action-settings-title">{{ panelTitle }}</div>
  <div class="tb-action-settings-panel-content">
    <div class="tb-form-row">
      <div class="fixed-title-width" >{{ 'widgets.value-action.action' | translate }}</div>
      <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
        <mat-select formControlName="action">
          @for (action of getValueActions; track action) {
            <mat-option [value]="action">
              {{ getValueActionTranslationsMap.get(action) | translate }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
    </div>
    @switch (getValueSettingsFormGroup.get('action').value) {
      @case (getValueAction.DO_NOTHING) {
        <div class="tb-form-row space-between">
          <div tb-hint-tooltip-icon="{{'widgets.value-action.init-value-hint' | translate}}" translate>widgets.value-action.value</div>
          <tb-value-input [valueType]="valueType"
                          [trueLabel]="trueLabel"
                          [falseLabel]="falseLabel"
          formControlName="defaultValue"></tb-value-input>
        </div>
      }
      @case (getValueAction.EXECUTE_RPC) {
        <div class="tb-form-row" formGroupName="executeRpc">
          <div class="fixed-title-width" >{{ 'widgets.value-action.method' | translate }}*</div>
          <mat-form-field class="tb-suffix-absolute flex" appearance="outline" subscriptSizing="dynamic">
            <input matInput required formControlName="method" placeholder="{{ 'widget-config.set' | translate }}">
            @if (getValueSettingsFormGroup.get('executeRpc').get('method').hasError('required')
              && getValueSettingsFormGroup.get('executeRpc').get('method').touched) {
              <mat-icon matSuffix
                        matTooltipPosition="above"
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="'widgets.value-action.method-name-required' | translate"
                        class="tb-error">
                warning
              </mat-icon>
            }
          </mat-form-field>
        </div>
      }
      @case (getValueAction.GET_ATTRIBUTE) {
        <ng-container formGroupName="getAttribute">
          <div class="tb-form-row">
            <div class="fixed-title-width">{{ 'widgets.value-action.attribute-scope' | translate }}</div>
            <mat-form-field class="flex" appearance="outline" subscriptSizing="dynamic">
              <mat-select formControlName="scope" placeholder="{{ 'attribute.scope-any' | translate }}">
                <mat-option [value]="null">
                  {{ 'attribute.scope-any' | translate }}
                </mat-option>
                @for (scope of attributeScopes; track scope) {
                  <mat-option [value]="scope">
                    {{ telemetryTypeTranslationsMap.get(scope) | translate }}
                  </mat-option>
                }
              </mat-select>
            </mat-form-field>
          </div>
          <div class="tb-form-row">
            <div class="fixed-title-width" >{{ 'widgets.value-action.attribute-key' | translate }}*</div>
            <tb-device-key-autocomplete
              class="flex-1"
              formControlName="key"
              required
              requiredText="widgets.value-action.attribute-key-required"
              inlineField
              [aliasController]="aliasController"
              [targetDevice]="targetDevice"
              [keyType]="dataKeyType.attribute"
              [attributeScope]="getValueSettingsFormGroup.get('getAttribute').get('scope').value">
            </tb-device-key-autocomplete>
          </div>
        </ng-container>
      }
      @case (getValueAction.GET_TIME_SERIES) {
        <ng-container formGroupName="getTimeSeries">
          <div class="tb-form-row">
            <div class="fixed-title-width" >{{ 'widgets.value-action.time-series-key' | translate }}*</div>
            <tb-device-key-autocomplete
              class="flex-1"
              formControlName="key"
              required
              requiredText="widgets.value-action.time-series-key-required"
              inlineField
              [aliasController]="aliasController"
              [targetDevice]="targetDevice"
              [keyType]="dataKeyType.timeseries">
            </tb-device-key-autocomplete>
          </div>
        </ng-container>
      }
      @case (getValueAction.GET_ALARM_STATUS) {
        <ng-container formGroupName="getAlarmStatus">
          <div class="tb-form-row space-between column-xs">
            <div class="fixed-title-width" translate>alarm.alarm-severity</div>
            <mat-chip-listbox multiple formControlName="severityList">
              @for (alarmSeverity of alarmSeverities; track alarmSeverity) {
                <mat-chip-option [value]="alarmSeverity">
                  {{ alarmSeverityTranslationMap.get(alarmSeverity) | translate }}
                </mat-chip-option>
              }
            </mat-chip-listbox>
          </div>
          <div class="tb-form-row column-xs">
            <div class="fixed-title-width" translate>alarm.alarm-types</div>
            <tb-entity-subtype-list subscriptSizing="dynamic"
                                    formControlName="typeList"
                                    appearance="outline"
                                    class="flex-1"
                                    [additionalClasses]="['tb-chips', 'flex']"
                                    [entityType]="entityType.ALARM">
            </tb-entity-subtype-list>
          </div>
        </ng-container>
      }
    }
    @if (getValueSettingsFormGroup.get('action').value !== getValueAction.DO_NOTHING &&
      getValueSettingsFormGroup.get('action').value !== getValueAction.GET_ALARM_STATUS) {
      <div
        class="tb-form-panel stroked" formGroupName="dataToValue">
        <div class="tb-form-row no-padding no-border column-xs">
          <div class="fixed-title-width" translate>widgets.value-action.action-result-converter</div>
          <tb-toggle-select formControlName="type"
                            class="flex-1"
                            [class.invisible]="getValueSettingsFormGroup.get('action').value === getValueAction.GET_DASHBOARD_STATE_OBJECT">
            <tb-toggle-option [value]="dataToValueType.NONE">{{ 'widgets.value-action.converter-none' | translate }}</tb-toggle-option>
            <tb-toggle-option [value]="dataToValueType.FUNCTION">{{ 'widgets.value-action.converter-function' | translate }}</tb-toggle-option>
          </tb-toggle-select>
        </div>
        @if (getValueSettingsFormGroup.get('dataToValue').get('type').value === dataToValueType.FUNCTION) {
          <tb-js-func
            formControlName="dataToValueFunction"
            withModules
            required
            [globalVariables]="functionScopeVariables"
            [functionArgs]="['data']"
            functionTitle="{{ 'widgets.value-action.parse-value-function' | translate }}"
            [helpId]="getParseValueFunctionHelpId()">
          </tb-js-func>
        }
        @if (valueType === ValueType.BOOLEAN) {
          <div class="tb-form-row align-start no-gap column-xs">
            <div class="fixed-title-width fixed-title-height">{{ 'widgets.value-action.state-when-result-is' | translate:{state: stateLabel} }}</div>
            <tb-value-input
              class="flex-1"
          [valueType]="(getValueSettingsFormGroup.get('action').value === getValueAction.GET_DASHBOARD_STATE &&
                        getValueSettingsFormGroup.get('dataToValue').get('type').value === dataToValueType.NONE) ? ValueType.STRING : null"
              layout="column"
              formControlName="compareToValue">
            </tb-value-input>
          </div>
        }
      </div>
    }
    @if (getValueSettingsFormGroup.get('action').value === getValueAction.EXECUTE_RPC) {
      <div
        class="tb-form-panel no-border tb-slide-toggle" formGroupName="executeRpc">
        <mat-expansion-panel class="tb-settings no-padding">
          <mat-expansion-panel-header class="flex flex-row flex-wrap">
            <mat-panel-title>
            </mat-panel-title>
            <mat-panel-description class="flex items-center justify-end" translate>
              widget-config.advanced-settings
            </mat-panel-description>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            <div class="tb-form-row space-between">
              <div>{{ 'widgets.value-action.request-timeout-ms' | translate }}</div>
              <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
                <input matInput formControlName="requestTimeout" type="number" required min="5000" step="1" placeholder="{{ 'widget-config.set' | translate }}">
                @if (getValueSettingsFormGroup.get('executeRpc').get('requestTimeout').touched &&
                  getValueSettingsFormGroup.get('executeRpc').get('requestTimeout').invalid) {
                  <mat-icon matSuffix
                            matTooltipPosition="above"
                            matTooltipClass="tb-error-tooltip"
                        [matTooltip]="(getValueSettingsFormGroup.get('executeRpc').get('requestTimeout').hasError('required')
                                        ? 'widgets.value-action.request-timeout-required' : 'widgets.value-action.min-request-timeout-error') | translate"
                    class="tb-error">
                    warning
                  </mat-icon>
                }
              </mat-form-field>
            </div>
            <div class="tb-form-panel stroked tb-slide-toggle">
              <mat-expansion-panel class="tb-settings"
                                   [expanded]="getValueSettingsFormGroup.get('executeRpc').get('requestPersistent').value"
                                   [disabled]="!getValueSettingsFormGroup.get('executeRpc').get('requestPersistent').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="requestPersistent" (click)="$event.stopPropagation()">
                      {{ 'widgets.value-action.request-persistent' | translate }}
                    </mat-slide-toggle>
                  </mat-panel-title>
                </mat-expansion-panel-header>
                <ng-template matExpansionPanelContent>
                  <div class="tb-form-row space-between">
                    <div tb-hint-tooltip-icon="{{'widgets.value-action.persistent-polling-interval-hint' | translate}}">{{ 'widgets.value-action.persistent-polling-interval' | translate }}</div>
                    <mat-form-field appearance="outline" class="number" subscriptSizing="dynamic">
                      <input matInput formControlName="persistentPollingInterval" type="number" required min="1000" placeholder="{{ 'widget-config.set' | translate }}">
                      @if (getValueSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').touched &&
                        getValueSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').invalid) {
                        <mat-icon matSuffix
                                  matTooltipPosition="above"
                                  matTooltipClass="tb-error-tooltip"
                              [matTooltip]="(getValueSettingsFormGroup.get('executeRpc').get('persistentPollingInterval').hasError('required')
                                          ? 'widgets.value-action.persistent-polling-interval-required' : 'widgets.value-action.min-persistent-polling-interval-error') | translate"
                          class="tb-error">
                          warning
                        </mat-icon>
                      }
                    </mat-form-field>
                  </div>
                </ng-template>
              </mat-expansion-panel>
            </div>
          </ng-template>
        </mat-expansion-panel>
      </div>
    }
  </div>
  <div class="tb-action-settings-panel-buttons">
    <button mat-button
            color="primary"
            type="button"
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button
            color="primary"
            type="button"
            (click)="applyGetValueSettings()"
            [disabled]="getValueSettingsFormGroup.invalid || !getValueSettingsFormGroup.dirty">
      {{ 'action.apply' | translate }}
    </button>
  </div>
</div>
