<!--

    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]="timewindowForm" mat-dialog-content  class="tb-timewindow-form">
  <mat-toolbar color="primary">
    <h2>{{ 'timewindow.timewindow-settings' | translate }}</h2>
    <span class="flex-1"></span>
    <div tb-help="timewindowSettings"></div>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div class="tb-timewindow-form-header tb-form-panel no-border no-padding-bottom">
    <tb-toggle-select class="tb-timewindow-form-type-options" appearance="fill"
                      [options]="timewindowTypeOptions" formControlName="selectedTab">
    </tb-toggle-select>
  </div>
  <div class="tb-timewindow-form-content tb-form-panel no-border">
    @if (timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME) {
      <section
        formGroupName="realtime" class="tb-form-panel">
        <div class="tb-form-panel-title">{{ 'timewindow.timewindow' | translate }}</div>
        <div class="tb-form-row">
          <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="hideInterval">
            {{ 'timewindow.hide-timewindow-section' | translate }}
          </mat-slide-toggle>
        </div>
        @if (realtimeTypeSelectionAvailable) {
          <tb-toggle-select
            appearance="stroked" [options]="realtimeTimewindowOptions" formControlName="realtimeType">
          </tb-toggle-select>
        }
        @if (timewindowForm.get('realtime.realtimeType').value === realtimeTypes.LAST_INTERVAL) {
          <div class="tb-form-row">
            <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="disableCustomInterval">
              {{ 'timewindow.disable-custom-interval' | translate }}
            </mat-slide-toggle>
          </div>
          <div class="tb-form-row column-xs">
            @if (!quickIntervalOnly) {
              <mat-slide-toggle
                class="mat-slide" formControlName="hideLastInterval">
                <div tb-hint-tooltip-icon="{{'timewindow.hide-last-interval' | translate}}">
                  {{ 'timewindow.hide' | translate }}
                </div>
              </mat-slide-toggle>
            }
            <tb-timeinterval
              formControlName="timewindowMs"
              subscriptSizing="dynamic"
              appearance="outline"
              [allowedIntervals]="timewindowForm.get('realtime.advancedParams.allowedLastIntervals').value"
              [disabledAdvanced]="timewindowForm.get('realtime.disableCustomInterval').value"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME &&
                            timewindowForm.get('realtime.realtimeType').value === realtimeTypes.LAST_INTERVAL">
              @if (!(timewindowForm.get('realtime.hideLastInterval').value ||
                timewindowForm.get('realtime.hideInterval').value)) {
                <button
                  matSuffix mat-icon-button type="button"
                  (click)="configureRealtimeLastIntervalOptions($event)">
                  <mat-icon>edit</mat-icon>
                </button>
              }
            </tb-timeinterval>
          </div>
        }
        @if (timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL) {
          <div
            class="tb-form-row column-xs">
            @if (!quickIntervalOnly) {
              <mat-slide-toggle
                class="mat-slide" formControlName="hideQuickInterval">
                <div tb-hint-tooltip-icon="{{'timewindow.hide-relative-interval' | translate}}">
                  {{ 'timewindow.hide' | translate }}
                </div>
              </mat-slide-toggle>
            }
            <tb-quick-time-interval
              displayLabel="false"
              formControlName="quickInterval"
              onlyCurrentInterval="true"
              subscriptSizing="dynamic"
              appearance="outline"
              [allowedIntervals]="timewindowForm.get('realtime.advancedParams.allowedQuickIntervals').value"
          [required]="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME &&
                          timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL">
              @if (!(timewindowForm.get('realtime.hideQuickInterval').value ||
                timewindowForm.get('realtime.hideInterval').value)) {
                <button
                  matSuffix mat-icon-button type="button"
                  (click)="configureRealtimeQuickIntervalOptions($event)">
                  <mat-icon>edit</mat-icon>
                </button>
              }
            </tb-quick-time-interval>
          </div>
        }
      </section>
    }

    @if (timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY) {
      <section
        formGroupName="history" class="tb-form-panel">
        <div class="tb-form-panel-title">{{ 'timewindow.timewindow' | translate }}</div>
        <div class="tb-form-row">
          <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="hideInterval">
            {{ 'timewindow.hide-timewindow-section' | translate }}
          </mat-slide-toggle>
        </div>
        <tb-toggle-select appearance="stroked" [options]="historyTimewindowOptions" formControlName="historyType">
        </tb-toggle-select>
        @if (timewindowForm.get('history.historyType').value === historyTypes.LAST_INTERVAL) {
          <div class="tb-form-row">
            <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="disableCustomInterval">
              {{ 'timewindow.disable-custom-interval' | translate }}
            </mat-slide-toggle>
          </div>
          <div class="tb-form-row column-xs">
            <mat-slide-toggle class="mat-slide" formControlName="hideLastInterval">
              <div tb-hint-tooltip-icon="{{'timewindow.hide-last-interval' | translate}}">
                {{ 'timewindow.hide' | translate }}
              </div>
            </mat-slide-toggle>
            <tb-timeinterval
              formControlName="timewindowMs"
              subscriptSizing="dynamic"
              appearance="outline"
              [allowedIntervals]="timewindowForm.get('history.advancedParams.allowedLastIntervals').value"
              [disabledAdvanced]="timewindowForm.get('history.disableCustomInterval').value"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY &&
                          timewindowForm.get('history.historyType').value === historyTypes.LAST_INTERVAL">
              @if (!(timewindowForm.get('history.hideLastInterval').value ||
                timewindowForm.get('history.hideInterval').value)) {
                <button
                  matSuffix mat-icon-button type="button"
                  (click)="configureHistoryLastIntervalOptions($event)">
                  <mat-icon>edit</mat-icon>
                </button>
              }
            </tb-timeinterval>
          </div>
        }
        @if (timewindowForm.get('history.historyType').value === historyTypes.FIXED) {
          <div class="tb-form-row column-xs">
            <mat-slide-toggle class="mat-slide" formControlName="hideFixedInterval">
              <div tb-hint-tooltip-icon="{{'timewindow.hide-fixed-interval' | translate}}">
                {{ 'timewindow.hide' | translate }}
              </div>
            </mat-slide-toggle>
            <tb-datetime-period
              formControlName="fixedTimewindow"
              subscriptSizing="dynamic"
              appearance="outline"
              class="history-time-input"
          [required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY &&
                        timewindowForm.get('history.historyType').value === historyTypes.FIXED">
            </tb-datetime-period>
          </div>
        }
        @if (timewindowForm.get('history.historyType').value === historyTypes.INTERVAL) {
          <div class="tb-form-row column-xs">
            <mat-slide-toggle class="mat-slide" formControlName="hideQuickInterval">
              <div tb-hint-tooltip-icon="{{'timewindow.hide-relative-interval' | translate}}">
                {{ 'timewindow.hide' | translate }}
              </div>
            </mat-slide-toggle>
            <tb-quick-time-interval
              displayLabel="false"
              formControlName="quickInterval"
              subscriptSizing="dynamic"
              appearance="outline"
              [allowedIntervals]="timewindowForm.get('history.advancedParams.allowedQuickIntervals').value"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY &&
                        timewindowForm.get('history.historyType').value === historyTypes.INTERVAL">
              @if (!(timewindowForm.get('history.hideQuickInterval').value ||
                timewindowForm.get('history.hideInterval').value)) {
                <button
                  matSuffix mat-icon-button type="button"
                  (click)="configureHistoryQuickIntervalOptions($event)">
                  <mat-icon>edit</mat-icon>
                </button>
              }
            </tb-quick-time-interval>
          </div>
        }
      </section>
    }

    @if (aggregation) {
      <section class="tb-form-panel">
        <div class="tb-form-panel-title">{{ 'aggregation.aggregation' | translate }}</div>
        <div class="tb-form-row column-xs">
          <mat-slide-toggle class="mat-slide" formControlName="hideAggregation">
            <div tb-hint-tooltip-icon="{{'timewindow.hide-aggregation' | translate}}">
              {{ 'timewindow.hide' | translate }}
            </div>
          </mat-slide-toggle>
          <ng-container formGroupName="aggregation">
            <tb-aggregation-type-select class="flex" subscriptSizing="dynamic" appearance="outline" displayLabel="false"
                                        formControlName="type"
                                        [allowedAggregationTypes]="timewindowForm.get('allowedAggTypes').value">
              @if (!timewindowForm.get('hideAggregation').value) {
                <button
                  matSuffix mat-icon-button type="button"
                  (click)="openAggregationOptionsConfig($event)">
                  <mat-icon>edit</mat-icon>
                </button>
              }
            </tb-aggregation-type-select>
          </ng-container>
        </div>
      </section>
      @if (timewindowForm.get('aggregation.type').value === aggregationTypes.NONE) {
        <section class="tb-form-panel"
                 >
          <div class="tb-form-panel-title">{{ 'aggregation.limit' | translate }}</div>
          <div class="tb-form-row column-xs">
            <mat-slide-toggle class="mat-slide" formControlName="hideAggInterval">
              <div tb-hint-tooltip-icon="{{'timewindow.hide-max-values' | translate}}">
                {{ 'timewindow.hide' | translate }}
              </div>
            </mat-slide-toggle>
            <ng-container formGroupName="aggregation">
              <tb-datapoints-limit formControlName="limit"
                                   [required]="timewindowForm.get('aggregation.type').value === aggregationTypes.NONE">
              </tb-datapoints-limit>
            </ng-container>
          </div>
        </section>
      }
      <section class="tb-form-panel" [class.!hidden]="timewindowForm.get('aggregation.type').value === aggregationTypes.NONE">
        <div class="tb-form-panel-title">{{ 'aggregation.group-interval' | translate }}</div>
        @if (timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME) {
          <div class="tb-form-row" formGroupName="realtime">
            <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="disableCustomGroupInterval">
              {{ 'timewindow.disable-custom-interval' | translate }}
            </mat-slide-toggle>
          </div>
          <div class="tb-form-row column-xs">
            <ng-container
              *ngTemplateOutlet="timewindowForm.get('aggregation.type').value !== aggregationTypes.NONE ? hideAggInterval : null">
            </ng-container>
            <ng-container formGroupName="realtime">
              <tb-timeinterval
                formControlName="interval"
                [min]="minRealtimeAggInterval" [max]="maxRealtimeAggInterval"
                [allowedIntervals]="realtimeAllowedAggIntervals"
                useCalendarIntervals
                subscriptSizing="dynamic"
                appearance="outline"
                [disabledAdvanced]="timewindowForm.get('realtime.disableCustomGroupInterval').value">
              </tb-timeinterval>
            </ng-container>
          </div>
        }
        @if (timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY) {
          <div class="tb-form-row" formGroupName="history">
            <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="disableCustomGroupInterval">
              {{ 'timewindow.disable-custom-interval' | translate }}
            </mat-slide-toggle>
          </div>
          <div class="tb-form-row column-xs">
            <ng-container
              *ngTemplateOutlet="timewindowForm.get('aggregation.type').value !== aggregationTypes.NONE ? hideAggInterval : null">
            </ng-container>
            <ng-container formGroupName="history">
              <tb-timeinterval
                formControlName="interval"
                [min]="minHistoryAggInterval" [max]="maxHistoryAggInterval"
                [allowedIntervals]="historyAllowedAggIntervals"
                useCalendarIntervals
                subscriptSizing="dynamic"
                appearance="outline"
                [disabledAdvanced]="timewindowForm.get('history.disableCustomGroupInterval').value">
              </tb-timeinterval>
            </ng-container>
          </div>
        }
        <ng-template #hideAggInterval>
          <mat-slide-toggle class="mat-slide" formControlName="hideAggInterval">
            <div tb-hint-tooltip-icon="{{'timewindow.hide-group-interval' | translate}}">
              {{ 'timewindow.hide' | translate }}
            </div>
          </mat-slide-toggle>
        </ng-template>
      </section>
    }

    <section class="tb-form-panel">
      <div class="tb-form-panel-title">{{ 'timezone.timezone' | translate }}</div>
      <div class="tb-form-row column-xs">
        <mat-slide-toggle class="mat-slide" formControlName="hideTimezone">
          <div tb-hint-tooltip-icon="{{'timewindow.hide-timezone' | translate}}">
            {{ 'timewindow.hide' | translate }}
          </div>
        </mat-slide-toggle>
        <tb-timezone-select [localBrowserTimezonePlaceholderOnEmpty]="true"
                            formControlName="timezone"
                            subscriptSizing="dynamic"
                            appearance="outline"
                            displayLabel="false">
        </tb-timezone-select>
      </div>
    </section>
    @if (showSaveAsDefault) {
      <section class="tb-form-panel">
        <div class="tb-form-panel-title" translate>timewindow.save-current-settings-as-default</div>
        <div class="tb-form-row">
          <mat-slide-toggle class="mat-slide" formControlName="hideSaveAsDefault">
            {{ 'timewindow.hide-option-from-end-users' | translate }}
          </mat-slide-toggle>
        </div>
      </section>
    }
  </div>
  <mat-divider></mat-divider>
  <div mat-dialog-actions class="tb-dialog-actions tb-flex flex-end no-gap">
    <button type="button"
            mat-button
            [disabled]="(isLoading$ | async)"
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button type="button"
            mat-raised-button
            color="primary"
            (click)="update()"
            [disabled]="(isLoading$ | async) || timewindowForm.invalid || !timewindowForm.dirty">
      {{ 'action.apply' | translate }}
    </button>
  </div>
</form>
