<!--

    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.

-->
<mat-expansion-panel class="device-profile-alarm flex-1" [formGroup]="alarmFormGroup" [(expanded)]="expanded">
  <mat-expansion-panel-header>
    <div class="flex flex-1 flex-row items-center justify-start">
      <mat-panel-title>
        <div class="flex flex-1 flex-row items-center justify-start">
          {{ alarmTypeTitle }}
        </div>
      </mat-panel-title>
      <span class="flex-1"></span>
      @if (!disabled) {
        <button mat-icon-button style="min-width: 40px;"
                type="button"
                (click)="removeAlarm.emit()"
                matTooltip="{{ 'action.remove' | translate }}"
                matTooltipPosition="above">
          <mat-icon>delete</mat-icon>
        </button>
      }
    </div>
  </mat-expansion-panel-header>
  <ng-template matExpansionPanelContent>
    <div class="flex flex-col gap-2">
      <mat-divider></mat-divider>
      <mat-form-field class="flex-1" floatLabel="always">
        <mat-label>{{'device-profile.alarm-type' | translate}}</mat-label>
        <input required matInput formControlName="alarmType" placeholder="Enter alarm type">
        @if (alarmFormGroup.get('alarmType').hasError('required')) {
          <mat-error>
            {{ 'device-profile.alarm-type-required' | translate }}
          </mat-error>
        }
        @if (alarmFormGroup.get('alarmType').hasError('unique')) {
          <mat-error>
            {{ 'device-profile.alarm-type-unique' | translate }}
          </mat-error>
        }
        @if (alarmFormGroup.get('alarmType').hasError('maxlength')) {
          <mat-error>
            {{ 'device-profile.alarm-type-max-length' | translate }}
          </mat-error>
        }
      </mat-form-field>
    </div>
    <mat-expansion-panel class="advanced-settings" [expanded]="false">
      <mat-expansion-panel-header>
        <mat-panel-title>
          <div class="flex flex-1 flex-row items-center justify-end">
            <div class="tb-small" translate>device-profile.advanced-settings</div>
          </div>
        </mat-panel-title>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <mat-checkbox formControlName="propagate" style="display: block;">
          {{ 'device-profile.propagate-alarm' | translate }}
        </mat-checkbox>
        @if (alarmFormGroup.get('propagate').value === true) {
          <section>
            <mat-form-field floatLabel="always" class="mat-block">
              <mat-label translate>device-profile.alarm-rule-relation-types-list</mat-label>
              <mat-chip-grid #relationTypesChipList [disabled]="disabled">
                @for (key of alarmFormGroup.get('propagateRelationTypes').value; track key) {
                  <mat-chip-row
                    (removed)="removeRelationType(key)">
                    {{key}}
                    <mat-icon matChipRemove>close</mat-icon>
                  </mat-chip-row>
                }
                <input matInput type="text" placeholder="{{'device-profile.alarm-rule-relation-types-list' | translate}}"
                       [matChipInputFor]="relationTypesChipList"
                       [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                       matChipInputAddOnBlur
                       (matChipInputTokenEnd)="addRelationType($event)">
              </mat-chip-grid>
              <mat-hint innerHTML="{{ 'device-profile.alarm-rule-relation-types-list-hint' | translate }}"></mat-hint>
            </mat-form-field>
          </section>
        }
        <mat-checkbox formControlName="propagateToOwner" style="display: block;">
          {{ 'device-profile.propagate-alarm-to-owner' | translate }}
        </mat-checkbox>
        <mat-checkbox formControlName="propagateToTenant" style="display: block;">
          {{ 'device-profile.propagate-alarm-to-tenant' | translate }}
        </mat-checkbox>
      </ng-template>
    </mat-expansion-panel>
    <div class="flex flex-1 flex-col">
      <div translate class="tb-small" style="padding-bottom: 8px;">device-profile.create-alarm-rules</div>
      <tb-create-alarm-rules formControlName="createRules"
                             style="padding-bottom: 16px;"
                             [deviceProfileId]="deviceProfileId">
      </tb-create-alarm-rules>
      <div translate class="tb-small" style="padding-bottom: 8px;">device-profile.clear-alarm-rule</div>
      <div class="flex flex-row items-center justify-start gap-2"
           [class.!hidden]="!alarmFormGroup.get('clearRule').value"
           style="padding-bottom: 8px;">
        <div class="clear-alarm-rule flex flex-1 flex-row">
          <tb-alarm-rule formControlName="clearRule" class="flex-1" [deviceProfileId]="deviceProfileId">
          </tb-alarm-rule>
        </div>
        @if (!disabled) {
          <button
            mat-icon-button color="primary" style="min-width: 40px;"
            type="button"
            (click)="removeClearAlarmRule()"
            matTooltip="{{ 'action.remove' | translate }}"
            matTooltipPosition="above">
            <mat-icon>remove_circle_outline</mat-icon>
          </button>
        }
      </div>
      @if (disabled && !alarmFormGroup.get('clearRule').value) {
        <div>
          <span translate style="margin: 16px 0"
          class="tb-prompt flex items-center justify-center">device-profile.no-clear-alarm-rule</span>
        </div>
      }
      @if (!disabled) {
        <div [class.!hidden]="alarmFormGroup.get('clearRule').value">
          <button mat-stroked-button color="primary"
                  type="button"
                  (click)="addClearAlarmRule()"
                  matTooltip="{{ 'device-profile.add-clear-alarm-rule' | translate }}"
                  matTooltipPosition="above">
            <mat-icon class="button-icon">add_circle_outline</mat-icon>
            {{ 'device-profile.add-clear-alarm-rule' | translate }}
          </button>
        </div>
      }
    </div>
  </ng-template>
</mat-expansion-panel>
