<!--

    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]="scadaSymbolObjectSettingsFormGroup">
  @if (behaviorGroups?.length) {
    <div class="tb-form-panel" formGroupName="behavior">
      <div class="tb-form-panel-title" translate>scada.behavior.behavior</div>
      @for (behaviorGroup of behaviorGroups; track behaviorGroup) {
        @if (behaviorGroup.title) {
          <div class="tb-form-panel stroked">
            <mat-expansion-panel class="tb-settings" expanded>
              <mat-expansion-panel-header class="flex flex-row flex-wrap">
                <mat-panel-title>
                  <div class="tb-form-panel-title">{{ behaviorGroup.title | customTranslate }}</div>
                </mat-panel-title>
              </mat-expansion-panel-header>
              <ng-template matExpansionPanelContent>
                <ng-container *ngTemplateOutlet="behaviorsTpl; context:{behaviors: behaviorGroup.behaviors}"></ng-container>
              </ng-template>
            </mat-expansion-panel>
          </div>
        }
        @if (!behaviorGroup.title) {
          <ng-container *ngTemplateOutlet="behaviorsTpl; context:{behaviors: behaviorGroup.behaviors}"></ng-container>
        }
      }
    </div>
  }
  <tb-dynamic-form formControlName="properties"
                   title="{{ 'widget-config.appearance' | translate }}"
                   [properties]="metadata?.properties">
    <ng-container class="tb-properties-content"><ng-content select=".tb-scada-symbol-appearance-properties"></ng-content></ng-container>
  </tb-dynamic-form>
</ng-container>

<ng-template #behaviorsTpl let-behaviors="behaviors">
  @for (behavior of behaviors; track behavior) {
    <ng-container *ngTemplateOutlet="behaviorTpl; context:{behavior: behavior}"></ng-container>
  }
</ng-template>

<ng-template #behaviorTpl let-behavior="behavior">
  <ng-container [formGroup]="scadaSymbolObjectSettingsFormGroup">
    <div class="tb-form-row flex-wrap overflow-auto" formGroupName="behavior">
      <div class="fixed-title-width fixed-title-height" tb-hint-tooltip-icon="{{ behavior.hint | customTranslate }}">{{ behavior.name | customTranslate }}</div>
      @if (behavior.type === ScadaSymbolBehaviorType.value) {
        <tb-get-value-action-settings
          class="tb-flex"
          panelTitle="{{ behavior.name | customTranslate }}"
          [valueType]="behavior.valueType"
          [trueLabel]="behavior.trueLabel | customTranslate"
          [falseLabel]="behavior.falseLabel | customTranslate"
          [stateLabel]="behavior.stateLabel | customTranslate"
          [aliasController]="aliasController"
          [targetDevice]="targetDevice"
          [widgetType]="widgetType"
          formControlName="{{ behavior.id }}">
        </tb-get-value-action-settings>
      }
      @if (behavior.type === ScadaSymbolBehaviorType.action) {
        <tb-set-value-action-settings
          class="tb-flex"
          panelTitle="{{ behavior.name | customTranslate }}"
          [valueType]="behavior.valueType"
          [aliasController]="aliasController"
          [targetDevice]="targetDevice"
          [widgetType]="widgetType"
          formControlName="{{ behavior.id }}">
        </tb-set-value-action-settings>
      }
      @if (behavior.type === ScadaSymbolBehaviorType.widgetAction) {
        <tb-widget-action-settings
          class="tb-flex"
          panelTitle="{{ behavior.name | customTranslate }}"
          [callbacks]="callbacks"
          [widgetType]="widgetType"
          formControlName="{{ behavior.id }}">
        </tb-widget-action-settings>
      }
    </div>
  </ng-container>
</ng-template>
