<!--

    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.

-->
<div class="tb-container">
  <label class="tb-title" *ngIf="label" [ngClass]="{'tb-error': !disabled && (required && !imageUrls.length), 'tb-required': !disabled && required}">{{label}}</label>
  <div class="images-container" dndDropzone [dndHorizontal]="true" dndEffectAllowed="move"
       [dndDisableIf]="disabled" (dndDrop)="imageDrop($event)"
       fxLayout="row" fxLayoutGap="8px" [ngClass]="{'no-images': !imageUrls.length}">
    <div dndPlaceholderRef class="image-card image-dnd-placeholder"></div>
    <div *ngFor="let imageUrl of imageUrls; let $index = index;"
         (dndStart)="imageDragStart($index)"
         (dndEnd)="imageDragEnd()"
         [dndDraggable]="imageUrl"
         [dndDisableIf]="disabled"
         dndEffectAllowed="move"
         [ngClass]="{'image-dragging': dragIndex === $index}"
         class="image-card" fxLayout="column">
      <span class="image-title">{{ 'image-input.images' | translate }} [{{ $index }}]</span>
      <div class="image-content-container" fxLayout="row">
        <div dndHandle *ngIf="!disabled" class="tb-image-action-container tb-drag-handle">
          <mat-icon color="primary">drag_indicator</mat-icon>
        </div>
        <div class="tb-image-preview-container">
          <img class="tb-image-preview" [src]="imageUrl | image: {preview: true} | async" />
        </div>
        <div *ngIf="!disabled" class="tb-image-action-container">
          <button mat-icon-button color="primary"
                  type="button"
                  (click)="clearImage($index)"
                  matTooltip="{{ 'action.remove' | translate }}"
                  matTooltipPosition="above">
            <mat-icon>close</mat-icon>
          </button>
        </div>
      </div>
    </div>
    <div class="no-images-prompt" *ngIf="!imageUrls.length">{{ 'image-input.no-images' | translate }}</div>
  </div>
  <div *ngIf="!disabled" class="tb-image-select-container">
    <div class="tb-image-container" *ngIf="linkType === ImageLinkType.external">
      <img *ngIf="externalLinkControl.value; else noImage" class="tb-image-preview" [src]="externalLinkControl.value | image | async">
    </div>
    <div *ngIf="linkType === ImageLinkType.external" class="tb-image-info-container">
      <div class="tb-external-image-container">
        <div class="tb-external-link-label">
          {{ 'image.image-link' | translate }}
        </div>
        <div class="tb-external-link-input-container">
          <mat-form-field class="tb-inline-field" appearance="outline" subscriptSizing="dynamic">
            <input matInput [formControl]="externalLinkControl" placeholder="{{ 'widget-config.set' | translate }}">
          </mat-form-field>
          <button class="tb-image-decline-btn"
                  type="button"
                  mat-icon-button
                  matTooltip="{{ 'action.decline' | translate }}"
                  matTooltipPosition="above"
                  (click)="declineLink($event)">
            <mat-icon>close</mat-icon>
          </button>
          <button class="tb-image-apply-btn"
                  type="button"
                  [disabled]="!externalLinkControl.value"
                  color="primary"
                  mat-icon-button
                  matTooltip="{{ 'action.apply' | translate }}"
                  matTooltipPosition="above"
                  (click)="applyLink($event)">
            <mat-icon>check</mat-icon>
          </button>
        </div>
      </div>
    </div>
    <div *ngIf="linkType === ImageLinkType.none" class="tb-image-select-buttons-container">
      <button #browseGalleryButton
              mat-stroked-button
              type="button"
              color="primary"
              class="tb-image-select-button"
              (click)="toggleGallery($event, browseGalleryButton)">
        <tb-icon matButtonIcon>filter</tb-icon>
        <span translate>image.browse-from-gallery</span>
      </button>
      <button mat-stroked-button
              type="button"
              color="primary"
              class="tb-image-select-button"
              (click)="setLink($event)">
        <tb-icon matButtonIcon>link</tb-icon>
        <span translate>image.set-link</span>
      </button>
    </div>
  </div>
</div>

<ng-template #noImage>
  <div class="tb-no-image">{{ 'image.no-image-selected' | translate }}</div>
</ng-template>
