<!--

    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.!hidden]="!propertyGroups?.length && !propertiesContent.children?.length" class="tb-form-panel"
     [class.stroked]="stroked" [class.no-padding]="noPadding" [class.no-border]="noBorder"
     [formGroup]="propertiesFormGroup">
  @if (title) {
    <div class="tb-form-panel-title">{{ title }}</div>
  }
  <ng-container #propertiesContent><ng-content select=".tb-properties-content"></ng-content></ng-container>
  @for (propertyGroup of propertyGroups; track propertyGroup) {
    @if (propertyGroup.visible) {
      @if (propertyGroup.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">{{ propertyGroup.title | customTranslate }}</div>
              </mat-panel-title>
            </mat-expansion-panel-header>
            <ng-template matExpansionPanelContent>
              <ng-container *ngTemplateOutlet="propertyContainersTpl; context:{propertyContainers: propertyGroup.containers}"></ng-container>
            </ng-template>
          </mat-expansion-panel>
        </div>
      }
      @if (!propertyGroup.title) {
        <ng-container *ngTemplateOutlet="propertyContainersTpl; context:{propertyContainers: propertyGroup.containers}"></ng-container>
      }
    }
  }
</div>

<ng-template #propertyContainersTpl let-propertyContainers="propertyContainers">
  @for (propertyContainer of propertyContainers; track propertyContainer) {
    <ng-container [formGroup]="propertiesFormGroup">
      @if (propertyContainer.visible) {
        @if (propertyContainer.type === FormPropertyContainerType.field) {
          <ng-container *ngTemplateOutlet="propertyFieldTpl;context:{property: propertyContainer.property}">
          </ng-container>
        }
        @if (propertyContainer.type === FormPropertyContainerType.row) {
          <ng-container *ngTemplateOutlet="propertyRowTpl;context:{propertyRow: propertyContainer}">
          </ng-container>
        }
        @if (propertyContainer.type === FormPropertyContainerType.fieldset) {
          <tb-dynamic-form
            formControlName="{{ propertyContainer.property.id }}"
            title="{{ propertyContainer.label | customTranslate }}"
            stroked
            [properties]="propertyContainer.properties">
          </tb-dynamic-form>
        }
        @if (propertyContainer.type === FormPropertyContainerType.array) {
          @if (propertyContainer.arrayItemProperty.type === FormPropertyType.image) {
            <div class="tb-form-row flex-1">
              <tb-multiple-gallery-image-input
                class="flex-1"
                label="{{ propertyContainer.property.name | customTranslate }}"
                formControlName="{{ propertyContainer.property.id }}"
                [required]="propertyContainer.property.required">
              </tb-multiple-gallery-image-input>
            </div>
          }
          @if (propertyContainer.arrayItemProperty.type !== FormPropertyType.image) {
            <tb-dynamic-form-array
              formControlName="{{ propertyContainer.property.id }}"
              [itemProperty]="propertyContainer.arrayItemProperty"
              title="{{ propertyContainer.label | customTranslate }}">
            </tb-dynamic-form-array>
          }
        }
        @if (propertyContainer.type === FormPropertyContainerType.htmlSection) {
          <div [innerHTML]="propertyContainer.safeHtml" [class]="propertyContainer.htmlClass"></div>
        }
      }
    </ng-container>
  }
</ng-template>

<ng-template #propertyFieldTpl let-property="property">
  <div [formGroup]="propertiesFormGroup" class="tb-form-row no-border no-padding tb-standard-fields">
    <ng-container *ngTemplateOutlet="inputFieldTpl;context:{property: property, singleField: true}">
    </ng-container>
  </div>
</ng-template>

