<!--

    Copyright © 2016-2022 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 fxLayout="column" [formGroup]="entityFilterFormGroup" class="tb-entity-filter">
  <mat-form-field class="mat-block">
    <mat-label translate>alias.filter-type</mat-label>
    <mat-select required formControlName="type">
      <mat-option *ngFor="let type of aliasFilterTypes" [value]="type">
        {{aliasFilterTypeTranslations.get(type) | translate}}
      </mat-option>
    </mat-select>
    <mat-error *ngIf="entityFilterFormGroup.get('type').hasError('required')">
      {{ 'alias.filter-type-required' | translate }}
    </mat-error>
  </mat-form-field>
  <section fxLayout="column" [formGroup]="filterFormGroup" [ngSwitch]="entityFilterFormGroup.get('type').value">
    <ng-template [ngSwitchCase]="aliasFilterType.singleEntity">
      <tb-entity-select required
                        useAliasEntityTypes="true"
                        formControlName="singleEntity">
      </tb-entity-select>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityList">
      <tb-entity-type-select required
                             showLabel
                             [allowedEntityTypes]="allowedEntityTypes"
                             formControlName="entityType">
      </tb-entity-type-select>
      <tb-entity-list required
                      [entityType]="filterFormGroup.get('entityType').value"
                      formControlName="entityList">
      </tb-entity-list>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityName">
      <tb-entity-type-select required
                             showLabel
                             [allowedEntityTypes]="allowedEntityTypes"
                             formControlName="entityType">
      </tb-entity-type-select>
      <mat-form-field class="mat-block">
        <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>
        <mat-error *ngIf="filterFormGroup.get('entityNameFilter').hasError('required')">
          {{ 'entity.entity-name-filter-required' | translate }}
        </mat-error>
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityType">
      <tb-entity-type-select required
                             showLabel
                             [allowedEntityTypes]="allowedEntityTypes"
                             formControlName="entityType">
      </tb-entity-type-select>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.stateEntity">
      <mat-form-field floatLabel="always" class="mat-block">
        <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 fxLayout="column">
        <label class="tb-small">{{ 'alias.default-state-entity' | translate }}</label>
        <tb-entity-select fxFlex
                          useAliasEntityTypes="true"
                          formControlName="defaultStateEntity">
        </tb-entity-select>
      </div>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.assetType">
      <tb-entity-subtype-autocomplete required
                                      formControlName="assetType"
                                      [entityType]="entityType.ASSET">
      </tb-entity-subtype-autocomplete>
      <mat-form-field class="mat-block">
        <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>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.deviceType">
      <tb-entity-subtype-autocomplete required
                                      formControlName="deviceType"
                                      [entityType]="entityType.DEVICE">
      </tb-entity-subtype-autocomplete>
      <mat-form-field class="mat-block">
        <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>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityViewType">
      <tb-entity-subtype-autocomplete required
                                      formControlName="entityViewType"
                                      [entityType]="entityType.ENTITY_VIEW">
      </tb-entity-subtype-autocomplete>
      <mat-form-field class="mat-block">
        <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>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.edgeType">
      <tb-entity-subtype-autocomplete required
                                      formControlName="edgeType"
                                      [entityType]="entityType.EDGE">
      </tb-entity-subtype-autocomplete>
      <mat-form-field class="mat-block">
        <mat-label translate>edge.name-starts-with</mat-label>
        <input matInput formControlName="edgeNameFilter">
      </mat-form-field>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.relationsQuery">
      <section fxLayout="column" id="relationsQueryFilter">
        <label class="tb-small">{{ 'alias.root-entity' | translate }}</label>
        <section class="tb-root-state-entity-switch" fxLayout="row" fxLayoutAlign="start center" style="padding-left: 0;">
          <mat-slide-toggle class="root-state-entity-switch"
                            formControlName="rootStateEntity">
          </mat-slide-toggle>
          <label class="tb-small root-state-entity-label" translate>alias.root-state-entity</label>
        </section>
        <div fxFlex fxLayout="row" *ngIf="!filterFormGroup.get('rootStateEntity').value">
          <tb-entity-select fxFlex
                            required
                            useAliasEntityTypes="true"
                            formControlName="rootEntity">
          </tb-entity-select>
        </div>
        <div fxFlex fxLayout="row" *ngIf="filterFormGroup.get('rootStateEntity').value">
          <mat-form-field floatLabel="always" class="mat-block" style="margin-top: 14px; padding-right: 8px;">
            <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 fxFlex fxLayout="column">
            <label class="tb-small">{{ 'alias.default-state-entity' | translate }}</label>
            <tb-entity-select fxFlex
                              useAliasEntityTypes="true"
                              formControlName="defaultStateEntity">
            </tb-entity-select>
          </div>
        </div>
        <div fxFlex fxLayout="row">
          <section class="tb-root-state-entity-switch" fxLayout="row" fxLayoutAlign="start center" style="padding-left: 0;">
            <mat-slide-toggle class="root-state-entity-switch"
                              formControlName="fetchLastLevelOnly">
            </mat-slide-toggle>
            <label class="tb-small root-state-entity-label" translate>alias.last-level-relation</label>
          </section>
        </div>
        <div fxFlex fxLayoutGap="8px" fxLayout="row" fxLayout.xs="column">
          <mat-form-field class="mat-block" style="min-width: 100px;">
            <mat-label translate>relation.direction</mat-label>
            <mat-select required formControlName="direction">
              <mat-option *ngFor="let type of directionTypes" [value]="type">
                {{ directionTypeTranslations.get(directionTypeEnum[type]) | translate }}
              </mat-option>
            </mat-select>
          </mat-form-field>
          <mat-form-field fxFlex floatLabel="always" class="mat-block">
            <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>
        <div class="mat-caption" style="padding-bottom: 10px; color: rgba(0,0,0,0.57);" translate>relation.relation-filters</div>
        <tb-relation-filters
          [allowedEntityTypes]="allowedEntityTypes"
          formControlName="filters">
        </tb-relation-filters>
      </section>
    </ng-template>
    <ng-template [ngSwitchCase]="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 : ''">
      <label class="tb-small">{{ 'alias.root-entity' | translate }}</label>
      <section class="tb-root-state-entity-switch" fxLayout="row" fxLayoutAlign="start center" style="padding-left: 0;">
        <mat-slide-toggle class="root-state-entity-switch"
                          formControlName="rootStateEntity">
        </mat-slide-toggle>
        <label class="tb-small root-state-entity-label" translate>alias.root-state-entity</label>
      </section>
      <div fxFlex fxLayout="row" *ngIf="!filterFormGroup.get('rootStateEntity').value">
        <tb-entity-select fxFlex
                          required
                          useAliasEntityTypes="true"
                          formControlName="rootEntity">
        </tb-entity-select>
      </div>
      <div fxFlex fxLayout="row" *ngIf="filterFormGroup.get('rootStateEntity').value">
        <mat-form-field floatLabel="always" class="mat-block" style="margin-top: 14px; padding-right: 8px;">
          <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 fxFlex fxLayout="column">
          <label class="tb-small">{{ 'alias.default-state-entity' | translate }}</label>
          <tb-entity-select fxFlex
                            useAliasEntityTypes="true"
                            formControlName="defaultStateEntity">
          </tb-entity-select>
        </div>
      </div>
      <div fxFlex fxLayout="row">
        <section class="tb-root-state-entity-switch" fxLayout="row" fxLayoutAlign="start center" style="padding-left: 0;">
          <mat-slide-toggle class="root-state-entity-switch"
                            formControlName="fetchLastLevelOnly">
          </mat-slide-toggle>
          <label class="tb-small root-state-entity-label" translate>alias.last-level-relation</label>
        </section>
      </div>
      <div fxFlex fxLayoutGap="8px" fxLayout="row" fxLayout.xs="column">
        <mat-form-field class="mat-block" style="min-width: 100px;">
          <mat-label translate>relation.direction</mat-label>
          <mat-select required formControlName="direction">
            <mat-option *ngFor="let type of directionTypes" [value]="type">
              {{ directionTypeTranslations.get(directionTypeEnum[type]) | translate }}
            </mat-option>
          </mat-select>
        </mat-form-field>
        <mat-form-field fxFlex floatLabel="always" class="mat-block">
          <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>
      <div class="mat-caption" style="color: rgba(0,0,0,0.57);" translate>relation.relation-type</div>
      <tb-relation-type-autocomplete
        fxFlex
        formControlName="relationType">
      </tb-relation-type-autocomplete>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.assetSearchQuery">
      <div class="mat-caption tb-required" style="color: rgba(0,0,0,0.57);" translate>asset.asset-types</div>
      <tb-entity-subtype-list
        required
        [entityType]="entityType.ASSET"
        formControlName="assetTypes">
      </tb-entity-subtype-list>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.deviceSearchQuery">
      <div class="mat-caption tb-required" style="color: rgba(0,0,0,0.57);" translate>device.device-types</div>
      <tb-entity-subtype-list
        required
        [entityType]="entityType.DEVICE"
        formControlName="deviceTypes">
      </tb-entity-subtype-list>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.edgeSearchQuery">
      <div class="mat-caption tb-required" style="color: rgba(0,0,0,0.57);" translate>edge.edge-types</div>
      <tb-entity-subtype-list
        required
        [entityType]="entityType.EDGE"
        formControlName="edgeTypes">
      </tb-entity-subtype-list>
    </ng-template>
    <ng-template [ngSwitchCase]="aliasFilterType.entityViewSearchQuery">
      <div class="mat-caption tb-required" style="color: rgba(0,0,0,0.57);" translate>entity-view.entity-view-types</div>
      <tb-entity-subtype-list
        required
        [entityType]="entityType.ENTITY_VIEW"
        formControlName="entityViewTypes">
      </tb-entity-subtype-list>
    </ng-template>
  </section>
</div>
