<!--

    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" class="tb-timewindow-form">
  @if (!historyOnly) {
    <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>
      @if (isEdit) {
        <button mat-icon-button type="button" class="tb-timewindow-form-settings-btn tb-mat-24"
                (click)="openTimewindowConfig()">
          <mat-icon>settings</mat-icon>
        </button>
      }
    </div>
  }

  <div class="tb-timewindow-form-content tb-form-panel no-border" [class.no-padding]="!panelMode">
    @if (timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME) {
      @if (realtimeIntervalSelectionAvailable) {
        <section class="tb-form-panel stroked">
          <div class="tb-flex space-between"
               [class.align-end]="realtimeTypeSelectionAvailable"
               [class.align-center]="!realtimeTypeSelectionAvailable">
            <div class="tb-flex-xs column">
              <div class="tb-form-panel-title">{{ 'timewindow.timewindow' | translate }}</div>
              @if (realtimeTypeSelectionAvailable) {
                <ng-container formGroupName="realtime">
                  <tb-toggle-select appearance="stroked" [options]="realtimeTimewindowOptions" formControlName="realtimeType">
                  </tb-toggle-select>
                </ng-container>
              }
            </div>
            <ng-container *ngTemplateOutlet="timezoneSelection">
            </ng-container>
          </div>
          <div class="tb-form-row no-border no-padding" formGroupName="realtime">
            @if (timewindowForm.get('realtime.realtimeType').value === realtimeTypes.LAST_INTERVAL) {
              <tb-timeinterval
                formControlName="timewindowMs"
                subscriptSizing="dynamic"
                appearance="outline"
                [allowedIntervals]="realtimeAllowedLastIntervals"
                [disabledAdvanced]="realtimeDisableCustomInterval"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME &&
                              timewindowForm.get('realtime.realtimeType').value === realtimeTypes.LAST_INTERVAL">
              </tb-timeinterval>
            }
            @if (timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL) {
              <tb-quick-time-interval
                displayLabel="false"
                formControlName="quickInterval"
                onlyCurrentInterval="true"
                subscriptSizing="dynamic"
                appearance="outline"
                [allowedIntervals]="realtimeAllowedQuickIntervals"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME &&
                              timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL">
              </tb-quick-time-interval>
            }
          </div>
        </section>
      } @else {
        <section class="tb-form-row space-between same-padding">
          <div>{{ 'timezone.timezone' | translate }}</div>
          <ng-container *ngTemplateOutlet="timezoneSelection">
          </ng-container>
        </section>
      }
    }

    @if (timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY) {
      @if (historyIntervalSelectionAvailable) {
        <section class="tb-form-panel stroked">
          <div class="tb-flex space-between"
               [class.align-end]="historyTypeSelectionAvailable"
               [class.align-center]="!historyTypeSelectionAvailable">
            <div class="tb-flex-xs column">
              <div class="tb-form-panel-title">{{ 'timewindow.timewindow' | translate }}</div>
              @if (historyTypeSelectionAvailable) {
                <ng-container formGroupName="history">
                  <tb-toggle-select appearance="stroked" [options]="historyTimewindowOptions" formControlName="historyType">
                  </tb-toggle-select>
                </ng-container>
              }
            </div>
            <ng-container *ngTemplateOutlet="timezoneSelection">
            </ng-container>
          </div>
          @if (historyIntervalSelectionAvailable &&
            timewindowForm.get('history.historyType').value !== historyTypes.FOR_ALL_TIME) {
            <div class="tb-form-row no-border no-padding"
                 formGroupName="history">
              @if (timewindowForm.get('history.historyType').value === historyTypes.LAST_INTERVAL) {
                <tb-timeinterval
                  formControlName="timewindowMs"
                  subscriptSizing="dynamic"
                  appearance="outline"
                  [allowedIntervals]="historyAllowedLastIntervals"
                  [disabledAdvanced]="historyDisableCustomInterval"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY &&
                            timewindowForm.get('history.historyType').value === historyTypes.LAST_INTERVAL">
                </tb-timeinterval>
              }
              @if (timewindowForm.get('history.historyType').value === historyTypes.FIXED) {
                <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>
              }
              @if (timewindowForm.get('history.historyType').value === historyTypes.INTERVAL) {
                <tb-quick-time-interval
                  displayLabel="false"
                  formControlName="quickInterval"
                  subscriptSizing="dynamic"
                  appearance="outline"
                  [allowedIntervals]="historyAllowedQuickIntervals"
            [required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY &&
                          timewindowForm.get('history.historyType').value === historyTypes.INTERVAL">
                </tb-quick-time-interval>
              }
            </div>
          }
        </section>
      } @else {
        <section class="tb-form-row space-between same-padding">
          <div>{{ 'timezone.timezone' | translate }}</div>
          <ng-container *ngTemplateOutlet="timezoneSelection">
          </ng-container>
        </section>
      }
    }

    @if (aggregationOptionsAvailable) {
      <ng-container formGroupName="aggregation">
        @if (isEdit || !timewindow.hideAggregation) {
          <section class="tb-form-row column-xs space-between same-padding">
            <div class="fixed-title-width-180">{{ 'aggregation.aggregation' | translate }}</div>
            <tb-aggregation-type-select class="flex" subscriptSizing="dynamic" appearance="outline" displayLabel="false"
                                        formControlName="type" [allowedAggregationTypes]="allowedAggTypes">
            </tb-aggregation-type-select>
          </section>
        }
        @if (timewindowForm.get('aggregation.type').value === aggregationTypes.NONE && (isEdit || !timewindow.hideAggInterval)) {
          <section class="tb-form-row column-xs space-between same-padding"
                   >
            <div>{{ 'aggregation.limit' | translate }}</div>
            <tb-datapoints-limit formControlName="limit"
                                 [required]="timewindowForm.get('aggregation.type').value === aggregationTypes.NONE">
            </tb-datapoints-limit>
          </section>
        }
      </ng-container>
      @if (timewindowForm.get('aggregation.type').value !== aggregationTypes.NONE) {
        <section class="tb-form-row column-xs same-padding"
                 [class.!hidden]="!(isEdit || !timewindow.hideAggInterval)">
          @if (timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME) {
            <ng-container formGroupName="realtime">
              <tb-timeinterval
                formControlName="interval"
                [min]="minRealtimeAggInterval" [max]="maxRealtimeAggInterval"
                [allowedIntervals]="realtimeAllowedAggIntervals"
                useCalendarIntervals
                subscriptSizing="dynamic"
                appearance="outline"
                [disabledAdvanced]="realtimeDisableCustomGroupInterval">
                <div class="fixed-title-width-180">{{ 'aggregation.group-interval' | translate }}</div>
              </tb-timeinterval>
            </ng-container>
          }
          @if (timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY) {
            <ng-container formGroupName="history">
              <tb-timeinterval
                formControlName="interval"
                [min]="minHistoryAggInterval" [max]="maxHistoryAggInterval"
                [allowedIntervals]="historyAllowedAggIntervals"
                useCalendarIntervals
                subscriptSizing="dynamic"
                appearance="outline"
                [disabledAdvanced]="historyDisableCustomGroupInterval">
                <div class="fixed-title-width-180">{{ 'aggregation.group-interval' | translate }}</div>
              </tb-timeinterval>
            </ng-container>
          }
        </section>
      }
    }
    @if (saveAsDefaultAvailable) {
      <mat-checkbox class="tb-timewindow-form-checkbox" [formControl]="saveTimewindowControl">
        {{ 'timewindow.save-current-settings-as-default' | translate }}
      </mat-checkbox>
    }
  </div>

  <ng-template #timezoneSelection>
    @if (timezone && (isEdit || !timewindow.hideTimezone)) {
      <tb-timezone
        asButton strokedButton noMargin
        localBrowserTimezonePlaceholderOnEmpty="true"
        formControlName="timezone">
      </tb-timezone>
    }
  </ng-template>
  @if (panelMode) {
    <mat-divider></mat-divider>
  }
  @if (panelMode) {
    <div class="tb-panel-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.update' | translate }}
      </button>
    </div>
  }
</form>
