<!--

    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 [formGroup]="widgetFormGroup">
  <fieldset [disabled]="isLoading$ | async">
    <tb-widget-config
      #widgetConfigComponent
      [aliasController]="aliasController"
      [functionsOnly]="widgetEditMode"
      [dashboard]="dashboard"
      [widget]="widget"
      [hideToggleHeader]="previewMode"
      [showLayoutConfig]="showLayoutConfig"
      [isDefaultBreakpoint]="isDefaultBreakpoint"
      formControlName="widgetConfig">
      <div class="tb-widget-config-header-prefix">
        @if (previewMode) {
          <button mat-button
                  (click)="previewMode = false">
            <mat-icon>chevron_left</mat-icon>
            {{ 'action.back' | translate }}
          </button>
        }
        @if (!previewMode) {
          <button
            class="gt-sm:!hidden"
            [disabled]="!widgetFormGroup.valid"
            mat-button color="primary"
            (click)="previewMode = true">
            <mat-icon>visibility</mat-icon>
            {{ 'widget-config.preview' | translate }}
          </button>
        }
      </div>
      <div class="tb-widget-config-header-suffix flex flex-row items-center justify-end gap-3 lt-md:flex-1">
        @if (!previewMode) {
          <button
            class="lt-md:!hidden"
            [disabled]="!widgetFormGroup.valid"
            mat-button color="primary"
            (click)="previewMode = true">
            <mat-icon>visibility</mat-icon>
            {{ 'widget-config.preview' | translate }}
          </button>
        }
        <div class="flex flex-row items-center justify-start gap-3">
          <button [disabled]="!widgetFormGroup.dirty"
                  mat-stroked-button color="primary"
                  (click)="onRevertWidgetConfig()">
            <mat-icon>close</mat-icon>
            {{ 'action.decline' | translate }}
          </button>
          <button [disabled]="!widgetFormGroup.valid || !widgetFormGroup.dirty"
                  mat-flat-button color="primary"
                  (click)="onApplyWidgetConfig()">
            <mat-icon>done</mat-icon>
            {{ 'action.apply' | translate }}
          </button>
        </div>
      </div>
    </tb-widget-config>
    @if (previewMode) {
      <div class="widget-preview-background">
        <tb-widget-preview class="widget-preview-section"
                           [aliasController]="aliasController"
                           [stateController]="stateController"
                           [dashboardTimewindow]="dashboard.configuration.timewindow"
                           [widget]="widget"
                           [widgetConfig]="widgetFormGroup.get('widgetConfig').value.config"
                           [previewWidth]="widgetConfig.typeParameters.previewWidth"
                           [previewHeight]="widgetConfig.typeParameters.previewHeight">
        </tb-widget-preview>
      </div>
    }
  </fieldset>
</form>
