<!--

    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-form-panel no-padding no-border" [formGroup]="widgetActionFormGroup">
  @if (withName) {
    <div class="tb-form-row">
      <div class="fixed-title-width">{{'widget-config.action-name' | translate}}*</div>
      <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
        <input required matInput formControlName="name" placeholder="{{ 'widget-config.set' | translate }}">
        @if (widgetActionFormGroup.get('name').invalid
          && (widgetActionFormGroup.get('name').touched || widgetActionFormGroup.get('name').hasError('actionNameNotUnique'))) {
          <mat-icon matSuffix
                    matTooltipPosition="above"
                    matTooltipClass="tb-error-tooltip"
                [matTooltip]="(widgetActionFormGroup.get('name').hasError('required')
                                     ? 'widget-config.action-name-required'
                                     : 'widget-config.action-name-not-unique') | translate"
            class="tb-error">
            warning
          </mat-icon>
        }
      </mat-form-field>
    </div>
  }
  <div class="tb-form-row">
    <div translate>widget-config.action</div>
    <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
      <mat-select required formControlName="type" placeholder="{{ 'widget-config.set' | translate }}">
        @for (actionType of actionTypes(); track actionType) {
          <mat-option [value]="actionType">
            {{ widgetActionTypeTranslations.get(widgetActionType[actionType]) | translate }}
          </mat-option>
        }
      </mat-select>
    </mat-form-field>
  </div>
  <ng-container [formGroup]="actionTypeFormGroup">
    @switch (widgetActionFormGroup.get('type').value) {
      @case (widgetActionType.openDashboard) {
        <div class="tb-form-row">
          <div class="fixed-title-width">{{ 'widget-action.target-dashboard' | translate }}*</div>
          <tb-dashboard-autocomplete class="flex-1"
                                     formControlName="targetDashboardId"
                                     required
                                     requiredText="widget-action.target-dashboard-required"
                                     placeholder="{{ 'widget-action.select-target-dashboard' | translate }}"
                                     inlineField
                                     [selectFirstDashboard]="true"
          ></tb-dashboard-autocomplete>
        </div>
      }
      @case (widgetActionFormGroup.get('type').value === widgetActionType.openDashboardState ||
        widgetActionFormGroup.get('type').value === widgetActionType.updateDashboardState ||
        widgetActionFormGroup.get('type').value === widgetActionType.openDashboard ?
        widgetActionFormGroup.get('type').value : '') {
        <div class="tb-form-row">
          <div class="fixed-title-width">
            <span>{{ 'widget-action.target-dashboard-state' | translate }}</span>
            @if (widgetActionFormGroup.get('type').value === widgetActionType.openDashboardState) {
              <span>*</span>
            }
          </div>
          <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic"
                          [class.tb-suffix-absolute]="!actionTypeFormGroup.get('targetDashboardStateId').value">
            <input matInput type="text" placeholder="{{ 'widget-action.target-dashboard-state' | translate }}"
                   #dashboardStateInput
                   formControlName="targetDashboardStateId"
                   [required]="widgetActionFormGroup.get('type').value === widgetActionType.openDashboardState"
                   (focusin)="onDashboardStateInputFocus()"
                   [matAutocomplete]="targetDashboardStateAutocomplete">
            @if (actionTypeFormGroup.get('targetDashboardStateId').value) {
              <button
                type="button"
                matSuffix mat-icon-button aria-label="Clear"
                (click)="clearTargetDashboardState()">
                <mat-icon class="material-icons">close</mat-icon>
              </button>
            }
            @if (actionTypeFormGroup.get('targetDashboardStateId').hasError('required')
              && actionTypeFormGroup.get('targetDashboardStateId').touched) {
              <mat-icon matSuffix
                        matTooltipPosition="above"
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="'widget-action.target-dashboard-state-required' | translate"
                        class="tb-error">
                warning
              </mat-icon>
            }
            <mat-autocomplete
              class="tb-autocomplete"
              #targetDashboardStateAutocomplete="matAutocomplete">
              @for (state of filteredDashboardStates | async; track state) {
                <mat-option [value]="state">
                  <span [innerHTML]="state | highlight:targetDashboardStateSearchText"></span>
                </mat-option>
              }
            </mat-autocomplete>
          </mat-form-field>
        </div>
      }
      @case (widgetActionType.openURL) {
        <div class="tb-form-row">
          <div class="tb-required">{{ 'widget-action.URL' | translate }}</div>
          <mat-form-field class="flex-1" style="margin-left: 9px" appearance="outline" subscriptSizing="dynamic">
            <input matInput placeholder="{{ 'widget-config.set' | translate }}" formControlName="url">
            @if (actionTypeFormGroup.get('url').invalid && actionTypeFormGroup.get('url').touched) {
              <mat-icon matSuffix
                        matTooltipPosition="above"
                        matTooltipClass="tb-error-tooltip"
                        [matTooltip]="'widget-action.url-required' | translate"
                        class="tb-error">
                warning
              </mat-icon>
            }
          </mat-form-field>
        </div>
      }
      @case (widgetActionFormGroup.get('type').value === widgetActionType.openDashboardState ||
        widgetActionFormGroup.get('type').value === widgetActionType.updateDashboardState ?
        widgetActionFormGroup.get('type').value : '') {
        <div class="tb-form-row">
          <mat-slide-toggle class="mat-slide" formControlName="openRightLayout">
            {{ 'widget-action.open-right-layout' | translate }}
          </mat-slide-toggle>
        </div>
      }
      @case (widgetActionFormGroup.get('type').value === widgetActionType.openDashboard ||
        widgetActionFormGroup.get('type').value === widgetActionType.openURL ?
        widgetActionFormGroup.get('type').value : '') {
        <div class="tb-form-row">
          <mat-slide-toggle class="mat-slide" formControlName="openNewBrowserTab">
            {{ 'widget-action.open-new-browser-tab' | translate }}
          </mat-slide-toggle>
        </div>
      }
      @case (widgetActionFormGroup.get('type').value === widgetActionType.openDashboardState ||
        widgetActionFormGroup.get('type').value === widgetActionType.updateDashboardState ||
        widgetActionFormGroup.get('type').value === widgetActionType.openDashboard ?
        widgetActionFormGroup.get('type').value : '') {
        @if (widgetType !== WidgetType.static) {
          <div class="tb-form-row">
            <mat-slide-toggle class="mat-slide" formControlName="setEntityId">
              {{ 'widget-action.set-entity-from-widget' | translate }}
            </mat-slide-toggle>
          </div>
        }
        @if (actionTypeFormGroup.get('setEntityId').value) {
          <div class="tb-form-row">
            <div class="fixed-title-width">{{ 'alias.state-entity-parameter-name' | translate }}</div>
            <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
              <input matInput
                     placeholder="{{ 'alias.default-entity-parameter-name' | translate }}"
                     formControlName="stateEntityParamName">
            </mat-form-field>
          </div>
        }
      }
      @case (widgetActionFormGroup.get('type').value === widgetActionType.openDashboardState ?
        widgetActionFormGroup.get('type').value : '') {
        <div class="tb-form-row">
          <div class="fixed-title-width">{{ 'widget-action.state-display-type' | translate }}</div>
          <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
            <mat-select formControlName="stateDisplayType">
              @for (displayType of allStateDisplayTypes; track displayType) {
                <mat-option [value]="displayType">
                  {{ stateDisplayTypeName(displayType) }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        </div>
        <ng-container [formGroup]="stateDisplayTypeFormGroup">
          @switch (actionTypeFormGroup.get('stateDisplayType').value) {
            @case ('separateDialog') {
              <div class="tb-form-row">
                <div class="fixed-title-width">{{ 'widget-action.dialog-title' | translate }}</div>
                <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
                  <input matInput placeholder="{{ 'widget-config.set' | translate }}" formControlName="dialogTitle">
                </mat-form-field>
              </div>
              <div class="tb-form-row">
                <mat-slide-toggle class="mat-slide" formControlName="dialogHideDashboardToolbar">
                  {{ 'widget-action.dialog-hide-dashboard-toolbar' | translate }}
                </mat-slide-toggle>
              </div>
              <div class="tb-form-row space-between">
                <div>{{ 'widget-action.dialog-width' | translate }}</div>
                <mat-form-field appearance="outline" subscriptSizing="dynamic" class="tb-suffix-absolute number">
                  <input type="number" min="1" max="100" step="1" matInput formControlName="dialogWidth">
                  @if (stateDisplayTypeFormGroup.get('dialogWidth').invalid && stateDisplayTypeFormGroup.get('dialogWidth').touched) {
                    <mat-icon matSuffix
                              matTooltipPosition="above"
                              matTooltipClass="tb-error-tooltip"
                              [matTooltip]="'widget-action.dialog-size-range-error' | translate"
                              class="tb-error">
                      warning
                    </mat-icon>
                  }
                </mat-form-field>
              </div>
              <div class="tb-form-row space-between">
                <div>{{ 'widget-action.dialog-height' | translate }}</div>
                <mat-form-field appearance="outline" subscriptSizing="dynamic" class="tb-suffix-absolute number">
                  <input type="number" min="1" max="100" step="1" matInput formControlName="dialogHeight">
                  @if (stateDisplayTypeFormGroup.get('dialogHeight').invalid && stateDisplayTypeFormGroup.get('dialogHeight').touched) {
                    <mat-icon matSuffix
                              matTooltipPosition="above"
                              matTooltipClass="tb-error-tooltip"
                              [matTooltip]="'widget-action.dialog-size-range-error' | translate"
                              class="tb-error">
                      warning
                    </mat-icon>
                  }
                </mat-form-field>
              </div>
            }
            @case ('popover') {
              <div class="tb-form-row">
                <div class="fixed-title-width">{{ 'widget-action.popover-preferred-placement' | translate }}</div>
                <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
                  <mat-select formControlName="popoverPreferredPlacement">
                    @for (placement of allPopoverPlacements; track placement) {
                      <mat-option [value]="placement">
                        {{ popoverPlacementName(placement) }}
                      </mat-option>
                    }
                  </mat-select>
                </mat-form-field>
              </div>
              <div class="tb-form-row">
                <mat-slide-toggle class="mat-slide" formControlName="popoverHideOnClickOutside">
                  {{ 'widget-action.popover-hide-on-click-outside' | translate }}
                </mat-slide-toggle>
              </div>
              <div class="tb-form-row">
                <mat-slide-toggle class="mat-slide" formControlName="popoverHideDashboardToolbar">
                  {{ 'widget-action.popover-hide-dashboard-toolbar' | translate }}
                </mat-slide-toggle>
              </div>
              <div class="tb-form-row space-between">
                <div>{{ 'widget-action.popover-width' | translate }}</div>
                <tb-css-size-input formControlName="popoverWidth">
                </tb-css-size-input>
              </div>
              <div class="tb-form-row space-between">
                <div>{{ 'widget-action.popover-height' | translate }}</div>
                <tb-css-size-input formControlName="popoverHeight">
                </tb-css-size-input>
              </div>
              <tb-json-object-edit
                [editorStyle]="{minHeight: '100px'}"
                label="{{ 'widget-action.popover-style' | translate }}"
                formControlName="popoverStyle"
              ></tb-json-object-edit>
            }
            @case ('normal') {
            }
          }
        </ng-container>
      }
      @case (widgetActionType.custom) {
        <tb-js-func
          formControlName="customFunction"
          functionTitle="{{ 'widget-action.custom-action-function' | translate }}"
          [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
          [globalVariables]="functionScopeVariables"
          [validationArgs]="[]"
          [editorCompleter]="customActionEditorCompleter"
          withModules
          helpId="widget/action/custom_action_fn"
          [helpPopupStyle]="{width: '900px'}"
        ></tb-js-func>
      }
      @case (widgetActionType.placeMapItem) {
        <div class="tb-form-row">
          <div class="fixed-title-width">{{ 'widget-action.map-item-type' | translate }}</div>
          <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
            <mat-select placeholder="{{ 'widget-config.set' | translate }}" formControlName="mapItemType">
              @for(type of mapItemTypes; track type) {
                <mat-option [value]="type">{{ mapItemTypeTranslationMap.get(type) | translate }}</mat-option>
              }
            </mat-select>
          </mat-form-field>
        </div>
        <tb-map-item-tooltips
          [mapItemType]="actionTypeFormGroup.get('mapItemType').value"
          formControlName="mapItemTooltips">
        </tb-map-item-tooltips>
      }
      @case (widgetActionFormGroup.get('type').value === widgetActionType.customPretty
        || widgetActionFormGroup.get('type').value === widgetActionType.placeMapItem
        ? widgetActionFormGroup.get('type').value : '') {
        <tb-custom-action-pretty-editor
          [widgetActionType]="widgetActionFormGroup.get('type').value"
          formControlName="customAction">
        </tb-custom-action-pretty-editor>
      }
      @case (widgetActionType.mobileAction) {
        <tb-mobile-action-editor formControlName="mobileAction">
        </tb-mobile-action-editor>
      }
    }
  </ng-container>
</div>
