<!--

    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 [formGroup]="fieldFormGroup" class="calculated-field-dialog-container flex h-full flex-col">
  <mat-toolbar color="primary">
    <h2>{{ 'alarm-rule.alarm-rule' | translate}}</h2>
    <span class="flex-1"></span>
    <div tb-help="alarmRules"></div>
    <button mat-icon-button
            [disabled]="isLoading"
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content class="flex-1">
    <div class="tb-form-panel no-border no-padding">
      <div class="tb-form-panel">
        <div class="tb-form-panel-title">{{ 'common.general' | translate }}</div>
        <div class="flex gap-2">
          <mat-form-field class="flex-1" appearance="outline" subscriptSizing="dynamic">
            <mat-label>{{ 'alarm-rule.alarm-type' | translate }}</mat-label>
            <input matInput maxlength="255" formControlName="name" required>
            <mat-hint translate>alarm-rule.alarm-type-hint</mat-hint>
            @if (fieldFormGroup.get('name').errors && fieldFormGroup.get('name').touched) {
              <mat-error>
                @if (fieldFormGroup.get('name').hasError('required')) {
                  {{ 'alarm-rule.alarm-type-required' | translate }}
                } @else if (fieldFormGroup.get('name').hasError('maxlength')) {
                  {{ 'alarm-rule.alarm-type-max-length' | translate }}
                }
              </mat-error>
            }
          </mat-form-field>
          <tb-entity-debug-settings-button
            formControlName="debugSettings"
            entityLabel="alarm-rule.alarm-rule"
            style="padding-top: 10px;"
            [class.mb-5]="fieldFormGroup.get('name').errors && fieldFormGroup.get('name').touched"
            [entityType]="EntityType.CALCULATED_FIELD"
            [additionalActionConfig]="additionalDebugActionConfig"
          />
        </div>
        @if (!data.entityId) {
          <tb-entity-select formControlName="entityId"
                            #entitySelect
                            appearance="outline"
                            required
                            entityTypeLabel="{{ 'entity.entity-type' | translate }}"
                            [filterAllowedEntityTypes]="false"
                            [allowedEntityTypes]="alarmRuleEntityTypeList"
                            [defaultEntityType]="EntityType.DEVICE_PROFILE"
                            (entityChanged)="changeEntity($event)">
          </tb-entity-select>
        }
      </div>
      <ng-container formGroupName="configuration">
        <div class="tb-form-panel" [class.disabled]="disabledArguments">
          <div class="tb-form-panel-title tb-required">{{ 'calculated-fields.arguments' | translate }}</div>
          <tb-calculated-field-arguments-table formControlName="arguments"
                                               [entityId]="data.entityId || fieldFormGroup.get('entityId').value"
                                               [tenantId]="data.tenantId"
                                               [ownerId]="ownerId"
                                               [watchKeyChange]="true"
                                               [entityName]="entityName"/>
        </div>
        <div class="tb-form-panel" [class.disabled]="disabledClearRuleButton">
          <div class="tb-form-panel-title">{{ 'alarm-rule.create-conditions' | translate }}</div>
          <div class="flex flex-1 flex-col">
            <tb-create-cf-alarm-rules formControlName="createRules" [arguments]="arguments" [testScript]="onTestScript.bind(this)">
            </tb-create-cf-alarm-rules>
          </div>
        </div>
        <div class="tb-form-panel" [class.disabled]="disabledClearRuleButton">
          <div class="tb-form-panel-title">{{ 'alarm-rule.clear-condition' | translate }}</div>
          <div class="flex flex-row items-center justify-start gap-2 pb-2"
               [class.!hidden]="!configFormGroup.get('clearRule').value">
            <div class="clear-alarm-rule flex flex-1 flex-row">
              <tb-cf-alarm-rule formControlName="clearRule" class="flex-1" [arguments]="arguments" [testScript]="onTestScript.bind(this)" isClearCondition>
              </tb-cf-alarm-rule>
            </div>
            <button mat-icon-button
                    class="button-icon"
                    type="button"
                    (click)="removeClearAlarmRule()"
                    matTooltip="{{ 'action.remove' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>delete</mat-icon>
            </button>
          </div>
          @if (!configFormGroup.get('clearRule').value) {
            <div>
              <span translate class="tb-prompt flex items-center justify-center text-base">alarm-rule.no-clear-alarm-rule</span>
            </div>
          }
          <div [class.!hidden]="configFormGroup.get('clearRule').value">
            <button mat-stroked-button color="primary"
                    [disabled]="disabledClearRuleButton"
                    type="button"
                    (click)="addClearAlarmRule()">
              {{ 'alarm-rule.add-clear-alarm-rule' | translate }}
            </button>
          </div>
        </div>
        <div class="tb-form-panel no-gap">
          <mat-expansion-panel class="tb-settings" [expanded]="false">
            <mat-expansion-panel-header [class.tb-disabled]="disabledClearRuleButton">
              {{ 'alarm-rule.advanced-settings' | translate }}
            </mat-expansion-panel-header>
            <ng-template matExpansionPanelContent>
              <div class="tb-form-panel stroked no-padding no-gap">
                <div class="tb-form-row no-border">
                  <mat-slide-toggle class="mat-slide margin" formControlName="propagate">
                    {{ 'alarm-rule.propagate-alarm' | translate }}
                  </mat-slide-toggle>
                </div>
                @if (configFormGroup.get('propagate').value) {
                  <tb-string-items-list appearance="outline"
                                        class="flex flex-1 px-4"
                                        label="{{ 'alarm-rule.alarm-rule-relation-types-list' | translate }}"
                                        placeholder="{{ 'alarm-rule.alarm-rule-relation-types-list' | translate }}"
                                        hint="{{ 'alarm-rule.alarm-rule-relation-types-list-hint' | translate }}"
                                        [predefinedValues]="predefinedTypeValues"
                                        allowUserValue
                                        formControlName="propagateRelationTypes">
                  </tb-string-items-list>
                }
              </div>
              <div class="tb-form-row">
                <mat-slide-toggle class="mat-slide margin" formControlName="propagateToOwner">
                  {{ 'alarm-rule.propagate-alarm-to-owner' | translate }}
                </mat-slide-toggle>
              </div>
              <div class="tb-form-row">
                <mat-slide-toggle class="mat-slide margin" formControlName="propagateToTenant">
                  {{ 'alarm-rule.propagate-alarm-to-tenant' | translate }}
                </mat-slide-toggle>
              </div>
            </ng-template>
          </mat-expansion-panel>
        </div>
      </ng-container>
    </div>
  </div>
  <div mat-dialog-actions class="justify-end">
    <button mat-button color="primary"
            type="button"
            [disabled]="isLoading"
            cdkFocusInitial
            (click)="cancel()">
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            (click)="add()"
            [disabled]="isLoading">
      {{ data.buttonTitle | translate }}
    </button>
  </div>
</div>
