<!--

    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 class="tb-scada-symbol-editor mat-content flex flex-col">
  <section class="tb-scada-symbol-editor-container flex flex-1 flex-col">
    <div class="tb-scada-symbol-editor-layout flex flex-1 flex-row">
      <mat-drawer-container class="size-full">
        <mat-drawer class="tb-scada-symbol-editor-details-drawer mat-elevation-z4"
                    disableClose="true"
                    mode="side"
                    opened
                    position="end">
          <tb-details-panel class="flex-1"
                            [isReadOnly]="true"
                            [showCloseDetails]="false"
                            headerHeightPx="64"
                            headerTitle="{{symbolData?.imageResource?.title}}">
            <div class="details-buttons flex flex-row items-center justify-start gap-4">
              <div tb-help="scadaSymbolDev"></div>
              <button mat-stroked-button
                      [disabled]="scadaSymbolFormGroup.invalid"
                      (click)="createWidget()">
                {{ 'scada.create-widget' | translate }}
              </button>
            </div>
            @if (previewMode) {
              <div class="tb-scada-symbol-editor-preview-content tb-absolute-fill">
                <div class="tb-scada-symbol-editor-preview-header">
                  <button mat-button
                          (click)="exitPreviewMode()">
                    <mat-icon>chevron_left</mat-icon>
                    {{ 'action.back' | translate }}
                  </button>
                  <span style="flex: 1;"></span>
                  <button [disabled]="!scadaPreviewFormGroup.dirty"
                          mat-stroked-button color="primary"
                          (click)="onRevertPreviewSettings()">
                    <mat-icon>close</mat-icon>
                    {{ 'action.decline' | translate }}
                  </button>
                  <button [disabled]="!scadaPreviewFormGroup.valid || !scadaPreviewFormGroup.dirty"
                          mat-flat-button color="primary"
                          (click)="onApplyPreviewSettings()">
                    <mat-icon>done</mat-icon>
                    {{ 'action.apply' | translate }}
                  </button>
                </div>
                <div [formGroup]="scadaPreviewFormGroup" class="tb-scada-symbol-editor-preview-settings">
                  <div class="mat-content">
                    <div class="tb-form-panel">
                      <div class="tb-form-panel-title" translate>scada.symbol.symbol</div>
                      <tb-scada-symbol-input
                        [scadaSymbolContent]="symbolData.scadaSymbolContent"
                        [scadaSymbolMetadata]="previewMetadata">
                      </tb-scada-symbol-input>
                    </div>
                    <tb-scada-symbol-object-settings
                      formControlName="scadaSymbolObjectSettings"
                      [scadaSymbolMetadata]="previewMetadata"
                      [aliasController]="aliasController"
                      [widgetType]="widgetType.rpc"
                      [callbacks]="widgetActionCallbacks">
                    </tb-scada-symbol-object-settings>
                  </div>
                </div>
              </div>
            }
            <div [class.!hidden]="previewMode" [formGroup]="scadaSymbolFormGroup">
              <tb-scada-symbol-metadata
                #symbolMetadata
                [tags]="tags"
                [aliasController]="aliasController"
                [callbacks]="widgetActionCallbacks"
                formControlName="metadata">
                <div class="tb-scada-symbol-metadata-header-prefix">
                  <button class="gt-sm:!hidden"
                          [disabled]="scadaSymbolFormGroup.invalid || !symbolEditorValid"
                          mat-button color="primary"
                          (click)="enterPreviewMode()">
                    <mat-icon>visibility</mat-icon>
                    {{ 'scada.preview' | translate }}
                  </button>
                </div>
                <div class="tb-scada-symbol-metadata-header-suffix flex flex-row items-center justify-end gap-3 lt-md:flex-1">
                  <button class="lt-md:!hidden"
                          [disabled]="scadaSymbolFormGroup.invalid || !symbolEditorValid"
                          mat-button color="primary"
                          (click)="enterPreviewMode()">
                    <mat-icon>visibility</mat-icon>
                    {{ 'scada.preview' | translate }}
                  </button>
                  @if (!readonly) {
                    <div class="flex flex-row items-center justify-start gap-3">
                      <button [disabled]="!(scadaSymbolFormGroup.dirty || symbolEditorDirty)"
                              mat-stroked-button color="primary"
                              (click)="onRevertScadaSymbolConfig()">
                        <mat-icon>close</mat-icon>
                        {{ 'action.decline' | translate }}
                      </button>
                      <button [disabled]="!scadaSymbolFormGroup.valid || !symbolEditorValid || !(scadaSymbolFormGroup.dirty || symbolEditorDirty)"
                              mat-flat-button color="primary"
                              (click)="onApplyScadaSymbolConfig()">
                        <mat-icon>done</mat-icon>
                        {{ 'action.apply' | translate }}
                      </button>
                    </div>
                  }
                </div>
              </tb-scada-symbol-metadata>
            </div>
          </tb-details-panel>
        </mat-drawer>
        <mat-drawer-content class="tb-scada-symbol-editor-content" [class.preview]="previewMode">
          @if (!previewMode) {
            <tb-scada-symbol-editor #symbolEditor
                                    [readonly]="readonly"
                                    [(showHiddenElements)]="showHiddenElements"
                                    [data]="symbolEditorData"
                                    [editObjectCallbacks]="editObjectCallbacks"
                                    (updateScadaSymbol)="updateScadaSymbol()"
            (downloadScadaSymbol)="downloadScadaSymbol()"></tb-scada-symbol-editor>
          }
          @if (previewMode) {
            <tb-dashboard
              class="tb-absolute-fill"
              [aliasController]="aliasController"
              [widgets]="previewWidgets"
              [autofillHeight]="false"
              displayGrid="always"
              [gridType]="previewWidget.sizeX >= previewWidget.sizeY ? GridType.ScrollVertical : GridType.ScrollHorizontal"
              [columns]="previewWidget.sizeX"
              setGridSize
              [centerVertical]="previewWidget.sizeX >= previewWidget.sizeY"
              [centerHorizontal]="previewWidget.sizeX < previewWidget.sizeY"
              [margin]="0"
              [isEdit]="false"
              [isMobileDisabled]="true"
              [isEditActionEnabled]="false"
              [isRemoveActionEnabled]="false">
            </tb-dashboard>
          }
        </mat-drawer-content>
      </mat-drawer-container>
    </div>
  </section>
</div>