<ng-template #propertyRowTpl let-propertyRow="propertyRow">
  <div [formGroup]="propertiesFormGroup" class="tb-form-row space-between overflow-auto" [class]="propertyRow.rowClass">
    @if (propertyRow.switch && propertyRow.switch.visible) {
      <mat-slide-toggle
        class="mat-slide fixed-title-width margin" formControlName="{{ propertyRow.switch.id }}">
        <div tb-hint-tooltip-icon="{{ propertyRow.hint | customTranslate }}">
          {{ propertyRow.label | customTranslate }}
        </div>
      </mat-slide-toggle>
    }
    @if (!propertyRow.switch) {
      <div class="fixed-title-width fixed-title-height">
        <div tb-hint-tooltip-icon="{{ propertyRow.hint | customTranslate }}">
          {{ propertyRow.label | customTranslate }}
        </div>
      </div>
    }
    @if (propertyRow.properties.length) {
      <div class="tb-flex" [class]="propertyRow.propertiesRowClass">
        @for (property of propertyRow.properties; track property) {
          @if (property.visible) {
            @if (property.subLabel) {
              <div class="tb-small-label">{{ property.subLabel | customTranslate }}</div>
            }
            @if (isInputFieldPropertyType(property.type)) {
              <ng-container *ngTemplateOutlet="inputFieldTpl;context:{property: property, singleField: false}">
              </ng-container>
            }
            @if (property.type === FormPropertyType.color) {
              <tb-color-input
                [required]="property.required"
                [class]="property.fieldClass"
                asBoxInput
                colorClearButton
                formControlName="{{ property.id }}">
              </tb-color-input>
            }
            @if (property.type === FormPropertyType.color_settings) {
              <tb-color-settings
                [class]="property.fieldClass"
                formControlName="{{ property.id }}"
                settingsKey="{{ property.name | customTranslate }}">
              </tb-color-settings>
            }
            @if (property.type === FormPropertyType.font) {
              <tb-font-settings
                [class]="property.fieldClass"
                formControlName="{{ property.id }}"
                clearButton
                disabledLineHeight>
              </tb-font-settings>
            }
            @if (property.type === FormPropertyType.units) {
              <tb-unit-input
                [class]="property.fieldClass"
                [required]="property.required"
                [supportsUnitConversion]="property.supportsUnitConversion"
              formControlName="{{ property.id }}"></tb-unit-input>
            }
            @if (property.type === FormPropertyType.icon) {
              <tb-material-icon-select
                [required]="property.required"
                asBoxInput
                formControlName="{{ property.id }}">
              </tb-material-icon-select>
            }
            @if (property.divider) {
              <mat-divider vertical></mat-divider>
            }
          }
        }
      </div>
    }
  </div>
</ng-template>

