<!--

    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 #tbWidgetElement tb-fullscreen [fullscreen]="widget.isFullscreen"
     [fullscreenBackgroundStyle]="dashboardStyle"
     [fullscreenBackgroundImage]="backgroundImage"
     (fullscreenChanged)="onFullscreenChanged($event)"
     class="tb-widget"
     [class.tb-highlighted]="isHighlighted(widget)"
     [class.tb-not-highlighted]="isNotHighlighted(widget)"
     [class.mat-elevation-z4]="widget.dropShadow"
     [class.tb-overflow-visible]="widgetComponent.widgetContext?.overflowVisible"
     [class.tb-has-timewindow]="widget.hasTimewindow"
     [class.tb-edit]="isEdit || isEditingWidget"
     [class.tb-hover]="hovered"
     [style]="widget.style">
  @if (!!widgetComponent.widgetContext?.inited) {
    <div
      class="tb-widget-header">
      @if (!widgetComponent.widgetContext?.embedTitlePanel) {
        <ng-container *ngTemplateOutlet="widgetTitlePanel"></ng-container>
      }
      @if (!widgetComponent.widgetContext?.embedActionsPanel) {
        <ng-container *ngTemplateOutlet="widgetHeaderActionsPanel"></ng-container>
      }
    </div>
  }
  <div class="tb-widget-content" [class.tb-no-interaction]="disableWidgetInteraction">
    <tb-widget #widgetComponent
               [dashboardWidget]="widget"
               [widget]="widget.widget"
               [isEdit]="isEdit"
               [isPreview]="isPreview"
               [isMobile]="isMobile"
               [widgetTitlePanel]="widgetTitlePanel"
               [widgetHeaderActionsPanel]="widgetHeaderActionsPanel">
    </tb-widget>
  </div>
</div>
<ng-template #widgetTitlePanel let-titleSuffixTemplate="titleSuffixTemplate">
  @if (widget.showWidgetTitlePanel) {
    <div
      class="tb-widget-title">
      @if (widget.showTitle) {
        <div
          [matTooltip]="widget.titleTooltip"
          matTooltipClass="tb-tooltip-multiline"
          matTooltipPosition="above"
          class="title-row">
          @if (widget.showTitleIcon) {
            <tb-icon [style]="widget.titleIconStyle">{{widget.titleIcon}}</tb-icon>
          }
          <div class="mat-subtitle-1 title" [style]="widget.titleStyle">
            {{widget.title$ | async}}
          </div>
          <ng-container *ngTemplateOutlet="titleSuffixTemplate"></ng-container>
        </div>
      }
      @if (widget.hasTimewindow) {
        <tb-timewindow
          aggregation="{{widget.hasAggregation}}"
          quickIntervalOnly="{{widget.onlyQuickInterval}}"
          historyOnly="{{widget.onlyHistoryTimewindow}}"
          alwaysDisplayTypePrefix
          timezone="true"
          noMargin
          noPadding
          [isEdit]="isEdit"
          [timewindowStyle]="widget.widget.config.timewindowStyle"
          [(ngModel)]="widget.widget.config.timewindow"
          (ngModelChange)="widgetComponent.onTimewindowChanged($event)">
        </tb-timewindow>
      }
    </div>
  }
</ng-template>

<ng-template #widgetHeaderActionsPanel let-absolute="absolute">
  @if (widget.showWidgetActions) {
    <div
      class="tb-widget-actions"
      [class.tb-widget-actions-absolute]="widgetActionAbsolute(widgetComponent, absolute)"
      (mousedown)="$event.stopPropagation()">
      <div class="flex items-center">
        @for (action of widget.customHeaderActions; track action.name; let last = $last) {
          <ng-container *ngTemplateOutlet="widgetHeaderActionButton; context:{ action, last }"></ng-container>
        }
      </div>
      @for (action of widget.widgetActions; track action) {
        <button mat-icon-button
                type="button"
                [class.!hidden]="isEdit || !action.show"
                (click)="action.onAction($event)"
                matTooltip="{{ action.name | translate }}"
                matTooltipPosition="above">
          <tb-icon>{{ action.icon }}</tb-icon>
        </button>
      }
      <button mat-icon-button
              type="button"
              [class.!hidden]="isEdit || !widget.enableFullscreen"
              (click)="$event.stopPropagation(); widget.isFullscreen = !widget.isFullscreen; updateEditWidgetActionsTooltipState()"
              matTooltip="{{(widget.isFullscreen ? 'fullscreen.exit' : 'fullscreen.expand') | translate}}"
              matTooltipPosition="above">
        <tb-icon>{{ widget.isFullscreen ? 'fullscreen_exit' : 'fullscreen' }}</tb-icon>
      </button>
    </div>
  }
</ng-template>

<ng-template #widgetHeaderActionButton let-action="action" let-last="last">
  @switch (action.buttonType) {
    @case (widgetHeaderActionButtonType.miniFab) {
      <button mat-mini-fab
              [style]="action.customButtonStyle"
              [class.!hidden]="isEdit"
              [class.mr-2]="!last"
              (click)="action.onAction($event)"
              matTooltip="{{ action.displayName }}"
              matTooltipPosition="above">
        <tb-icon>{{ action.icon }}</tb-icon>
      </button>
    }
    @case (widgetHeaderActionButtonType.basic) {
      <button [class.!hidden]="isEdit" mat-button
              [class.mr-2]="!last"
              [style]="action.customButtonStyle"
              (click)="action.onAction($event)"
              matTooltip="{{ action.displayName }}"
              matTooltipPosition="above">
        @if (action.showIcon) {
          <tb-icon matButtonIcon>{{ action.icon }}</tb-icon>
        }
        <span>{{ action.displayName }}</span>
      </button>
    }
    @case (widgetHeaderActionButtonType.raised) {
      <button [class.!hidden]="isEdit" mat-raised-button
              [class.mr-2]="!last"
              [style]="action.customButtonStyle"
              (click)="action.onAction($event)"
              matTooltip="{{ action.displayName }}"
              matTooltipPosition="above">
        @if (action.showIcon) {
          <tb-icon matButtonIcon>{{ action.icon }}</tb-icon>
        }
        <span>{{ action.displayName }}</span>
      </button>
    }
    @case (widgetHeaderActionButtonType.stroked) {
      <button [class.!hidden]="isEdit" mat-stroked-button
              [class.mr-2]="!last"
              [style]="action.customButtonStyle"
              (click)="action.onAction($event)"
              matTooltip="{{ action.displayName }}"
              matTooltipPosition="above">
        @if (action.showIcon) {
          <tb-icon matButtonIcon>{{ action.icon }}</tb-icon>
        }
        <span>{{ action.displayName }}</span>
      </button>
    }
    @case (widgetHeaderActionButtonType.flat) {
      <button [class.!hidden]="isEdit" mat-flat-button
              [class.mr-2]="!last"
              [style]="action.customButtonStyle"
              (click)="action.onAction($event)"
              matTooltip="{{ action.displayName }}"
              matTooltipPosition="above">
        @if (action.showIcon) {
          <tb-icon matButtonIcon>{{ action.icon }}</tb-icon>
        }
        <span>{{ action.displayName }}</span>
      </button>
    }
    @default {
      <button mat-icon-button
              [style]="action.customButtonStyle"
              [class.!hidden]="isEdit"
              (click)="action.onAction($event)"
              matTooltip="{{ action.displayName }}"
              matTooltipPosition="above">
        <tb-icon>{{ action.icon }}</tb-icon>
      </button>
    }
  }
</ng-template>
