<!--

    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.

-->
<mat-card appearance="outlined" class="tb-bundle-widgets-card">
  <mat-card-header>
    <mat-card-title>
      <div class="title-container">
        <button mat-icon-button
                matTooltip="{{ 'action.back' | translate }}"
                matTooltipPosition="above"
                (click)="goBack()">
          <mat-icon>arrow_back</mat-icon>
        </button>
        <span class="mat-headline-5">{{ widgetsBundle.title }}: {{ 'widget.widgets' | translate }}</span>
        <span class="flex-1"></span>
        @if (!isReadOnly) {
          <button mat-icon-button [disabled]="isLoading$ | async"
                  matTooltip="{{ 'widget.add-new-widget' | translate }}"
                  matTooltipPosition="above"
                  [matMenuTriggerFor]="addWidgetMenu">
            <mat-icon>add</mat-icon>
          </button>
        }
        <mat-menu #addWidgetMenu="matMenu" xPosition="before">
          <button mat-menu-item (click)="addWidgetType($event)">
            <mat-icon>insert_drive_file</mat-icon>
            <span>{{ 'dashboard.create-new-widget' | translate }}</span>
          </button>
          <button mat-menu-item (click)="importWidgetType()">
            <mat-icon>file_upload</mat-icon>
            <span>{{ 'widget.import' | translate }}</span>
          </button>
        </mat-menu>
        <button mat-icon-button
                matTooltip="{{ 'widgets-bundle.export' | translate }}"
                matTooltipPosition="above"
                (click)="exportWidgetsBundle()">
          <mat-icon>file_download</mat-icon>
        </button>
      </div>
    </mat-card-title>
  </mat-card-header>
  @if (isLoading$ | async) {
    <mat-progress-bar color="warn" mode="indeterminate">
    </mat-progress-bar>
  }
  @if ((isLoading$ | async) === false) {
    <div style="height: 4px;"></div>
  }
  <mat-card-content>
    <div class="tb-drop-list tb-bundle-widgets-container" cdkDropList cdkDropListOrientation="vertical"
         (cdkDropListDropped)="widgetDrop($event)" [cdkDropListDisabled]="!editMode || widgets?.length < 2">
      @for (widget of widgets; track trackByWidget($index, widget); let isLast = $last) {
        <div cdkDrag class="tb-draggable tb-bundle-widget-row">
          <div class="tb-bundle-widget-row-details">
            <img class="tb-bundle-widget-image-preview" [src]="widget.image | image: {preview: true, emptyUrl: '/assets/widget-preview-empty.svg'} | async" alt="{{ widget.name }}">
            <div class="tb-bundle-widget-details">
              <div class="tb-bundle-widget-title">{{ widget.name }}</div>
              @if (widget.deprecated) {
                <div class="tb-bundle-widget-deprecated">{{ 'widget.deprecated' | translate }}</div>
              }
            </div>
          </div>
          @if (!editMode) {
            <button
              mat-icon-button
              (click)="openWidgetEditor($event, widget)"
              matTooltip="{{ 'widget.edit' | translate }}"
              matTooltipPosition="above">
              <mat-icon>edit</mat-icon>
            </button>
          }
          @if (!editMode) {
            <button
              mat-icon-button
              (click)="exportWidgetType($event, widget)"
              matTooltip="{{ 'widget.export' | translate }}"
              matTooltipPosition="above">
              <mat-icon>file_download</mat-icon>
            </button>
          }
          @if (editMode && !addMode) {
            <button
              mat-icon-button
              (click)="removeWidgetType($event, widget)"
              matTooltip="{{'widget.remove' | translate }}"
              matTooltipPosition="above">
              <mat-icon>delete</mat-icon>
            </button>
          }
          @if (editMode) {
            <button cdkDragHandle
                    [class.!hidden]="!widgets || widgets.length <= 1"
                    mat-icon-button
                    matTooltip="{{ 'action.drag' | translate }}"
                    matTooltipPosition="above"
                    class="tb-drag-handle">
              <tb-icon>drag_indicator</tb-icon>
            </button>
          }
        </div>
      }
    </div>
    @if (editMode && !addMode && widgets?.length) {
      <div class="tb-add-widget-button"
           matTooltip="{{ 'widget.add-existing-widget' | translate }}"
           matTooltipPosition="above"
           (click)="addWidgetMode()">
        <tb-icon color="primary" class="tb-add-widget-icon">add</tb-icon>
      </div>
    }
    @if (editMode && !addMode && !widgets?.length) {
      <div class="tb-add-widget-button-panel">
        <div class="tb-add-widget-button-with-text" (click)="addWidgetMode()">
          <tb-icon color="primary" class="tb-add-widget-icon">add</tb-icon>
          <div class="tb-add-widget-button-text" translate>widget.add-existing-widget</div>
        </div>
      </div>
    }
    @if (isReadOnly && !widgets?.length) {
      <div class="tb-no-data-available">
        <div class="tb-no-data-bg"></div>
        <div class="tb-no-data-text" translate>widget.no-widgets</div>
      </div>
    }
    @if (addMode) {
      <div class="tb-add-bundle-widget-form">
        <tb-widget-type-autocomplete
          required
          placeholder="{{ 'widget.select-widget' | translate }}"
          [excludeWidgetTypeIds]="excludeWidgetTypeIds"
          [formControl]="addWidgetFormControl">
        </tb-widget-type-autocomplete>
        <div class="tb-add-bundle-widget-actions">
          <button mat-button color="primary"
                  [disabled]="(isLoading$ | async)"
                  (click)="cancelAddWidgetMode()">{{'action.cancel' | translate}}
          </button>
        </div>
      </div>
    }
  </mat-card-content>
  <mat-card-actions>
    @if (editMode && (widgets?.length || isDirty)) {
      <button mat-button color="primary"
              [disabled]="(isLoading$ | async)"
              (click)="cancel()">{{'action.cancel' | translate}}
      </button>
    }
    @if (editMode && (widgets?.length || isDirty)) {
      <button mat-raised-button color="primary"
              [disabled]="(isLoading$ | async) || !isDirty"
              (click)="save()">{{'action.save' | translate}}
      </button>
    }
    @if (!isReadOnly && !editMode) {
      <button mat-raised-button color="primary"
              [disabled]="(isLoading$ | async)"
              (click)="edit()">{{'action.edit' | translate}}
      </button>
    }
  </mat-card-actions>
</mat-card>

