<!--

    Copyright © 2016-2022 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" fxLayout="column">
  <mat-form-field class="mat-block" hideRequiredMarker floatLabel="always">
    <mat-label> </mat-label>
    <mat-select formControlName="type" required placeholder="{{ 'device-profile.schedule-type' | translate }}">
      <mat-option *ngFor="let alarmScheduleType of alarmScheduleTypes" [value]="alarmScheduleType">
        {{ alarmScheduleTypeTranslate.get(alarmScheduleType) | translate }}
      </mat-option>
    </mat-select>
    <mat-error *ngIf="alarmScheduleForm.get('type').hasError('required')">
      {{ 'device-profile.schedule-type-required' | translate }}
    </mat-error>
  </mat-form-field>
  <div *ngIf="alarmScheduleForm.get('type').value !== alarmScheduleType.ANY_TIME">
    <tb-timezone-select
      userTimezoneByDefault
      required
      formControlName="timezone">
    </tb-timezone-select>
    <section *ngIf="alarmScheduleForm.get('type').value === alarmScheduleType.SPECIFIC_TIME">
      <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 fxLayout="column" fxLayout.gt-md="row" fxLayoutGap="16px">
        <div fxLayout="row" fxLayoutGap="16px">
          <mat-checkbox *ngFor="let day of firstRowDays" [formControl]="weeklyRepeatControl(day)">
            {{ dayOfWeekTranslationsArray[day] | translate }}
          </mat-checkbox>
        </div>
        <div fxLayout="row" fxLayoutGap="16px">
          <mat-checkbox *ngFor="let day of secondRowDays" [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 fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="8px">
        <div fxLayout="row" fxLayoutGap="8px" fxFlex.gt-md>
          <mat-form-field fxFlex.xs fxFlex.sm="150px" fxFlex.md="150px" fxFlex.gt-md>
            <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 fxFlex.xs fxFlex.sm="150px" fxFlex.md="150px" fxFlex.gt-md>
            <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 fxFlex fxLayoutAlign="center center" style="margin: auto">
          <div style="text-align: center"
               [innerHTML]="getSchedulerRangeText(alarmScheduleForm)">
          </div>
        </div>
      </div>
    </section>
    <section *ngIf="alarmScheduleForm.get('type').value === alarmScheduleType.CUSTOM">
      <tb-alarm-dynamic-value formControlName = 'dynamicValue' helpId = 'device-profile/alarm_сustom_schedule_format'></tb-alarm-dynamic-value>
      <div class="tb-small" style="margin-bottom: 0.5em" translate>device-profile.schedule-days</div>
      <div *ngFor="let day of allDays" fxLayout="column" formArrayName="items" fxLayoutGap="1em">
        <div fxLayout.xs="column" fxLayout="row" fxLayoutGap="8px" [formGroupName]="''+day" fxLayoutAlign="start center" fxLayoutAlign.xs="center start">
          <mat-checkbox formControlName="enabled" fxFlex="17" (change)="changeCustomScheduler($event, day)">
            {{ dayOfWeekTranslationsArray[day] | translate }}
          </mat-checkbox>
          <div fxLayout="row" fxLayoutGap="8px" fxFlex>
            <mat-form-field fxFlex.xs fxFlex.sm="100px" fxFlex.md="100px">
              <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 fxFlex.xs fxFlex.sm="100px" fxFlex.md="100px">
              <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 fxFlex fxLayoutAlign="center 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>
