<!--

    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 [formGroup]="entityFilterFormGroup" class="tb-entity-filter">
  <mat-form-field class="mat-block" appearance="outline">
    <mat-label translate>alias.filter-type</mat-label>
    <mat-select required formControlName="type">
      @for (type of aliasFilterTypes; track type) {
        <mat-option [value]="type">
          {{aliasFilterTypeTranslations.get(type) | translate}}
        </mat-option>
      }
    </mat-select>
    @if (entityFilterFormGroup.get('type').hasError('required')) {
      <mat-error>
        {{ 'alias.filter-type-required' | translate }}
      </mat-error>
    }
  </mat-form-field>
  <section class="flex flex-col" [formGroup]="filterFormGroup">
    @switch (entityFilterFormGroup.get('type').value) {
      @case (aliasFilterType.singleEntity) {
        <tb-entity-select required
                          appearance="outline"
                          useAliasEntityTypes="true"
                          formControlName="singleEntity">
        </tb-entity-select>
      }
      @case (aliasFilterType.entityList) {
        <tb-entity-type-select required
                               appearance="outline"
                               showLabel
                               useAliasEntityTypes="true"
                               [allowedEntityTypes]="listEntityTypes"
                               formControlName="entityType">
        </tb-entity-type-select>
        <tb-entity-list required
                        appearance="outline"
                        syncIdsWithDB
                        labelText="{{'entity.entity-list' | translate}}"
                        [entityType]="filterFormGroup.get('entityType').value"
                        formControlName="entityList">
        </tb-entity-list>
      }
      @case (aliasFilterType.entityName) {
        <tb-entity-type-select required
                               appearance="outline"
                               showLabel
                               useAliasEntityTypes="true"
                               [allowedEntityTypes]="listEntityTypes"
                               formControlName="entityType">
        </tb-entity-type-select>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>entity.name-starts-with</mat-label>
          <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
          matTooltip="{{'entity.help-text' | translate }}">help</mat-icon>
          <input matInput formControlName="entityNameFilter" required>
          @if (filterFormGroup.get('entityNameFilter').hasError('required')) {
            <mat-error>
              {{ 'entity.entity-name-filter-required' | translate }}
            </mat-error>
          }
        </mat-form-field>
      }
      @case (aliasFilterType.entityType) {
        <tb-entity-type-select required
                               appearance="outline"
                               showLabel
                               useAliasEntityTypes="true"
                               [allowedEntityTypes]="listEntityTypes"
                               formControlName="entityType">
        </tb-entity-type-select>
      }
      @case (aliasFilterType.stateEntity) {
        <mat-form-field floatLabel="always" class="mat-block" appearance="outline">
          <mat-label translate>alias.state-entity-parameter-name</mat-label>
          <input matInput formControlName="stateEntityParamName"
                 placeholder="{{ 'alias.default-entity-parameter-name' | translate }}">
        </mat-form-field>
        <div class="tb-form-panel stroked no-padding-bottom">
          <div class="tb-form-panel-title tb-normal">{{ 'alias.default-state-entity' | translate }}</div>
          <tb-entity-select appearance="outline"
                            useAliasEntityTypes="true"
                            formControlName="defaultStateEntity">
          </tb-entity-select>
        </div>
      }
      @case (aliasFilterType.assetType) {
        <tb-entity-subtype-list required
                                floatLabel="always"
                                appearance="outline"
                                label="{{ 'asset.asset-types' | translate }}"
                                [entityType]="entityType.ASSET"
                                formControlName="assetTypes">
        </tb-entity-subtype-list>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>asset.name-starts-with</mat-label>
          <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
          matTooltip="{{'asset.help-text' | translate }}">help</mat-icon>
          <input matInput formControlName="assetNameFilter">
        </mat-form-field>
      }
      @case (aliasFilterType.deviceType) {
        <tb-entity-subtype-list required
                                floatLabel="always"
                                appearance="outline"
                                label="{{ 'device.device-types' | translate }}"
                                [entityType]="entityType.DEVICE"
                                formControlName="deviceTypes">
        </tb-entity-subtype-list>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>device.name-starts-with</mat-label>
          <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
          matTooltip="{{'device.help-text' | translate }}">help</mat-icon>
          <input matInput formControlName="deviceNameFilter">
        </mat-form-field>
      }
      @case (aliasFilterType.entityViewType) {
        <tb-entity-subtype-list required
                                floatLabel="always"
                                appearance="outline"
                                label="{{ 'entity-view.entity-view-types' | translate }}"
                                [entityType]="entityType.ENTITY_VIEW"
                                formControlName="entityViewTypes">
        </tb-entity-subtype-list>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>entity-view.name-starts-with</mat-label>
          <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
          matTooltip="{{'entity-view.help-text' | translate }}">help</mat-icon>
          <input matInput formControlName="entityViewNameFilter">
        </mat-form-field>
      }
      @case (aliasFilterType.edgeType) {
        <tb-entity-subtype-list required
                                floatLabel="always"
                                appearance="outline"
                                label="{{ 'edge.edge-types' | translate }}"
                                [entityType]="entityType.EDGE"
                                formControlName="edgeTypes">
        </tb-entity-subtype-list>
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>edge.name-starts-with</mat-label>
          <input matInput formControlName="edgeNameFilter">
        </mat-form-field>
      }
      @case (aliasFilterType.relationsQuery) {
        <section class="tb-form-panel no-border no-padding">
          <div class="tb-form-panel stroked" [class.no-padding-bottom]="!filterFormGroup.get('rootStateEntity').value">
            <div class="tb-form-panel-title" translate>alias.root-entity</div>
            <mat-slide-toggle class="mat-slide" formControlName="rootStateEntity">
              {{ 'alias.root-state-entity' | translate }}
            </mat-slide-toggle>
            @if (filterFormGroup.get('rootStateEntity').value) {
              <div class="tb-form-panel no-border no-padding no-gap">
                <mat-form-field floatLabel="always" class="mat-block" appearance="outline">
                  <mat-label translate>alias.state-entity-parameter-name</mat-label>
                  <input matInput formControlName="stateEntityParamName"
                         placeholder="{{ 'alias.default-entity-parameter-name' | translate }}">
                </mat-form-field>
                <div class="tb-form-panel stroked no-padding-bottom">
                  <div class="tb-form-panel-title tb-normal">{{ 'alias.default-state-entity' | translate }}</div>
                  <tb-entity-select appearance="outline"
                                    useAliasEntityTypes="true"
                                    formControlName="defaultStateEntity">
                  </tb-entity-select>
                </div>
              </div>
            } @else {
              <tb-entity-select required
                                appearance="outline"
                                useAliasEntityTypes="true"
                                formControlName="rootEntity">
              </tb-entity-select>
            }
          </div>
          <div class="tb-form-panel stroked">
            <div class="tb-form-panel-title" translate>alias.query-options</div>
            <div class="flex flex-row gap-4">
              <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
                <mat-label translate>relation.direction</mat-label>
                <mat-select required formControlName="direction">
                  @for (type of directionTypes; track type) {
                    <mat-option [value]="type">
                      {{ directionTypeTranslations.get(directionTypeEnum[type]) | translate }}
                    </mat-option>
                  }
                </mat-select>
              </mat-form-field>
              <mat-form-field class="flex-1" floatLabel="always" appearance="outline" subscriptSizing="dynamic">
                <mat-label translate>alias.max-relation-level</mat-label>
                <input matInput
                       type="number"
                       min="1"
                       step="1"
                       placeholder="{{ 'alias.unlimited-level' | translate }}"
                       formControlName="maxLevel">
              </mat-form-field>
            </div>
            @if (filterFormGroup.get('maxLevel').value > 1 || !filterFormGroup.get('maxLevel').value) {
              <mat-slide-toggle
                class="mat-slide" formControlName="fetchLastLevelOnly">
                {{ 'alias.last-level-relation' | translate }}
              </mat-slide-toggle>
            }
          </div>
          <div class="tb-form-panel stroked">
            <div class="tb-form-panel-title" translate>relation.relation-filters</div>
            <tb-relation-filters
              enableNotOption
              [allowedEntityTypes]="allowedEntityTypes"
              formControlName="filters">
            </tb-relation-filters>
          </div>
        </section>
      }
      @case (entityFilterFormGroup.get('type').value === aliasFilterType.assetSearchQuery ||
        entityFilterFormGroup.get('type').value === aliasFilterType.deviceSearchQuery ||
        entityFilterFormGroup.get('type').value === aliasFilterType.edgeSearchQuery ||
        entityFilterFormGroup.get('type').value === aliasFilterType.entityViewSearchQuery ?
        entityFilterFormGroup.get('type').value : '') {
        <section class="tb-form-panel no-border no-padding">
          <div class="tb-form-panel stroked" [class.no-padding-bottom]="!filterFormGroup.get('rootStateEntity').value">
            <div class="tb-form-panel-title" translate>alias.root-entity</div>
            <mat-slide-toggle class="mat-slide" formControlName="rootStateEntity">
              {{ 'alias.root-state-entity' | translate }}
            </mat-slide-toggle>
            @if (filterFormGroup.get('rootStateEntity').value) {
              <div class="tb-form-panel no-border no-padding no-gap">
                <mat-form-field floatLabel="always" class="mat-block" appearance="outline">
                  <mat-label translate>alias.state-entity-parameter-name</mat-label>
                  <input matInput formControlName="stateEntityParamName"
                         placeholder="{{ 'alias.default-entity-parameter-name' | translate }}">
                </mat-form-field>
                <div class="tb-form-panel stroked no-padding-bottom">
                  <div class="tb-form-panel-title tb-normal">{{ 'alias.default-state-entity' | translate }}</div>
                  <tb-entity-select appearance="outline"
                                    useAliasEntityTypes="true"
                                    formControlName="defaultStateEntity">
                  </tb-entity-select>
                </div>
              </div>
            } @else {
              <tb-entity-select required
                                appearance="outline"
                                useAliasEntityTypes="true"
                                formControlName="rootEntity">
              </tb-entity-select>
            }
          </div>
          <div class="tb-form-panel stroked">
            <div class="tb-form-panel-title" translate>alias.query-options</div>
            <div class="flex flex-row gap-4">
              <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
                <mat-label translate>relation.direction</mat-label>
                <mat-select required formControlName="direction">
                  @for (type of directionTypes; track type) {
                    <mat-option [value]="type">
                      {{ directionTypeTranslations.get(directionTypeEnum[type]) | translate }}
                    </mat-option>
                  }
                </mat-select>
              </mat-form-field>
              <mat-form-field class="flex-1" floatLabel="always" appearance="outline" subscriptSizing="dynamic">
                <mat-label translate>alias.max-relation-level</mat-label>
                <input matInput
                       type="number"
                       min="1"
                       step="1"
                       placeholder="{{ 'alias.unlimited-level' | translate }}"
                       formControlName="maxLevel">
              </mat-form-field>
            </div>
            @if (filterFormGroup.get('maxLevel').value > 1 || !filterFormGroup.get('maxLevel').value) {
              <mat-slide-toggle
                class="mat-slide" formControlName="fetchLastLevelOnly">
                {{ 'alias.last-level-relation' | translate }}
              </mat-slide-toggle>
            }
          </div>
          <div class="tb-form-panel stroked no-padding-bottom">
            <div class="tb-form-panel-title" translate>relation.relation-filter</div>
            <tb-relation-type-autocomplete
              appearance="outline"
              subscriptSizing="dynamic"
              formControlName="relationType">
            </tb-relation-type-autocomplete>
            @switch (entityFilterFormGroup.get('type').value) {
              @case (aliasFilterType.assetSearchQuery) {
                <tb-entity-subtype-list
                  required
                  floatLabel="always"
                  appearance="outline"
                  label="{{ 'asset.asset-types' | translate }}"
                  [entityType]="entityType.ASSET"
                  formControlName="assetTypes">
                </tb-entity-subtype-list>
              }
              @case (aliasFilterType.deviceSearchQuery) {
                <tb-entity-subtype-list
                  required
                  floatLabel="always"
                  appearance="outline"
                  label="{{ 'device.device-types' | translate }}"
                  [entityType]="entityType.DEVICE"
                  formControlName="deviceTypes">
                </tb-entity-subtype-list>
              }
              @case (aliasFilterType.edgeSearchQuery) {
                <tb-entity-subtype-list
                  required
                  floatLabel="always"
                  appearance="outline"
                  label="{{ 'edge.edge-types' | translate }}"
                  [entityType]="entityType.EDGE"
                  formControlName="edgeTypes">
                </tb-entity-subtype-list>
              }
              @case (aliasFilterType.entityViewSearchQuery) {
                <tb-entity-subtype-list
                  required
                  floatLabel="always"
                  appearance="outline"
                  label="{{ 'entity-view.entity-view-types' | translate }}"
                  [entityType]="entityType.ENTITY_VIEW"
                  formControlName="entityViewTypes">
                </tb-entity-subtype-list>
              }
            }
          </div>
        </section>
      }
    }
  </section>
</div>
