<!--

    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="mat-padding tb-entity-table tb-absolute-fill">
  <div class="tb-entity-table-content tb-outlined-border flex flex-1 flex-col">
    <mat-toolbar class="mat-mdc-table-toolbar" [class.!hidden]="mode !== 'default' || textSearchMode || !dataSource.selection.isEmpty()">
      <div class="mat-toolbar-tools">
        <div class="title-container flex flex-row items-center justify-start xs:flex-col xs:items-start xs:justify-center">
          <span class="tb-entity-table-title">{{telemetryTypeTranslationsMap.get(attributeScope) | translate}}</span>
          @if (!disableAttributeScopeSelection && !attributeScopeSelectionReadonly) {
            <mat-form-field class="mat-block tb-attribute-scope">
              <mat-label translate>attribute.attributes-scope</mat-label>
              <mat-select [disabled]="(isLoading$ | async)"
                          [ngModel]="attributeScope"
                          (ngModelChange)="attributeScopeChanged($event)">
                @for (scope of attributeScopes; track scope) {
                  <mat-option [value]="scope">
                    {{ telemetryTypeTranslationsMap.get(toTelemetryTypeFunc(scope)) | translate }}
                  </mat-option>
                }
              </mat-select>
            </mat-form-field>
          }
        </div>
        <span class="flex-1"></span>
        @if (attributeScope !== attributeScopeTypes.CLIENT_SCOPE && !disableAddTelemetry) {
          <button mat-icon-button
                  [disabled]="isLoading$ | async"
                  (click)="addAttribute($event)"
                  matTooltip="{{ 'action.add' | translate }}"
                  matTooltipPosition="above">
            <mat-icon>add</mat-icon>
          </button>
        }
        @if (!isClientSideTelemetryTypeMap.get(attributeScope)) {
          <button mat-icon-button
                  [disabled]="isLoading$ | async"
                  (click)="reloadAttributes()"
                  matTooltip="{{ 'action.refresh' | translate }}"
                  matTooltipPosition="above">
            <mat-icon>refresh</mat-icon>
          </button>
        }
        <button mat-icon-button
                [disabled]="isLoading$ | async"
                (click)="enterFilterMode()"
                matTooltip="{{ 'action.search' | translate }}"
                matTooltipPosition="above">
          <mat-icon>search</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <mat-toolbar class="mat-mdc-table-toolbar" [class.!hidden]="mode !== 'default' || !textSearchMode || !dataSource.selection.isEmpty()">
      <div class="mat-toolbar-tools">
        <button mat-icon-button
                matTooltip="{{ 'action.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="{{ 'common.enter-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 !== 'default' || dataSource.selection.isEmpty()">
      <div class="mat-toolbar-tools xs:flex xs:flex-row xs:flex-wrap">
        <span class="tb-entity-table-info">
          {{ (attributeScope === latestTelemetryTypes.LATEST_TELEMETRY ?
          'attribute.selected-telemetry' : 'attribute.selected-attributes') | translate:{count: dataSource.selection.selected.length} }}
        </span>
        <span class="flex-1"></span>
        <button [class.!hidden]="attributeScope === attributeScopeTypes.CLIENT_SCOPE"
                class="button-widget-action"
                mat-icon-button [disabled]="isLoading$ | async"
                matTooltip="{{ 'action.delete' | translate }}"
                matTooltipPosition="above"
                (click)="deleteTelemetry($event)">
          <mat-icon>delete</mat-icon>
        </button>
        <button [class.!hidden]="isSysAdmin"
                mat-raised-button color="accent"
                class="button-widget-action"
                [disabled]="isLoading$ | async"
                matTooltip="{{ 'attribute.show-on-widget' | translate }}"
                matTooltipPosition="above"
                (click)="enterWidgetMode()">
          <mat-icon>now_widgets</mat-icon>
          <span translate>attribute.show-on-widget</span>
        </button>
      </div>
    </mat-toolbar>
    <mat-toolbar class="mat-mdc-table-toolbar" color="primary" [class.!hidden]="mode !== 'widget'">
      <div class="mat-toolbar-tools gap-2 xs:flex xs:flex-row xs:flex-wrap">
        <div class="flex flex-1 flex-row items-stretch justify-start">
          <span class="tb-details-subtitle">{{ 'widgets-bundle.current' | translate }}</span>
          <tb-widgets-bundle-select class="flex-1"
                                    style="margin-left: 5%"
                                    required
                                    [selectFirstBundle]="false"
                                    [selectBundleAlias]="selectedWidgetsBundleAlias"
                                    [ngModel]="null"
                                    (ngModelChange)="onWidgetsBundleChanged($event)">
          </tb-widgets-bundle-select>
        </div>
        <button mat-raised-button [class.!hidden]="!widgetsList.length"
                color="accent"
                class="button-widget-action"
                [disabled]="isLoading$ | async"
                matTooltip="{{ 'attribute.add-to-dashboard' | translate }}"
                matTooltipPosition="above"
                (click)="addWidgetToDashboard()">
          <mat-icon>dashboard</mat-icon>
          <span translate>attribute.add-to-dashboard</span>
        </button>
        <button mat-icon-button
                [disabled]="isLoading$ | async"
                matTooltip="{{ 'action.close' | translate }}"
                matTooltipPosition="above"
                (click)="exitWidgetMode()">
          <mat-icon>close</mat-icon>
        </button>
      </div>
    </mat-toolbar>
    <div class="table-container flex-1" [class.!hidden]="mode === 'widget'">
      <table mat-table [dataSource]="dataSource"
             matSort [matSortActive]="pageLink.sortOrder.property" [matSortDirection]="pageLink.sortDirection()" matSortDisableClear>
        <ng-container matColumnDef="select" sticky>
          <mat-header-cell *matHeaderCellDef style="width: 40px;">
            <mat-checkbox (change)="$event ? dataSource.masterToggle() : null"
                          [(ngModel)]="selectAllModel"
                          [indeterminate]="dataSource.selection.hasValue() && !(dataSource.isAllSelected() | async)">
            </mat-checkbox>
          </mat-header-cell>
          <mat-cell *matCellDef="let attribute">
            <mat-checkbox (click)="$event.stopPropagation()"
                          (change)="$event ? dataSource.selection.toggle(attribute) : null"
                          [checked]="dataSource.selection.isSelected(attribute)">
            </mat-checkbox>
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="lastUpdateTs">
          <mat-header-cell *matHeaderCellDef mat-sort-header style="min-width: 150px; max-width: 150px; width: 150px;"> {{ 'attribute.last-update-time' | translate }} </mat-header-cell>
          <mat-cell *matCellDef="let attribute">
            {{ attribute.lastUpdateTs | date:'yyyy-MM-dd HH:mm:ss' }}
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="key">
          <mat-header-cell *matHeaderCellDef mat-sort-header style="width: 40%"> {{ 'attribute.key' | translate }} </mat-header-cell>
          <mat-cell *matCellDef="let attribute" class="attribute">
            <div class="flex flex-row" style="align-items: center">
              <span class="ellipsis">{{ attribute.key | tbJson:300 }}</span>
              <tb-copy-button
                class="attribute-copy"
                [disabled]="isLoading$ | async"
                [copyText]="attribute.key"
                tooltipText="{{ 'attribute.copy-key' | translate }}"
                tooltipPosition="above"
                icon="content_copy"
                [style]="{padding: '4px', 'font-size': '16px', color: 'rgba(0,0,0,.87)'}"
                >
              </tb-copy-button>
            </div>
          </mat-cell>
        </ng-container>
        <ng-container matColumnDef="value">
          <mat-header-cell *matHeaderCellDef style="width: 60%"> {{ 'attribute.value' | translate }} </mat-header-cell>
          <mat-cell *matCellDef="let attribute"
                    class="tb-value-cell attribute"
                    (click)="editAttribute($event, attribute)">
            <div class="flex flex-1 flex-row" style="align-items: center">
              <div class="flex flex-1 flex-row" style="overflow: hidden; align-items: center;">
                <span class="ellipsis">{{attribute.value | tbJson:300}}</span>
                @if (attribute.value !== '') {
                  <tb-copy-button
                    class="attribute-copy"
                    [disabled]="isLoading$ | async"
                    [copyText]="attribute.value | tbJson"
                    tooltipText="{{ 'attribute.copy-value' | translate }}"
                    tooltipPosition="above"
                    icon="content_copy"
                    [style]="{padding: '4px', 'font-size': '16px', color: 'rgba(0,0,0,.87)'}"
                    >
                  </tb-copy-button>
                }
              </div>
              <span [class.!hidden]="isClientSideTelemetryTypeMap.get(attributeScope)">
                <mat-icon>edit</mat-icon>
              </span>
              @if (attributeScope === latestTelemetryTypes.LATEST_TELEMETRY) {
                <button mat-icon-button
                        (click)="deleteTimeseries($event, attribute)">
                  <mat-icon>delete</mat-icon>
                </button>
              }
            </div>
          </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.mat-selected]="dataSource.selection.isSelected(attribute)"
        *matRowDef="let attribute; columns: displayedColumns;" (click)="dataSource.selection.toggle(attribute)"></mat-row>
      </table>
      <span [class.!hidden]="(dataSource.isEmpty() | async) === false"
            class="no-data-found flex items-center justify-center" translate>{{
            attributeScope === latestTelemetryTypes.LATEST_TELEMETRY
            ? 'attribute.no-telemetry-text'
            : 'attribute.no-attributes-text'
      }}</span>
    </div>
    <mat-divider [class.!hidden]="mode === 'widget'"></mat-divider>
    <mat-paginator [class.!hidden]="mode === 'widget'"
                   [length]="dataSource.total() | async"
                   [pageIndex]="pageLink.page"
                   [pageSize]="pageLink.pageSize"
                   [pageSizeOptions]="[10, 20, 30]"
                   [hidePageSize]="hidePageSize"
    showFirstLastButtons></mat-paginator>
    @if (mode === 'widget' && widgetsList.length > 0) {
      <ngx-hm-carousel
        #carousel
        [(ngModel)]="widgetsCarouselIndex"
        (ngModelChange)="onWidgetsCarouselIndexChanged()"
        [data]="widgetsList"
        [infinite]="false"
        class="carousel c-accent flex-1">
        <section ngx-hm-carousel-container class="content">
          @for (widgets of widgetsList; track widgets) {
            <article class="item cursor-pointer"
                     ngx-hm-carousel-item
                     >
              <tb-dashboard class="tb-absolute-fill"
                            [aliasController]="aliasController"
                            [widgets]="widgets"
                            [columns]="20"
                            [isEdit]="false"
                            [isPreview]="true"
                            [isMobileDisabled]="true"
                            [isEditActionEnabled]="false"
                            [isRemoveActionEnabled]="false">
              </tb-dashboard>
            </article>
          }
        </section>
        <ng-template #carouselPrev>
          @if (widgetsCarouselIndex > 0) {
            <button mat-icon-button
                    matTooltip="{{ 'attribute.prev-widget' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>keyboard_arrow_left</mat-icon>
            </button>
          }
        </ng-template>
        <ng-template #carouselNext>
          @if (widgetsCarouselIndex < widgetsList.length - 1) {
            <button mat-icon-button
                    matTooltip="{{ 'attribute.next-widget' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>keyboard_arrow_right</mat-icon>
            </button>
          }
        </ng-template>
        <ng-template #carouselDot let-model>
          <div class="ball"
          [class.visible]="model.index === model.currentIndex"></div>
        </ng-template>
      </ngx-hm-carousel>
    }
    @if (mode === 'widget' && widgetsLoaded &&
      widgetsList.length === 0 &&
      widgetBundleSet) {
      <span translate
      class="mat-headline-5 flex flex-1 items-center justify-center">widgets-bundle.empty</span>
    }
    @if (mode === 'widget' && !widgetBundleSet) {
      <span translate
      class="mat-headline-5 flex flex-1 items-center justify-center">widget.select-widgets-bundle</span>
    }
  </div>
</div>
