<!--

    Copyright © 2016-2026 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">
  @if (label) {
    <label class="tb-title" [class]="{'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">
      @if (showPreview && (safeImageUrl || disabled)) {
        <div class="image-container" [class]="{'disabled': disabled}">
          <div class="tb-image-preview-container">
            @if (!safeImageUrl && disabled) {
              <div class="tb-image-preview-text">{{ 'dashboard.empty-image' | translate }}</div>
            } @else {
              <img class="tb-image-preview" [src]="safeImageUrl" />
            }
          </div>
          @if (safeImageUrl && showClearButton && !disabled) {
            <button
              mat-icon-button color="primary"
              class="tb-mat-24"
              type="button"
              (click)="clearImage()"
              matTooltip="{{ 'action.remove' | translate }}"
              matTooltipPosition="above">
              <mat-icon>close</mat-icon>
            </button>
          }
        </div>
      }
      @if (!disabled) {
        <div class="drop-area tb-flow-drop"
             flowDrop
             [flow]="flow.flowJs">
          <div class="upload-label">
            <mat-icon class="tb-mat-32">cloud_upload</mat-icon>
            <div class="upload-text-area">
              <span class="hide-xs" translate>image-input.drag-and-drop</span>
              <div class="upload-button-area">
                <span class="hide-xs" translate>image-input.or</span>
                <button type="button" mat-button color="primary" class="browse-file">
                  <label for="{{inputId}}">{{'image-input.browse' | translate}}</label>
                </button>
                <input class="file-input" flowButton type="file" [flow]="flow.flowJs" [flowAttributes]="{accept: accept}" id="{{inputId}}">
              </div>
            </div>
          </div>
        </div>
      }
      @if (!showPreview && safeImageUrl && showClearButton && !disabled) {
        <button
          mat-icon-button color="primary"
          class="tb-mat-24"
          type="button"
          (click)="clearImage()"
          matTooltip="{{ 'action.remove' | translate }}"
          matTooltipPosition="above">
          <mat-icon>close</mat-icon>
        </button>
      }
    </div>
  </ng-container>
  @if ((showFileName && fileName) || (maxSizeByte && !disabled)) {
    <div class="tb-image-info-container">
      @if (showFileName && fileName) {
        <div class="tb-image-file-name" [title]="fileName">{{ fileName }}</div>
      }
      @if (maxSizeByte && !disabled) {
        <div class="tb-image-file-hint" translate [translateParams]="{ size: maxSizeByte | fileSize}">dashboard.maximum-upload-file-size</div>
      }
    </div>
  }
</div>
