<!--

    Copyright © 2016-2023 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.

-->
<fieldset class="fields-group" [formGroup]="entityTypesVersionLoadFormGroup" fxLayout="column">
  <legend class="group-title" translate>version-control.entities-to-restore</legend>
  <div fxLayout="column" style="flex: 1; overflow: auto;">
    <div *ngFor="let entityTypeFormGroup of entityTypesFormGroupArray(); trackBy: trackByEntityType;
             let $index = index; last as isLast;"
         fxLayout="row" fxLayoutAlign="start center" [ngStyle]="!isLast ? {paddingBottom: '8px'} : {}">
      <mat-expansion-panel class="entity-type-config" [ngClass]="{'load': loading}" fxFlex [formGroup]="entityTypeFormGroup"
                           [expanded]="entityTypesFormGroupExpanded(entityTypeFormGroup)" (opened)="loading = false">
        <mat-expansion-panel-header>
          <div fxFlex fxLayout="row" fxLayoutAlign="start center">
            <mat-panel-title>
              <div fxLayout="row" fxFlex fxLayoutAlign="start center">
                <div>{{ entityTypeText(entityTypeFormGroup) }}</div>
              </div>
            </mat-panel-title>
            <span fxFlex></span>
            <button *ngIf="!disabled" mat-icon-button style="min-width: 40px;"
                    type="button"
                    (click)="removeEntityType($index)"
                    matTooltip="{{ 'action.remove' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>delete</mat-icon>
            </button>
          </div>
        </mat-expansion-panel-header>
        <div class="entity-type-config-content" fxLayout="column" fxLayoutGap="0.5em">
          <mat-divider></mat-divider>
          <div fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="32px">
            <tb-entity-type-select
              fxFlex
              showLabel
              formControlName="entityType"
              required
              [filterAllowedEntityTypes]="false"
              [allowedEntityTypes]="allowedEntityTypes(entityTypeFormGroup)">
            </tb-entity-type-select>
            <div fxFlex fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="16px" fxLayoutGap.xs="8px" formGroupName="config">
              <div fxFlex fxLayout="column">
                <mat-checkbox #removeOtherEntitiesCheckbox
                              formControlName="removeOtherEntities"
                              (change)="onRemoveOtherEntities(removeOtherEntitiesCheckbox, entityTypeFormGroup)">
                  {{ 'version-control.remove-other-entities' | translate }}
                </mat-checkbox>
                <mat-checkbox formControlName="findExistingEntityByName">
                  {{ 'version-control.find-existing-entity-by-name' | translate }}
                </mat-checkbox>
              </div>
              <div fxFlex fxLayout="column">
                <mat-checkbox *ngIf="entityTypeFormGroup.get('entityType').value === entityTypes.DEVICE" formControlName="loadCredentials">
                  {{ 'version-control.load-credentials' | translate }}
                </mat-checkbox>
                <mat-checkbox *ngIf="!entityTypesWithoutRelatedData.has(entityTypeFormGroup.get('entityType').value)" formControlName="loadAttributes">
                  {{ 'version-control.load-attributes' | translate }}
                </mat-checkbox>
                <mat-checkbox *ngIf="!entityTypesWithoutRelatedData.has(entityTypeFormGroup.get('entityType').value)" formControlName="loadRelations">
                  {{ 'version-control.load-relations' | translate }}
                </mat-checkbox>
              </div>
            </div>
          </div>
        </div>
      </mat-expansion-panel>
    </div>
    <div *ngIf="!entityTypesFormGroupArray().length">
          <span translate fxLayoutAlign="center center"
                class="tb-prompt">version-control.no-entities-to-restore-prompt</span>
    </div>
  </div>
  <div style="padding-top: 16px;" fxLayout="row">
    <button mat-raised-button color="primary"
            type="button"
            [disabled]="!addEnabled()"
            (click)="addEntityType()">
      <span translate>version-control.add-entity-type</span>
    </button>
    <span fxFlex></span>
    <button mat-raised-button color="primary"
            type="button"
            [disabled]="!entityTypesFormGroupArray().length"
            (click)="removeAll()">
      <span translate>version-control.remove-all</span>
    </button>
  </div>
</fieldset>
