<!--

    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-images tb-absolute-fill" [class.tb-dialog-mode]="dialogMode" [class.mat-padding]="!dialogMode">
  <div class="tb-images-content flex flex-1 flex-col" [class.tb-outlined-border]="pageMode">
    <mat-toolbar class="mat-mdc-table-toolbar lt-lg:flex lt-lg:flex-col lt-lg:items-stretch lt-lg:justify-start"
                 [class.!hidden]="textSearchMode || !(mode === 'grid' || dataSource?.selection.isEmpty())"
                 [class.multi-row]="!isSysAdmin">
      <div class="flex flex-1 flex-row items-center justify-start">
        <div class="mat-toolbar-tools">
          <div class="tb-images-title lt-md:!hidden">
            <div tbTruncateWithTooltip>{{ (isScada ? 'scada.symbols' : 'image.gallery') | translate }}</div>
            @if (isScada) {
              <div tb-help="scada"></div>
            }
          </div>
          <div class="tb-images-view-type-toolbar flex flex-row items-center justify-start">
            <div class="tb-toolbar-button" [class.tb-selected]="mode === 'list'">
              <button mat-icon-button
                      (click)="setMode('list')"
                      matTooltip="{{'image.list-mode' | translate }}"
                      matTooltipPosition="below">
                <mat-icon>view_list</mat-icon>
              </button>
            </div>
            <div class="tb-toolbar-button" [class.tb-selected]="mode === 'grid'">
              <button mat-icon-button
                      (click)="setMode('grid');"
                      matTooltip="{{'image.grid-mode' | translate }}"
                      matTooltipPosition="below">
                <tb-icon>mdi:view-grid</tb-icon>
              </button>
            </div>
          </div>
          @if (!isSysAdmin) {
            <mat-slide-toggle
              class="lt-lg:!hidden"
              [ngModel]="includeSystemImages"
            (ngModelChange)="includeSystemImagesChanged($event)">{{ (isScada ? 'scada.include-system-symbols' : 'image.include-system-images') | translate }}</mat-slide-toggle>
          }
        </div>
        <section class="flex flex-1 flex-row items-center justify-start gt-md:gap-4">
          <span class="flex-1"></span>
          <section class="flex flex-row items-center justify-start">
            <button [disabled]="isLoading$ | async"
                    mat-icon-button
                    (click)="enterFilterMode()"
                    matTooltip="{{'action.search' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>search</mat-icon>
            </button>
            <button [disabled]="isLoading$ | async"
                    mat-icon-button
                    (click)="updateData()"
                    matTooltip="{{'action.refresh' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>refresh</mat-icon>
            </button>
            <button [disabled]="isLoading$ | async"
                    mat-icon-button
                    (click)="importImage()"
                    matTooltip="{{ (isScada ? 'scada.import-symbol' : 'image.import-image' ) | translate }}"
                    matTooltipPosition="above">
              <tb-icon>mdi:file-import</tb-icon>
            </button>
            <button [class.!hidden]="dialogMode && isScada"
                    class="ml-3 gt-md:!hidden"
                    [disabled]="isLoading$ | async"
                    mat-mini-fab color="primary"
                    style="--mat-fab-small-container-elevation-shadow: none;"
                    (click)="uploadImage()"
                    matTooltip="{{ (isScada ? 'scada.upload-symbol' : 'image.upload-image' ) | translate }}"
                    matTooltipPosition="above">
              <mat-icon>add</mat-icon>
            </button>
          </section>
          <button [class.!hidden]="dialogMode && isScada"
                  class="lt-lg:!hidden"
                  [disabled]="isLoading$ | async"
                  mat-flat-button
                  color="primary"
                  (click)="uploadImage()">
            {{ (isScada ? 'scada.upload-symbol' : 'image.upload-image' ) | translate }}
          </button>
        </section>
      </div>
      @if (!isSysAdmin) {
        <mat-slide-toggle class="gt-md:!hidden"
                          [ngModel]="includeSystemImages"
        (ngModelChange)="includeSystemImagesChanged($event)">{{ (isScada ? 'scada.include-system-symbols' : 'image.include-system-images') | translate }}</mat-slide-toggle>
      }
    </mat-toolbar>
    <mat-toolbar class="mat-mdc-table-toolbar" [class.!hidden]="!textSearchMode || !(mode === 'grid' || dataSource?.selection.isEmpty())">
      <div class="mat-toolbar-tools">
        <button mat-icon-button
                matTooltip="{{ (isScada ? 'scada.search' : 'image.search' ) | translate }}"
                matTooltipPosition="above">
          <mat-icon>search</mat-icon>
        </button>
        <mat-form-field class="flex-1">
          <mat-label>&nbsp;</mat-label>
          <input #searchInput matInput
                 [formControl]="textSearch"
                 placeholder="{{ (isScada ? 'scada.search' : 'image.search' ) | translate }}"/>
        </mat-form-field>
        <button mat-icon-button (click)="exitFilterMode()"
                matTooltip="{{ 'action.close' | translate }}"
                matTooltipPosition="above">
          <mat-icon>close</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <mat-toolbar class="mat-mdc-table-toolbar" color="primary" [class.!hidden]="mode !== 'list' || dataSource?.selection.isEmpty()">
      <div class="mat-toolbar-tools">
        <span>
          {{ translate.get((isScada ? 'scada.selected-symbols' : 'image.selected-images' ), {count: dataSource?.selection.selected.length}) | async }}
        </span>
        <span class="flex-1"></span>
        <button mat-icon-button [disabled]="isLoading$ | async"
                matTooltip="{{ 'action.delete' | translate }}"
                matTooltipPosition="above"
                (click)="deleteImages($event)">
          <mat-icon>delete</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    @if (mode === 'list') {
      <div class="flex flex-1 flex-col min-h-0">
        <div class="table-container flex-1">
          <table mat-table [dataSource]="dataSource" [trackBy]="trackByEntity"
                 matSort [matSortActive]="pageLink.sortOrder.property" [matSortDirection]="pageLink.sortDirection()" matSortDisableClear>
            <ng-container matColumnDef="select" sticky>
              <mat-header-cell *matHeaderCellDef style="width: 30px;">
                <mat-checkbox (change)="$event ? dataSource.masterToggle() : null"
                              [checked]="dataSource.selection.hasValue() && (dataSource.isAllSelected() | async)"
                              [indeterminate]="dataSource.selection.hasValue() && (dataSource.isAllSelected() | async) === false">
                </mat-checkbox>
              </mat-header-cell>
              <mat-cell *matCellDef="let image">
                <mat-checkbox (click)="$event.stopPropagation()"
                              [class.!hidden]="!deleteEnabled(image)"
                              (change)="$event ? dataSource.selection.toggle(image) : null"
                              [checked]="dataSource.selection.isSelected(image)">
                </mat-checkbox>
              </mat-cell>
            </ng-container>
            <ng-container matColumnDef="preview">
              <mat-header-cell *matHeaderCellDef style="width: 50px; min-width: 50px;"></mat-header-cell>
              <mat-cell *matCellDef="let image">
                <div class="tb-image-preview-cell">
                  <img class="tb-image-preview" [src]="image.link | image: {preview: true} | async" alt="{{ image.title }}">
                </div>
              </mat-cell>
            </ng-container>
            <ng-container matColumnDef="title">
              <mat-header-cell *matHeaderCellDef mat-sort-header style="width: 100%;"> {{ 'image.name' | translate }} </mat-header-cell>
              <mat-cell *matCellDef="let image">
                {{ image.title }}
              </mat-cell>
            </ng-container>
            <ng-container matColumnDef="createdTime">
              <mat-header-cell *matHeaderCellDef mat-sort-header style="width: 160px; min-width: 160px;"> {{ 'image.created-time' | translate }} </mat-header-cell>
              <mat-cell *matCellDef="let image">
                {{ image.createdTime | date:'yyyy-MM-dd HH:mm:ss' }}
              </mat-cell>
            </ng-container>
            <ng-container matColumnDef="resolution">
              <mat-header-cell *matHeaderCellDef style="width: 80px; min-width: 80px;"> {{ 'image.resolution' | translate }} </mat-header-cell>
              <mat-cell *matCellDef="let image">
                {{ image.descriptor.width }}x{{ image.descriptor.height }}
              </mat-cell>
            </ng-container>
            <ng-container matColumnDef="size">
              <mat-header-cell *matHeaderCellDef style="width: 80px; min-width: 80px;"> {{ 'image.size' | translate }} </mat-header-cell>
              <mat-cell *matCellDef="let image">
                {{ image.descriptor.size | fileSize }}
              </mat-cell>
            </ng-container>
            <ng-container matColumnDef="system">
              <mat-header-cell *matHeaderCellDef style="width: 60px; min-width: 60px;"> {{ 'image.system' | translate }} </mat-header-cell>
              <mat-cell *matCellDef="let image">
                <mat-icon class="material-icons mat-icon">{{isSystem(image) ? 'check_box' : 'check_box_outline_blank'}}</mat-icon>
              </mat-cell>
            </ng-container>
            <ng-container matColumnDef="actions" stickyEnd>
              <mat-header-cell *matHeaderCellDef
                               [class.gt-md:min-w-60]="!isScada"
                               [class.gt-md:max-w-60]="!isScada"
                               [class.gt-md:w-60]="!isScada"
                               [class.gt-md:min-w-48]="isScada"
                               [class.gt-md:max-w-48]="isScada"
                               [class.gt-md:w-48]="isScada">
              </mat-header-cell>
              <mat-cell *matCellDef="let image"
                        [class.gt-md:min-w-60]="!isScada"
                        [class.gt-md:max-w-60]="!isScada"
                        [class.gt-md:w-60]="!isScada"
                        [class.gt-md:min-w-48]="isScada"
                        [class.gt-md:max-w-48]="isScada"
                        [class.gt-md:w-48]="isScada">
                <div class="flex flex-1 flex-row items-stretch justify-end lt-lg:!hidden">
                  <button mat-icon-button [disabled]="isLoading$ | async"
                          matTooltip="{{ (isScada ? 'scada.download-symbol' : 'image.download-image' ) | translate }}"
                          matTooltipPosition="above"
                          (click)="downloadImage($event, image)">
                    <mat-icon>file_download</mat-icon>
                  </button>
                  <button mat-icon-button [disabled]="isLoading$ | async"
                          matTooltip="{{ (isScada ? 'scada.export-symbol' : 'image.export-image' ) | translate }}"
                          matTooltipPosition="above"
                          (click)="exportImage($event, image)">
                    <tb-icon>mdi:file-export</tb-icon>
                  </button>
                  @if (!isScada) {
                    <button mat-icon-button [disabled]="isLoading$ | async"
                            matTooltip="{{ 'image.embed-image' | translate }}"
                            matTooltipPosition="above"
                            (click)="embedImage($event, image)">
                      <mat-icon>code</mat-icon>
                    </button>
                  }
                  <button mat-icon-button [disabled]="isLoading$ | async"
                          matTooltip="{{ (readonly(image) ? (isScada ? 'scada.symbol-details' : 'image.image-details') : (isScada ? 'scada.edit-symbol' : 'image.edit-image')) | translate }}"
                          matTooltipPosition="above"
                          (click)="editImage($event, image)">
                    <mat-icon>edit</mat-icon>
                  </button>
                  <button mat-icon-button [disabled]="(isLoading$ | async) || !deleteEnabled(image)"
                          matTooltip="{{ (isScada ? 'scada.delete-symbol' : 'image.delete-image' ) | translate }}"
                          matTooltipPosition="above"
                          (click)="deleteImage($event, image)">
                    <mat-icon>delete</mat-icon>
                  </button>
                </div>
                <div class="gt-md:!hidden">
                  <button mat-icon-button
                          (click)="$event.stopPropagation()"
                          [matMenuTriggerFor]="cellActionsMenu">
                    <mat-icon class="material-icons">more_vert</mat-icon>
                  </button>
                  <mat-menu #cellActionsMenu="matMenu" xPosition="before">
                    <button mat-menu-item
                            [disabled]="isLoading$ | async"
                            (click)="downloadImage($event, image)">
                      <mat-icon>file_download</mat-icon>
                      <span>{{ (isScada ? 'scada.download-symbol' : 'image.download-image' ) | translate }}</span>
                    </button>
                    <button mat-menu-item
                            [disabled]="isLoading$ | async"
                            (click)="exportImage($event, image)">
                      <tb-icon matMenuItemIcon>mdi:file-export</tb-icon>
                      <span>{{ (isScada ? 'scada.export-symbol' : 'image.export-image' ) | translate }}</span>
                    </button>
                    @if (!isScada) {
                      <button mat-menu-item
                              [disabled]="isLoading$ | async"
                              (click)="embedImage($event, image)">
                        <mat-icon>code</mat-icon>
                        <span>{{ 'image.embed-image' | translate }}</span>
                      </button>
                    }
                    <button mat-menu-item
                            [disabled]="isLoading$ | async"
                            (click)="editImage($event, image)">
                      <mat-icon>edit</mat-icon>
                      <span>{{ (readonly(image) ? (isScada ? 'scada.symbol-details' : 'image.image-details') : (isScada ? 'scada.edit-symbol' : 'image.edit-image')) | translate }}</span>
                    </button>
                    <button mat-menu-item
                            [disabled]="isLoading$ | async"
                            [class.!hidden]="!deleteEnabled(image)"
                            (click)="deleteImage($event, image)">
                      <mat-icon>delete</mat-icon>
                      <span>{{ (isScada ? 'scada.delete-symbol' : 'image.delete-image' ) | translate }}</span>
                    </button>
                  </mat-menu>
                </div>
              </mat-cell>
            </ng-container>
            <ng-container matColumnDef="imageSelect" stickyEnd>
              <mat-header-cell *matHeaderCellDef></mat-header-cell>
              <mat-cell *matCellDef="let image">
                <button mat-stroked-button
                        color="primary"
                (click)="selectImage($event, image)">{{ 'action.select' | translate }}</button>
              </mat-cell>
            </ng-container>
            <mat-header-row class="mat-row-select" *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
            <mat-row class="mat-row-select"
                     [class.!hidden]="dataSource.dataLoading"
                     [class.mat-selected]="dataSource.selection.isSelected(image)"
            *matRowDef="let image; columns: displayedColumns;" (click)="rowClick($event, image)"></mat-row>
          </table>
          @if ((dataSource.isEmpty() | async) && !dataSource.dataLoading) {
            <ng-container *ngTemplateOutlet="noImages"></ng-container>
          }
          <span [class.!hidden]="!dataSource.dataLoading"
          class="no-data-found flex items-center justify-center">{{ 'common.loading' | translate }}</span>
        </div>
        <mat-divider></mat-divider>
        <mat-paginator [length]="dataSource.total() | async"
                       [pageIndex]="pageLink.page"
                       [pageSize]="pageLink.pageSize"
                       [pageSizeOptions]="pageSizeOptions"
                       [hidePageSize]="hidePageSize"
        showFirstLastButtons></mat-paginator>
      </div>
    }
    @if (mode === 'grid') {
      <div class="flex flex-1 flex-col" [class.mat-padding]="!dialogMode">
        <tb-scroll-grid class="flex-1"
                        [columns]="gridColumns"
                        [itemSize]="gridImagesItemSizeStrategy"
                        [fetchFunction]="gridImagesFetchFunction"
                        [filter]="gridImagesFilter"
                        [itemCard]="imageCard"
                        [loadingCell]="imageLoadingCard"
                        [dataLoading]="loadingImages"
                        [noData]="noImages">
        </tb-scroll-grid>
      </div>
    }
  </div>
