<!--

    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 class="tb-container">
  <label class="tb-title" *ngIf="label" [ngClass]="{'tb-error': !disabled && (required && !safeImageUrl), 'tb-required': !disabled && required}">{{label}}</label>
  <ng-container #flow="flow"
                [flowConfig]="{singleFile: true, allowDuplicateUploads: true}">
    <div class="tb-image-select-container">
      <div class="image-container" *ngIf="showPreview">
        <div class="image-content-container" [ngClass]="{'no-padding': disabled}">
          <div class="tb-image-preview-container">
            <div *ngIf="!safeImageUrl; else elseBlock">{{ (disabled ? 'dashboard.empty-image' : 'dashboard.no-image') | translate }}</div>
            <ng-template #elseBlock><img class="tb-image-preview" [src]="safeImageUrl" /></ng-template>
          </div>
          <div *ngIf="showClearButton && !disabled" class="tb-image-clear-container">
            <button mat-button mat-icon-button color="primary"
                    type="button"
                    (click)="clearImage()"
                    matTooltip="{{ 'action.remove' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>close</mat-icon>
            </button>
          </div>
        </div>
      </div>
      <div *ngIf="!disabled" class="drop-area tb-flow-drop"
           [ngClass]="{'float-left': !showPreview && showClearButton}"
           flowDrop
           [flow]="flow.flowJs">
        <div class="upload-label">
          <mat-icon>cloud_upload</mat-icon>
          <span translate>image-input.drop-image-or</span>
          <button type="button" mat-button color="primary" class="browse-file">
            <label for="{{inputId}}">{{'file-input.browse-file' | translate}}</label>
          </button>
          <input class="file-input" flowButton type="file" [flow]="flow.flowJs" [flowAttributes]="{accept: 'image/*'}" id="{{inputId}}">
        </div>
      </div>
      <div *ngIf="!showPreview && showClearButton && !disabled" class="tb-image-clear-container full-height">
        <button mat-button mat-icon-button color="primary"
                type="button"
                (click)="clearImage()"
                matTooltip="{{ 'action.remove' | translate }}"
                matTooltipPosition="above">
          <mat-icon>close</mat-icon>
        </button>
      </div>
    </div>
  </ng-container>
  <div class="tb-hint" *ngIf="maxSizeByte && !disabled" translate [translateParams]="{ size: maxSizeByte | fileSize}">dashboard.maximum-upload-file-size</div>
</div>
