<!--

    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.

-->
<div>
  <mat-card appearance="outlined" class="settings-card tb-absolute-fill">
    <mat-card-header>
      <div class="flex flex-1 flex-row items-start justify-between xs:flex-col xs:justify-start xs:gap-2">
        <div class="flex flex-1 flex-col">
          <span class="mat-headline-5" translate>notification.settings.notification-settings</span>
        </div>
        <div class="flex flex-col">
          <button mat-button color="primary"
                  [disabled]="(isLoading$ | async)"
          (click)="resetSettings()">{{ 'notification.settings.reset-all' | translate }}</button>
        </div>
      </div>
    </mat-card-header>
    @if (isLoading$ | async) {
      <mat-progress-bar color="warn" mode="indeterminate">
      </mat-progress-bar>
    }
    @if (!(isLoading$ | async)) {
      <div style="height: 4px;"></div>
    }
    <mat-card-content style="padding-top: 16px; overflow: hidden;">
      <form [formGroup]="notificationSettings" class="notification-form">
        <section class="notification-section">
          <div  class="notification-section-block">
            <div class="flex flex-row items-center justify-start" style="height: 44px;">
              <div class="max-w-50% flex-full">
                <mat-checkbox color="warn"
                              (click)="$event.stopPropagation()"
                              [indeterminate]="getIndeterminate()"
                              (change)="changeInstanceTypeCheckBox($event.checked)"
                              [checked]="getChecked()">
                  <mat-label translate>notification.settings.type</mat-label>
                </mat-checkbox>
              </div>
              @for (deliveryMethods of notificationDeliveryMethods; track deliveryMethods) {
                <div class="flex-1">
                  <div class="flex flex-1 items-center justify-start">
                    <mat-checkbox color="warn"
                                  [checked]="getChecked(deliveryMethods)"
                                  (change)="changeInstanceTypeCheckBox($event.checked, deliveryMethods)"
                                  [indeterminate]="getIndeterminate(deliveryMethods)"
                                  (click)="$event.stopPropagation()">
                      <mat-label>{{ notificationDeliveryMethodInfoMap.get(deliveryMethods).name | translate }}</mat-label>
                    </mat-checkbox>
                  </div>
                </div>
              }
            </div>
            <mat-divider></mat-divider>
            @for (settingsControl of notificationSettingsFormArray.controls; track settingsControl; let i = $index) {
              <div>
                <tb-notification-setting-form [formControl]="settingsControl"
                                              [deliveryMethods]="notificationDeliveryMethods">
                </tb-notification-setting-form>
                @if (!$last) {
                  <mat-divider></mat-divider>
                }
              </div>
            }
          </div>
        </section>
      </form>
    </mat-card-content>
    <div class="flex flex-row items-start justify-end" style="padding: 16px;">
      <button mat-button mat-raised-button color="primary"
              type="button"
              (click)="save()"
              [disabled]="(isLoading$ | async) || notificationSettings.invalid || !notificationSettings.dirty">
        {{ 'action.save' | translate }}
      </button>
    </div>
  </mat-card>
</div>