</div>
<ng-template #loadingImages>
  <div class="tb-absolute-fill flex flex-col items-center justify-center">
    <span>
      {{ 'common.loading' | translate }}
    </span>
  </div>
</ng-template>
<ng-template #noImages>
  <div class="tb-no-images">
    <div class="tb-no-data-bg"></div>
    <div class="tb-no-data-text">{{ (isScada ? 'scada.no-symbols' : 'image.no-images') | translate }}</div>
  </div>
</ng-template>
<ng-template #imageCard let-item="item" let-itemIndex="itemIndex">
  <div class="tb-image-card tb-primary-fill">
    <div class="tb-image-card-overlay">
      @if (!selectionMode) {
        <div class="tb-image-card-overlay-buttons">
          <button mat-icon-button [disabled]="isLoading$ | async"
                  matTooltip="{{ (isScada ? 'scada.download-symbol' : 'image.download-image' ) | translate }}"
                  matTooltipPosition="above"
                  (click)="downloadImage($event, item)">
            <mat-icon>file_download</mat-icon>
          </button>
          <button mat-icon-button [disabled]="isLoading$ | async"
                  matTooltip="{{ (isScada ? 'scada.export-symbol' : 'image.export-image' ) | translate }}"
                  matTooltipPosition="above"
                  (click)="exportImage($event, item)">
            <tb-icon>mdi:file-export</tb-icon>
          </button>
          @if (!isScada) {
            <button mat-icon-button [disabled]="isLoading$ | async"
                    matTooltip="{{ 'image.embed-image' | translate }}"
                    matTooltipPosition="above"
                    (click)="embedImage($event, item, itemIndex)">
              <mat-icon>code</mat-icon>
            </button>
          }
          @if (deleteEnabled(item)) {
            <button
              mat-icon-button [disabled]="(isLoading$ | async)"
              matTooltip="{{ (isScada ? 'scada.delete-symbol' : 'image.delete-image' ) | translate }}"
              matTooltipPosition="above"
              (click)="deleteImage($event, item, itemIndex)">
              <mat-icon>delete</mat-icon>
            </button>
          }
        </div>
      }
    </div>
    <div class="tb-image-preview-container">
      <div class="tb-image-preview-overlay" (click)="selectionMode && selectImage($event, item)">
        @if (!selectionMode) {
          <button
            mat-flat-button
            color="primary"
            (click)="editImage($event, item, itemIndex)">
            {{ (readonly(item) ? (isScada ? 'scada.symbol-details' : 'image.image-details') : (isScada ? 'scada.edit-symbol' : 'image.edit-image')) | translate }}
          </button>
        }
        @if (selectionMode) {
          <button
            mat-flat-button
            color="primary"
            (click)="selectImage($event, item)">{{ 'action.select' | translate }}
          </button>
        }
      </div>
      <div class="tb-image-preview-spacer"></div>
      <img class="tb-image-preview" [src]="item.link | image: {preview: true} | async" alt="{{ item.title }}">
    </div>
    <div class="tb-image-details">
      <div class="tb-image-title-container">
        <div class="tb-image-title">
          {{ item.title }}
        </div>
        @if (isSystem(item)) {
          <div class="tb-image-sys">sys</div>
        }
      </div>
      <div class="tb-image-info-container">
        <div>{{ item.descriptor.width }}x{{ item.descriptor.height }}</div>
        <mat-divider vertical></mat-divider>
        <div>{{ item.descriptor.size | fileSize }}</div>
      </div>
    </div>
  </div>
</ng-template>
<ng-template #imageLoadingCard>
  <div class="tb-image-card tb-primary-fill loading-cell">
    <div class="tb-image-preview-container">
      <div class="tb-image-preview-spacer"></div>
      <div class="tb-image-preview"></div>
    </div>
    <div class="tb-image-details"></div>
  </div>
</ng-template>
