<!--

    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.

-->
<form class="tb-edit-links-dialog" [formGroup]="editLinksFormGroup">
  <mat-toolbar style="background: transparent;">
    <h2>{{ (mode === 'docs' ? 'widgets.documentation.title' : 'widgets.quick-links.title') | translate }}</h2>
    <span class="flex-1"></span>
    <button mat-icon-button
            (click)="close()"
            type="button">
      <tb-icon class="material-icons">close</tb-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content>
    <div class="tb-drop-list" cdkDropList cdkDropListOrientation="vertical"
         (cdkDropListDropped)="linkDrop($event)" [cdkDropListDisabled]="editMode || addMode">
      @for (linkControl of linksFormArray().controls; track trackByLink($index, linkControl); let isLast = $last) {
        <div cdkDrag class="tb-draggable flex flex-row items-center justify-start"
             [class.pb-2]="!isLast">
          @switch (mode) {
            @case ('docs') {
              <tb-doc-link class="flex-1" #docLink [formControl]="linkControl"
                           [disableEdit]="editMode || addMode"
                           (editModeChanged)="editMode = $event"
                           (docLinkUpdated)="update()"
                           (docLinkDeleted)="deleteLink($index)">
              </tb-doc-link>
              @if (!docLink.isEditing()) {
                <ng-container *ngTemplateOutlet="dragHandle"></ng-container>
              }
            }
            @case ('quickLinks') {
              <tb-quick-link class="flex-1" #quickLink [formControl]="linkControl"
                             [disableEdit]="editMode || addMode"
                             (editModeChanged)="editMode = $event"
                             (quickLinkUpdated)="update()"
                             (quickLinkDeleted)="deleteLink($index)">
              </tb-quick-link>
              @if (!quickLink.isEditing()) {
                <ng-container *ngTemplateOutlet="dragHandle"></ng-container>
              }
            }
          }
          <ng-template #dragHandle>
            <div cdkDragHandle
                 matTooltip="{{ 'action.drag' | translate }}"
                 matTooltipPosition="above"
                 class="tb-drag-handle">
              <tb-icon>drag_indicator</tb-icon>
            </div>
          </ng-template>
        </div>
      }
    </div>
  </div>
  <div mat-dialog-actions>
    @if (!editMode && !addMode) {
      <div class="tb-add-link-button flex-1"
           matTooltip="{{ (mode === 'docs' ? 'widgets.documentation.add-link' : 'widgets.quick-links.add-link') | translate }}"
           matTooltipPosition="above"
           (click)="addLink()">
        <tb-icon class="tb-add-icon">add</tb-icon>
      </div>
    }
    @if (addMode) {
      @switch (mode) {
        @case ('docs') {
          <tb-doc-link class="flex-1" [(ngModel)]="addingLink"
                       [ngModelOptions]="{standalone: true}"
                       (docLinkAdded)="linkAdded($event)"
                       (docLinkAddCanceled)="addMode = false">
          </tb-doc-link>
        }
        @case ('quickLinks') {
          <tb-quick-link class="flex-1" [(ngModel)]="addingLink"
                         [ngModelOptions]="{standalone: true}"
                         (quickLinkAdded)="linkAdded($event)"
                         (quickLinkAddCanceled)="addMode = false">
          </tb-quick-link>
        }
      }
    }
  </div>
</form>
