<!--

    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-toolbar color="primary">
  <h2>{{ dialogTitle | translate }}</h2>
  <span class="flex-1"></span>
  <div tb-help="sentNotification"></div>
  <button mat-icon-button
          (click)="cancel()"
          type="button">
    <mat-icon class="material-icons">close</mat-icon>
  </button>
</mat-toolbar>
@if (isLoading$ | async) {
  <mat-progress-bar color="warn" mode="indeterminate">
  </mat-progress-bar>
}
<div mat-dialog-content>
  <mat-horizontal-stepper linear #createNotification
                          labelPosition="end"
                          [orientation]="(stepperOrientation | async)"
                          (selectionChange)="changeStep($event)">
    <ng-template matStepperIcon="edit">
      <mat-icon>check</mat-icon>
    </ng-template>
    <mat-step [stepControl]="notificationRequestForm">
      <ng-template matStepLabel>{{ 'notification.setup' | translate }}</ng-template>
      <form [formGroup]="notificationRequestForm">
        <div class="flex items-stretch justify-center">
          <tb-toggle-select formControlName="useTemplate" appearance="fill">
            <tb-toggle-option  [value]="false">{{ 'notification.start-from-scratch' | translate }}</tb-toggle-option>
            <tb-toggle-option  [value]="true">{{ 'notification.use-template' | translate }}</tb-toggle-option>
          </tb-toggle-select>
        </div>
        <div>
          @if (notificationRequestForm.get('useTemplate').value) {
            <tb-template-autocomplete
              required allowCreate allowEdit
              appearance="outline"
              formControlName="templateId"
              [notificationTypes]="notificationType.GENERAL">
            </tb-template-autocomplete>
            <ng-container *ngTemplateOutlet="recipientsList"></ng-container>
          } @else {
            <ng-container *ngTemplateOutlet="recipientsList"></ng-container>
            <section formGroupName="template">
              <section formGroupName="configuration">
                <div class="delivery-title">
                  <div>
                    <label [class.tb-error]="notificationRequestForm.get('template.configuration.deliveryMethodsTemplates').hasError('atLeastOne')"
                    class="tb-title tb-required">{{ "notification.delivery-methods" | translate }}</label>
                    <div class="tb-form-hint" translate>notification.at-least-one-should-be-selected</div>
                  </div>
                  @if (showRefresh) {
                    <button
                      matTooltip="{{ 'notification.refresh-allow-delivery-method' | translate }}"
                      matTooltipPosition="above"
                      (click)="refreshAllowDeliveryMethod()"
                      mat-icon-button>
                      <mat-icon>refresh</mat-icon>
                    </button>
                  }
                </div>
                <section formGroupName="deliveryMethodsTemplates" class="delivery-methods-container">
                  @for (deliveryMethods of notificationDeliveryMethods; track deliveryMethods) {
                    @if (isInteractDeliveryMethod(deliveryMethods)) {
                      <a
                        class="tb-form-panel stroked delivery-method-container tb-pointer interact"
                        [routerLink]="configurationPage(deliveryMethods)"
                        [formGroupName]="deliveryMethods"
                        [matTooltip]="getDeliveryMethodsTooltip(deliveryMethods)"
                        matTooltipPosition="above">
                        <mat-slide-toggle class="delivery-method" formControlName="enabled">
                          {{ notificationDeliveryMethodInfoMap.get(deliveryMethods).name | translate }}
                        </mat-slide-toggle>
                        @if (isInteractDeliveryMethod(deliveryMethods)) {
                          <mat-icon>
                            chevron_right
                          </mat-icon>
                        }
                      </a>
                    } @else {
                      <section class="delivery-method-container tb-form-panel stroked"
                               [formGroupName]="deliveryMethods"
                               [matTooltip]="getDeliveryMethodsTooltip(deliveryMethods)"
                               [matTooltipDisabled]="getDeliveryMethodsTemplatesControl(deliveryMethods).enabled"
                               matTooltipPosition="above">
                        <mat-slide-toggle class="delivery-method" formControlName="enabled">
                          {{ notificationDeliveryMethodInfoMap.get(deliveryMethods).name | translate }}
                        </mat-slide-toggle>
                      </section>
                    }
                  }
                </section>
              </section>
            </section>
          }
        </div>
        <ng-template #recipientsList>
          <tb-entity-list
            syncIdsWithDB
            required
            formControlName="targets"
            appearance="outline"
            labelText="{{ 'notification.recipients' | translate }}"
            placeholderText="{{ 'notification.recipient' | translate }}"
            requiredText="{{ 'notification.recipients-required' | translate }}"
            [entityType]="entityType.NOTIFICATION_TARGET"
            subType="{{ notificationType.GENERAL }}">
            <button #createTargetButton
                    mat-button color="primary" matSuffix
                    (click)="createTarget($event, createTargetButton)">
              <span style="white-space: nowrap">{{ 'notification.create-new' | translate }}</span>
            </button>
          </tb-entity-list>
        </ng-template>
        <section formGroupName="additionalConfig" class="tb-form-panel stroked no-padding-bottom no-gap">
          <mat-slide-toggle formControlName="enabled" class="toggle">
            {{ 'notification.scheduler-later' | translate }}
          </mat-slide-toggle>
          @if (notificationRequestForm.get('additionalConfig.enabled').value) {
            <div class="flex flex-col">
              <tb-timezone-select userTimezoneByDefault
                                  required
                                  appearance="outline"
                                  formControlName="timezone">
              </tb-timezone-select>
              <mat-form-field appearance="outline">
                <mat-label translate>notification.time</mat-label>
                <mat-datetimepicker-toggle [for]="startTimePicker" matPrefix></mat-datetimepicker-toggle>
                <mat-datetimepicker #startTimePicker type="datetime" openOnFocus="true"></mat-datetimepicker>
                <input required matInput class="flex-1" formControlName="time" [min]="minDate()" [max]="maxDate()" [matDatetimepicker]="startTimePicker">
              </mat-form-field>
            </div>
          }
        </section>
      </form>
    </mat-step>
    @if (!notificationRequestForm.get('useTemplate').value) {
      <mat-step [stepControl]="notificationTemplateConfigurationForm" #composeStep=matStep>
        <ng-template matStepLabel>{{ 'notification.compose' | translate }}</ng-template>
        <ng-template matStepContent>
          <form [formGroup]="notificationTemplateConfigurationForm">
            <tb-template-configuration
              [notificationType]="notificationType.GENERAL"
              [predefinedDeliveryMethodsTemplate]="deliveryConfiguration"
              [interacted]="composeStep.interacted"
              formControlName="deliveryMethodsTemplates">
            </tb-template-configuration>
          </form>
        </ng-template>
      </mat-step>
    }
    <mat-step>
      <ng-template matStepLabel>{{ 'notification.review' | translate }}</ng-template>
      @if ((isLoading$ | async) && !preview) {
        <div class="tb-loader">
          <mat-progress-spinner color="warn" mode="indeterminate"
                                strokeWidth="5">
          </mat-progress-spinner>
        </div>
      }
      @if (preview) {
        <div class="tb-form-panel no-padding no-border">
          <section class="preview-group tb-form-panel stroked no-gap">
            <div class="flex flex-row items-center justify-start gap-2">
              <tb-icon class="tb-mat-18">supervisor_account</tb-icon>
              <div class="tb-form-panel-title">{{ 'notification.recipients-count' | translate : {count: preview.totalRecipientsCount} }}</div>
            </div>
            @if (notificationRequestForm.get('targets').value?.length > 1) {
              <div class="details-recipients">
                @for (detail of (preview.recipientsCountByTarget | keyvalue); track detail.key) {
                  <div class="details-recipient">
                    <span class="number">{{ detail.value }}</span>{{ detail.key }}
                  </div>
                }
              </div>
            }
            <mat-divider class="divider"></mat-divider>
            <mat-chip-set>
              @for (recipientTitle of preview.recipientsPreview; track recipientTitle) {
                <mat-chip-row class="tb-chip-row-ellipsis">
                  <span>{{ recipientTitle }}</span>
                </mat-chip-row>
              }
            </mat-chip-set>
          </section>
          @if (preview.processedTemplates.WEB?.enabled) {
            <section class="preview-group notification tb-form-panel stroked no-gap">
              <div class="flex flex-row items-center justify-start gap-2">
                <tb-icon class="tb-mat-18">mdi:bell-badge</tb-icon>
                <div class="tb-form-panel-title" translate>notification.delivery-method.web-preview</div>
              </div>
              <div class="web-preview">
                <tb-notification preview [notification]="preview.processedTemplates.WEB"></tb-notification>
              </div>
            </section>
          }
          @if (preview.processedTemplates.MOBILE_APP?.enabled) {
            <section class="preview-group notification tb-form-panel stroked no-gap">
              <div class="flex flex-row items-center justify-start gap-2">
                <tb-icon class="tb-mat-18">mdi:cellphone-text</tb-icon>
                <div class="tb-form-panel-title" translate>notification.delivery-method.mobile-app-preview</div>
              </div>
              <div class="notification-content">
                <div class="subject">{{ preview.processedTemplates.MOBILE_APP.subject }}</div>
                <div>{{ preview.processedTemplates.MOBILE_APP.body }}</div>
              </div>
            </section>
          }
          @if (preview.processedTemplates.SMS?.enabled) {
            <section class="preview-group notification tb-form-panel stroked no-gap">
              <div class="flex flex-row items-center justify-start gap-2">
                <tb-icon class="tb-mat-18">mdi:message-processing</tb-icon>
                <div class="tb-form-panel-title" translate>notification.delivery-method.sms-preview</div>
              </div>
              <div class="notification-content">
                {{ preview.processedTemplates.SMS.body }}
              </div>
            </section>
          }
          @if (preview.processedTemplates.EMAIL?.enabled) {
            <section class="preview-group notification tb-form-panel stroked no-gap">
              <div class="flex flex-row items-center justify-start gap-2">
                <tb-icon class="tb-mat-18">mdi:email</tb-icon>
                <div class="tb-form-panel-title" translate>notification.delivery-method.email-preview</div>
              </div>
              <div class="notification-content">
                <div class="subject">{{ preview.processedTemplates.EMAIL.subject }}</div>
                <mat-divider></mat-divider>
                <editor [init]="tinyMceOptions" [initialValue]="preview.processedTemplates.EMAIL.body"></editor>
              </div>
            </section>
          }
          @if (preview.processedTemplates.SLACK?.enabled) {
            <section class="preview-group notification tb-form-panel stroked no-gap">
              <div class="flex flex-row items-center justify-start gap-2">
                <tb-icon class="tb-mat-18">mdi:slack</tb-icon>
                <div class="tb-form-panel-title" translate>notification.delivery-method.slack-preview</div>
              </div>
              <div class="notification-content">
                {{ preview.processedTemplates.SLACK.body }}
              </div>
            </section>
          }
          @if (preview.processedTemplates.MICROSOFT_TEAMS?.enabled) {
            <section class="preview-group notification tb-form-panel stroked no-gap">
              <div class="flex flex-row items-center justify-start gap-2">
                <tb-icon class="tb-mat-18">mdi:microsoft-teams</tb-icon>
                <div class="tb-form-panel-title" translate>notification.delivery-method.microsoft-teams-preview</div>
              </div>
              <div class="notification-content mini" [style.background-color]="preview.processedTemplates.MICROSOFT_TEAMS?.themeColor">
                <div class="subject">{{ preview.processedTemplates.MICROSOFT_TEAMS.subject }}</div>
                {{ preview.processedTemplates.MICROSOFT_TEAMS.body }}
                @if (preview.processedTemplates.MICROSOFT_TEAMS.button?.enabled) {
                  <button mat-raised-button>{{ preview.processedTemplates.MICROSOFT_TEAMS.button?.text }}</button>
                }
              </div>
            </section>
          }
        </div>
      }
    </mat-step>
  </mat-horizontal-stepper>
</div>
<mat-divider></mat-divider>
<div mat-dialog-actions class="tb-dialog-actions">
  @if (selectedIndex > 0) {
    <button mat-stroked-button (click)="backStep()">{{ 'action.back' | translate }}</button>
  }
  <span class="flex-1"></span>
  <button mat-raised-button
          color="primary"
  (click)="nextStep()">{{ nextStepLabel() | translate }}</button>
</div>