<ng-template #inputFieldTpl let-property="property" let-singleField="singleField">
  <ng-container [formGroup]="propertiesFormGroup">
    @if (property.type === FormPropertyType.textarea) {
      <mat-form-field
        [class]="singleField ? 'flex' : property.fieldClass"
        [floatLabel]="singleField ? 'always' : 'auto'"
        appearance="outline" subscriptSizing="dynamic">
        @if (singleField) {
          <mat-label>{{ property.name | customTranslate }}</mat-label>
        }
        <textarea matInput
                  [rows]="property.rows || 2"
                  formControlName="{{ property.id }}"
                  [required]="property.required"
        placeholder="{{ 'widget-config.set' | translate }}"></textarea>
      </mat-form-field>
    }
    @if ([FormPropertyType.text, FormPropertyType.password].includes(property.type)) {
      <mat-form-field
        [class]="singleField ? 'flex' : property.fieldClass"
        [floatLabel]="singleField ? 'always' : 'auto'"
        appearance="outline" subscriptSizing="dynamic">
        @if (singleField) {
          <mat-label>{{ property.name | customTranslate }}</mat-label>
        }
        <input matInput
               [type]="property.type"
               formControlName="{{ property.id }}"
               [required]="property.required"
               placeholder="{{ 'widget-config.set' | translate }}">
        @if (property.fieldSuffix) {
          <div class="lt-md:!hidden" matSuffix>{{ property.fieldSuffix | customTranslate }}</div>
        }
      </mat-form-field>
    }
    @if (property.type === FormPropertyType.number) {
      <mat-form-field
        [class]="singleField ? 'flex' : property.fieldClass"
        class="number"
        [floatLabel]="singleField ? 'always' : 'auto'"
        appearance="outline" subscriptSizing="dynamic">
        @if (singleField) {
          <mat-label>{{ property.name | customTranslate }}</mat-label>
        }
        <input matInput
               type="number"
               formControlName="{{ property.id }}"
               [required]="property.required"
               [min]="property.min"
               [max]="property.max"
               [step]="property.step"
               placeholder="{{ 'widget-config.set' | translate }}">
        @if (property.fieldSuffix) {
          <div class="lt-md:!hidden" matSuffix>{{ property.fieldSuffix | customTranslate }}</div>
        }
      </mat-form-field>
    }
    @if (property.type === FormPropertyType.select) {
      <mat-form-field
        [class]="singleField ? 'flex' : (property.fieldClass || 'medium-width')"
        [floatLabel]="singleField ? 'always' : 'auto'"
        appearance="outline" subscriptSizing="dynamic">
        @if (singleField) {
          <mat-label>{{ property.name | customTranslate }}</mat-label>
        }
        <mat-select formControlName="{{ property.id }}"
                    [required]="property.required"
                    [multiple]="property.multiple">
          @if (!property.multiple && property.allowEmptyOption) {
            <mat-option [value]="null"></mat-option>
          }
          @for (item of property.items; track item) {
            <mat-option [value]="item.value">
              {{ item.label | customTranslate }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
    }
    @if (property.type === FormPropertyType.datetime) {
      <tb-datetime
        [fieldClass]="singleField ? 'flex' : property.fieldClass"
        [class.flex-1]="singleField || property.fieldClass?.includes('flex')"
        [floatLabel]="singleField ? 'always' : 'auto'"
        [showLabel]="singleField"
        [dateText]="singleField ? (property.name | customTranslate) : ''"
        [allowClear]="property.allowClear"
        [required]="property.required"
        appearance="outline"
        subscriptSizing="dynamic"
        [type]="property.dateTimeType"
        formControlName="{{ property.id }}">
      </tb-datetime>
    }
    @if (property.type === FormPropertyType.javascript) {
      <div class="tb-form-row flex-1">
        <tb-js-func class="flex-1"
                    label="{{ property.name | customTranslate }}"
                    disableUndefinedCheck
                    [required]="property.required"
                    [helpId]="property.helpId"
                    formControlName="{{ property.id }}">
        </tb-js-func>
      </div>
    }
    @if (property.type === FormPropertyType.json) {
      <div class="tb-form-row flex-1">
        <tb-json-content class="flex-1"
                         label="{{ property.name | customTranslate }}"
                         [required]="property.required"
                         [contentType]="ContentType.JSON"
                         formControlName="{{ property.id }}">
        </tb-json-content>
      </div>
    }
    @if (property.type === FormPropertyType.html) {
      <div class="tb-form-row flex-1">
        <tb-html class="flex-1"
                 label="{{ property.name | customTranslate }}"
                 [required]="property.required"
                 formControlName="{{ property.id }}">
        </tb-html>
      </div>
    }
    @if (property.type === FormPropertyType.css) {
      <div class="tb-form-row flex-1">
        <tb-css class="flex-1"
                label="{{ property.name | customTranslate }}"
                [required]="property.required"
                formControlName="{{ property.id }}">
        </tb-css>
      </div>
    }
    @if (property.type === FormPropertyType.markdown) {
      <div class="tb-form-row flex-1">
        <tb-markdown-editor class="flex-1"
                            label="{{ property.name | customTranslate }}"
                            [required]="property.required"
                            [helpId]="property.helpId"
                            formControlName="{{ property.id }}">
        </tb-markdown-editor>
      </div>
    }
    @if (property.type === FormPropertyType.image) {
      <div class="tb-form-row flex-1">
        <tb-gallery-image-input
          class="flex-1"
          label="{{ property.name | customTranslate }}"
          formControlName="{{ property.id }}"
          [required]="property.required">
        </tb-gallery-image-input>
      </div>
    }
    @if (property.type === FormPropertyType.radios) {
      <div
        class="tb-form-row tb-radios-property column-xs flex-1" [class.direction-column]="property.direction === 'column'">
        <div tbTruncateWithTooltip class="fixed-title-width fixed-title-height">{{ property.name | customTranslate }}</div>
        <mat-radio-group class="flex flex-1 flex-wrap" [class.flex-col]="property.direction === 'column'"
                         [required]="property.required"
                         formControlName="{{ property.id }}">
          @for (item of property.items; track item) {
            <mat-radio-button [value]="item.value">
              <div tbTruncateWithTooltip>{{ item.label | customTranslate }}</div>
            </mat-radio-button>
          }
        </mat-radio-group>
      </div>
    }
  </ng-container>
</ng-template>
