<!--

    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.

-->
<section [formGroup]="alarmScheduleForm" class="flex flex-col">
  <mat-form-field class="mat-block">
    <mat-select formControlName="type" required placeholder="{{ 'device-profile.schedule-type' | translate }}">
      @for (alarmScheduleType of alarmScheduleTypes; track alarmScheduleType) {
        <mat-option [value]="alarmScheduleType">
          {{ alarmScheduleTypeTranslate.get(alarmScheduleType) | translate }}
        </mat-option>
      }
    </mat-select>
    @if (alarmScheduleForm.get('type').hasError('required')) {
      <mat-error>
        {{ 'device-profile.schedule-type-required' | translate }}
      </mat-error>
    }
  </mat-form-field>
  @if (alarmScheduleForm.get('type').value !== alarmScheduleType.ANY_TIME) {
    <div>
      <tb-timezone-select
        userTimezoneByDefault
        required
        formControlName="timezone">
      </tb-timezone-select>
      @if (alarmScheduleForm.get('type').value === alarmScheduleType.SPECIFIC_TIME) {
        <section>
          <tb-alarm-dynamic-value formControlName = 'dynamicValue' helpId = 'device-profile/alarm_specific_schedule_format'></tb-alarm-dynamic-value>
          <div class="tb-small" style="margin-bottom: 0.5em" translate>device-profile.schedule-days</div>
          <div class="flex flex-col gap-2 gt-md:flex-row">
            <div class="flex flex-row gap-2">
              @for (day of firstRowDays; track day) {
                <mat-checkbox [formControl]="weeklyRepeatControl(day)">
                  {{ dayOfWeekTranslationsArray[day] | translate }}
                </mat-checkbox>
              }
            </div>
            <div class="flex flex-row gap-2">
              @for (day of secondRowDays; track day) {
                <mat-checkbox [formControl]="weeklyRepeatControl(day)">
                  {{ dayOfWeekTranslationsArray[day] | translate }}
                </mat-checkbox>
              }
            </div>
          </div>
      <tb-error style="display: block;" [error]="alarmScheduleForm.get('daysOfWeek').hasError('dayOfWeeks')
                  ? ('device-profile.schedule-days-of-week-required' | translate) : ''"></tb-error>
          <div class="tb-small" style="margin-bottom: 0.5em" translate>device-profile.schedule-time</div>
          <div class="flex flex-row xs:flex-col gt-xs:gap-2">
            <div class="flex flex-row gap-2 gt-md:flex-1">
              <mat-form-field class="flex-1 sm:min-w-37.5 sm:max-w-37.5 sm:basis-37.5 md:min-w-37.5 md:max-w-37.5 md:basis-37.5">
                <mat-label translate>device-profile.schedule-time-from</mat-label>
                <mat-datetimepicker-toggle [for]="startTimePicker" matPrefix></mat-datetimepicker-toggle>
                <mat-datetimepicker #startTimePicker type="time" openOnFocus="true"></mat-datetimepicker>
                <input required matInput formControlName="startsOn" [matDatetimepicker]="startTimePicker">
              </mat-form-field>
              <mat-form-field class="flex-1 sm:min-w-37.5 sm:max-w-37.5 sm:basis-37.5 md:min-w-37.5 md:max-w-37.5 md:basis-37.5">
                <mat-label translate>device-profile.schedule-time-to</mat-label>
                <mat-datetimepicker-toggle [for]="endTimePicker" matPrefix></mat-datetimepicker-toggle>
                <mat-datetimepicker #endTimePicker type="time" openOnFocus="true"></mat-datetimepicker>
                <input required matInput formControlName="endsOn" [matDatetimepicker]="endTimePicker">
              </mat-form-field>
            </div>
            <div class="flex flex-1 items-center justify-center" style="margin: auto">
              <div style="text-align: center"
                   [innerHTML]="getSchedulerRangeText(alarmScheduleForm)">
              </div>
            </div>
          </div>
        </section>
      }
      @if (alarmScheduleForm.get('type').value === alarmScheduleType.CUSTOM) {
        <section>
          <tb-alarm-dynamic-value formControlName = 'dynamicValue' helpId = 'device-profile/alarm_custom_schedule_format'></tb-alarm-dynamic-value>
          <div class="tb-small" style="margin-bottom: 0.5em" translate>device-profile.schedule-days</div>
          @for (day of allDays; track day) {
            <div class="flex flex-col gap-4" formArrayName="items">
              <div class="flex flex-row items-center justify-start gap-2 xs:flex-col xs:items-start xs:justify-center" [formGroupName]="''+day">
                <mat-checkbox formControlName="enabled" class="max-w-17% flex-full" (change)="changeCustomScheduler($event, day)">
                  {{ dayOfWeekTranslationsArray[day] | translate }}
                </mat-checkbox>
                <div class="flex flex-1 flex-row gap-2">
                  <mat-form-field class="xs:flex-1 sm:min-w-25 sm:max-w-25 sm:flex-1 sm:basis-25 md:min-w-25 md:max-w-25 md:flex-1 md:basis-25">
                    <mat-label translate>device-profile.schedule-time-from</mat-label>
                    <mat-datetimepicker-toggle [for]="startTimePicker" matPrefix></mat-datetimepicker-toggle>
                    <mat-datetimepicker #startTimePicker type="time" openOnFocus="true"></mat-datetimepicker>
                    <input required matInput formControlName="startsOn" [matDatetimepicker]="startTimePicker">
                  </mat-form-field>
                  <mat-form-field class="xs:flex-1 sm:min-w-25 sm:max-w-25 sm:flex-1 sm:basis-25 md:min-w-25 md:max-w-25 md:flex-1 md:basis-25">
                    <mat-label translate>device-profile.schedule-time-to</mat-label>
                    <mat-datetimepicker-toggle [for]="endTimePicker" matPrefix></mat-datetimepicker-toggle>
                    <mat-datetimepicker #endTimePicker type="time" openOnFocus="true"></mat-datetimepicker>
                    <input required matInput formControlName="endsOn" [matDatetimepicker]="endTimePicker">
                  </mat-form-field>
                </div>
                <div class="flex flex-1 items-center justify-center"
                     style="text-align: center"
                     [innerHTML]="getSchedulerRangeText(itemsSchedulerForm.at(day))">
                </div>
              </div>
            </div>
          }
      <tb-error style="display: block;" [error]="alarmScheduleForm.get('items').hasError('dayOfWeeks')
                  ? ('device-profile.schedule-days-of-week-required' | translate) : ''"></tb-error>
        </section>
      }
    </div>
  }
</section>
