<!--

    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.

-->
<ng-container [formGroup]="htmlContainerSettingsForm">
  <div class="tb-form-panel no-padding no-border relative h-full">
    <div class="flex flex-row items-center">
      <tb-toggle-select formControlName="type">
        <tb-toggle-option [value]="HtmlContainerWidgetType.PLAIN">{{ 'widgets.html-container.type-plain' | translate }}</tb-toggle-option>
        <tb-toggle-option [value]="HtmlContainerWidgetType.ANGULAR">{{ 'widgets.html-container.type-angular' | translate }}</tb-toggle-option>
      </tb-toggle-select>
    </div>
    <div class="tb-html-container-settings-panel flex flex-1 flex-col" tb-fullscreen [fullscreen]="fullscreen">
      <div class="tb-action-expand-button flex flex-row items-center justify-end">
        <button mat-stroked-button
                matTooltip="{{ 'widget.toggle-fullscreen' | translate }}"
                matTooltipPosition="above"
                (click)="toggleFullScreen()">
          <mat-icon>{{ fullscreen ? 'fullscreen_exit' : 'fullscreen' }}</mat-icon>
          <span>{{ (fullscreen ? 'fullscreen.exit' : 'fullscreen.fullscreen') | translate }}</span>
        </button>
      </div>
      <div class="flex flex-1">
        <mat-tab-group #leftPanel
                       [mat-stretch-tabs]="fullscreen"
                       [selectedIndex]="fullscreen ? 2 : 3"
                       [animationDuration]="tabsAnimationDuration"
                       [disablePagination]="fullscreen"
                       class="tb-content"
                       [class.flex-1]="!fullscreen">
          <mat-tab #resourceTab="matTab">
            <ng-template mat-tab-label>
              <div [matBadge]="resourcesFormArray.length" [matBadgeHidden]="resourceTab.isActive || !resourcesFormArray.length"
                   matBadgeSize="small">{{ 'widgets.html-container.resources' | translate }}</div>
            </ng-template>
            <div class="flex flex-col gap-2 pt-4" [class.px-2]="fullscreen">
              @if (resourcesFormArray.length) {
                @for (resourceControl of resourcesControls; track resourceControl; let i = $index) {
                  <div class="tb-form-row no-border no-padding" [formGroup]="resourceControl">
                    <tb-resource-autocomplete class="flex-1"
                                              formControlName="url"
                                              inlineField
                                              hideRequiredMarker required
                                              [allowAutocomplete]="resourceControl.get('isModule').value && htmlContainerSettingsForm.get('type').value === HtmlContainerWidgetType.ANGULAR"
                                              placeholder="{{ 'widget.resource-url' | translate }}">
                    </tb-resource-autocomplete>
                    @if (htmlContainerSettingsForm.get('type').value === HtmlContainerWidgetType.ANGULAR) {
                      <mat-checkbox formControlName="isModule">
                        {{ 'widget.resource-is-extension' | translate }}
                      </mat-checkbox>
                    }
                    <button mat-icon-button color="primary"
                            (click)="removeResource(i)"
                            matTooltip="{{'widget.remove-resource' | translate}}"
                            matTooltipPosition="above">
                      <mat-icon>delete</mat-icon>
                    </button>
                  </div>
                }
              } @else {
                <span translate
                      class="tb-prompt flex items-center justify-center">widgets.html-container.no-resources</span>
              }
              <div>
                <button mat-raised-button color="primary"
                        (click)="addResource()"
                        matTooltip="{{'widget.add-resource' | translate}}"
                        matTooltipPosition="above">
                  <span translate>action.add</span>
                </button>
              </div>
            </div>
          </mat-tab>
          <mat-tab label="{{ 'widgets.html-container.css' | translate }}">
            <tb-css class="flex-1"
                    [fillHeight]="true"
                    formControlName="css"
                    label="{{ 'widgets.html-container.css' | translate }}">
            </tb-css>
          </mat-tab>
          <mat-tab label="{{ (htmlContainerSettingsForm.get('type').value === HtmlContainerWidgetType.ANGULAR ? 'widgets.html-container.angular-html-template' : 'widgets.html-container.html') | translate }}">
            <tb-html class="flex-1"
                     [fillHeight]="true"
                     formControlName="html"
                     label="{{ (htmlContainerSettingsForm.get('type').value === HtmlContainerWidgetType.ANGULAR ? 'widgets.html-container.angular-html-template' : 'widgets.html-container.html') | translate }}">
            </tb-html>
          </mat-tab>
          @if (!fullscreen) {
            <mat-tab label="{{ 'widgets.html-container.java-script' | translate }}">
              <ng-container *ngTemplateOutlet="javascript"></ng-container>
            </mat-tab>
          }
        </mat-tab-group>
        <div #rightPanel class="tb-content flex" [class.!hidden]="!fullscreen">
          @if (fullscreen) {
            <ng-container *ngTemplateOutlet="javascript"></ng-container>
          }
        </div>
    </div>
    </div>
  </div>
</ng-container>
<ng-template #javascript>
  <ng-container [formGroup]="htmlContainerSettingsForm">
    <tb-js-func class="flex-1"
                [fillHeight]="true"
                formControlName="js"
                [globalVariables]="functionScopeVariables"
                [editorCompleter]="containerFunctionEditorCompleter"
                [functionArgs]="htmlContainerSettingsForm.get('type').value === HtmlContainerWidgetType.ANGULAR ? ['ctx'] : ['ctx', 'container']"
                withModules
                functionTitle="{{ 'widgets.html-container.js-function' | translate }}">
    </tb-js-func>
  </ng-container>
</ng-template>
