<!--

    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.

-->
<form [formGroup]="keyFilterFormGroup" (ngSubmit)="save()" style="width: 900px;">
  <mat-toolbar color="primary">
    <h2>{{(data.isAdd ? 'filter.add-key-filter' : (data.readonly ? 'filter.key-filter' : 'filter.edit-key-filter')) | translate}}</h2>
    <span fxFlex></span>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content>
    <fieldset [disabled]="isLoading$ | async" fxLayout="column">
      <section fxLayout="row" fxLayoutGap="8px" class="entity-key">
        <section [fxFlex]="isConstantKeyType ? 45 : 70" fxLayout="row" formGroupName="key" fxLayoutGap="8px">
          <mat-form-field fxFlex="40" class="mat-block">
            <mat-label translate>filter.key-type.key-type</mat-label>
            <mat-select required formControlName="type">
              <mat-option *ngFor="let type of entityKeyTypes" [value]="type">
                {{entityKeyTypeTranslations.get(type) | translate}}
              </mat-option>
            </mat-select>
          </mat-form-field>
          <mat-form-field fxFlex="60" class="mat-block">
            <mat-label translate>filter.key-name</mat-label>
            <input matInput required formControlName="key"
                   #keyNameInput
                   (focusin)="onFocus()"
                   [matAutocomplete]="keyName"
                   [matAutocompleteDisabled]="!showAutocomplete">
            <button *ngIf="keyFilterFormGroup.get('key.key').value && showAutocomplete"
                    type="button"
                    matSuffix mat-button mat-icon-button aria-label="Clear"
                    (click)="clear()">
              <mat-icon class="material-icons">close</mat-icon>
            </button>
            <mat-autocomplete autoActiveFirstOption #keyName="matAutocomplete">
              <mat-option *ngFor="let keyName of filteredKeysName | async" [value]="keyName">
                <span [innerHTML]="keyName | highlight:searchText"></span>
              </mat-option>
            </mat-autocomplete>
            <mat-error *ngIf="keyFilterFormGroup.get('key.key').hasError('required')">
              {{ 'filter.key-name-required' | translate }}
            </mat-error>
          </mat-form-field>
        </section>
        <mat-form-field [fxFlex]="isConstantKeyType ? 20 : 30" class="mat-block">
          <mat-label translate>filter.value-type.value-type</mat-label>
          <mat-select formControlName="valueType">
            <mat-select-trigger>
              <mat-icon class="tb-mat-18" svgIcon="{{ entityKeyValueTypes.get(keyFilterFormGroup.get('valueType').value)?.icon }}"></mat-icon>
              <span>{{ entityKeyValueTypes.get(keyFilterFormGroup.get('valueType').value)?.name | translate }}</span>
            </mat-select-trigger>
            <mat-option *ngFor="let valueType of entityKeyValueTypesKeys" [value]="valueType">
              <mat-icon class="tb-mat-18" svgIcon="{{ entityKeyValueTypes.get(entityKeyValueTypeEnum[valueType]).icon }}"></mat-icon>
              <span>{{ entityKeyValueTypes.get(entityKeyValueTypeEnum[valueType]).name | translate }}</span>
            </mat-option>
          </mat-select>
          <mat-error *ngIf="keyFilterFormGroup.get('valueType').hasError('required')">
            {{ 'filter.value-type-required' | translate }}
          </mat-error>
        </mat-form-field>
        <div fxFlex *ngIf="isConstantKeyType">
          <div fxLayout="column" [ngSwitch]="keyFilterFormGroup.get('valueType').value">
            <ng-template [ngSwitchCase]="entityKeyValueTypeEnum.STRING">
              <mat-form-field class="mat-block">
                <mat-label translate>filter.value</mat-label>
                <input matInput required formControlName="value" placeholder="{{'filter.value' | translate}}">
              </mat-form-field>
            </ng-template>
            <ng-template [ngSwitchCase]="entityKeyValueTypeEnum.NUMERIC">
              <mat-form-field class="mat-block">
                <mat-label translate>filter.value</mat-label>
                <input required type="number" matInput formControlName="value">
              </mat-form-field>
            </ng-template>
            <ng-template [ngSwitchCase]="entityKeyValueTypeEnum.DATE_TIME">
              <tb-datetime formControlName="value"
                           dateText="filter.date"
                           timeText="filter.time"
                           required></tb-datetime>
            </ng-template>
            <ng-template [ngSwitchCase]="entityKeyValueTypeEnum.BOOLEAN">
              <mat-checkbox formControlName="value" style="margin-top: 20px">
                {{ (keyFilterFormGroup.get('value').value ? 'value.true' : 'value.false') | translate }}
              </mat-checkbox>
            </ng-template>
          </div>
        </div>
      </section>
      <tb-filter-predicate-list *ngIf="keyFilterFormGroup.get('valueType').value"
                                [allowUserDynamicSource]="data.allowUserDynamicSource"
                                [onlyUserDynamicSource]="isConstantKeyType"
                                [displayUserParameters]="data.displayUserParameters"
                                [valueType]="keyFilterFormGroup.get('valueType').value"
                                [key]="keyFilterFormGroup.get('key.key').value"
                                formControlName="predicates">
      </tb-filter-predicate-list>
    </fieldset>
  </div>
  <div mat-dialog-actions fxLayoutAlign="end center">
    <button mat-button color="primary"
            type="button"
            [disabled]="(isLoading$ | async)"
            (click)="cancel()"
            cdkFocusInitial>
      {{ (data.readonly ? 'action.close' : 'action.cancel') | translate }}
    </button>
    <button mat-raised-button color="primary"
            type="submit"
            *ngIf="!data.readonly"
            [disabled]="(isLoading$ | async) || keyFilterFormGroup.invalid || !keyFilterFormGroup.dirty">
      {{ (data.isAdd ? 'action.add' : 'action.update') | translate }}
    </button>
  </div>
</form>
