<!--

    Copyright © 2016-2023 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"
     [ngClass]="{
                'tb-highlighted': isHighlighted(widget),
                'tb-not-highlighted': isNotHighlighted(widget),
                'mat-elevation-z4': widget.dropShadow,
                'tb-has-timewindow': widget.hasTimewindow,
                'tb-edit': isEdit
             }"
     [ngStyle]="widget.style"
     (mousedown)="onMouseDown($event)"
     (click)="onClicked($event)"
     (contextmenu)="onContextMenu($event)">
  <div *ngIf="!!widgetComponent.widgetContext?.inited"
       class="tb-widget-header">
    <ng-container *ngIf="!widgetComponent.widgetContext?.embedTitlePanel">
      <ng-container *ngTemplateOutlet="widgetTitlePanel"></ng-container>
    </ng-container>
    <div *ngIf="widget.showWidgetActions"
         class="tb-widget-actions"
         [class]="{'tb-widget-actions-absolute': !(widget.showWidgetTitlePanel && !widgetComponent.widgetContext?.embedTitlePanel && (widget.showTitle||widget.hasAggregation))}"
         (mousedown)="$event.stopPropagation()">
      <button mat-icon-button *ngFor="let action of widget.customHeaderActions"
              [fxShow]="!isEdit"
              (click)="action.onAction($event)"
              matTooltip="{{ action.displayName }}"
              matTooltipPosition="above">
        <tb-icon>{{ action.icon }}</tb-icon>
      </button>
      <button mat-icon-button *ngFor="let action of widget.widgetActions"
              [fxShow]="!isEdit && action.show"
              (click)="action.onAction($event)"
              matTooltip="{{ action.name | translate }}"
              matTooltipPosition="above">
        <tb-icon>{{ action.icon }}</tb-icon>
      </button>
      <button mat-icon-button
              [fxShow]="!isEdit && widget.enableFullscreen"
              (click)="$event.stopPropagation(); widget.isFullscreen = !widget.isFullscreen"
              matTooltip="{{(widget.isFullscreen ? 'fullscreen.exit' : 'fullscreen.expand') | translate}}"
              matTooltipPosition="above">
        <tb-icon>{{ widget.isFullscreen ? 'fullscreen_exit' : 'fullscreen' }}</tb-icon>
      </button>
      <button mat-icon-button
              [fxShow]="isEditActionEnabled && !widget.isFullscreen"
              (click)="onEdit($event)"
              matTooltip="{{ 'widget.edit' | translate }}"
              matTooltipPosition="above">
        <tb-icon>edit</tb-icon>
      </button>
      <button mat-icon-button
              [fxShow]="isExportActionEnabled && !widget.isFullscreen"
              (click)="onExport($event)"
              matTooltip="{{ 'widget.export' | translate }}"
              matTooltipPosition="above">
        <tb-icon>file_download</tb-icon>
      </button>
      <button mat-icon-button
              [fxShow]="isRemoveActionEnabled && !widget.isFullscreen"
              (click)="onRemove($event);"
              matTooltip="{{ 'widget.remove' | translate }}"
              matTooltipPosition="above">
        <tb-icon>close</tb-icon>
      </button>
    </div>
  </div>
  <div class="tb-widget-content" [ngClass]="{'tb-no-interaction': disableWidgetInteraction}">
    <tb-widget #widgetComponent
               [dashboardWidget]="widget"
               [isEdit]="isEdit"
               [isMobile]="isMobile"
               [widgetTitlePanel]="widgetTitlePanel">
    </tb-widget>
  </div>
</div>
<ng-template #widgetTitlePanel>
  <div *ngIf="widget.showWidgetTitlePanel"
       class="tb-widget-title">
              <div *ngIf="widget.showTitle"
                    [matTooltip]="widget.titleTooltip"
                    matTooltipClass="tb-tooltip-multiline"
                    matTooltipPosition="above"
                    class="title-row">
                <tb-icon *ngIf="widget.showTitleIcon" [style]="widget.titleIconStyle">{{widget.titleIcon}}</tb-icon>
                <div class="mat-subtitle-1 title" [style]="widget.titleStyle">
                  {{widget.title$ | async}}
                </div>
              </div>
    <tb-timewindow *ngIf="widget.hasTimewindow"
                   aggregation="{{widget.hasAggregation}}"
                   quickIntervalOnly="{{widget.onlyQuickInterval}}"
                   historyOnly="{{widget.onlyHistoryTimewindow}}"
                   alwaysDisplayTypePrefix
                   timezone="true"
                   noMargin
                   noPadding
                   [isEdit]="isEdit"
                   [timewindowStyle]="widgetComponent.widget.config.timewindowStyle"
                   [(ngModel)]="widgetComponent.widget.config.timewindow"
                   (ngModelChange)="widgetComponent.onTimewindowChanged($event)">
    </tb-timewindow>
  </div>
</ng-template>
