<!--

    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.

-->
<form [formGroup]="queueFormGroup" class="flex flex-col">
  <mat-form-field class="mat-block" appearance="outline">
    <mat-label translate>admin.queue-name</mat-label>
    <input matInput formControlName="name" required>
    @if (queueFormGroup.get('name').hasError('required')) {
      <mat-error>
        {{ 'queue.name-required' | translate }}
      </mat-error>
    }
    @if (queueFormGroup.get('name').hasError('unique')) {
      <mat-error>
        {{ 'queue.name-unique' | translate }}
      </mat-error>
    }
    @if (queueFormGroup.get('name').hasError('pattern')) {
      <mat-error>
        {{ 'queue.name-pattern' | translate }}
      </mat-error>
    }
  </mat-form-field>
  <mat-accordion class="queue-strategy" [multi]="true">
    <mat-expansion-panel [expanded]="true">
      <mat-expansion-panel-header>
        <mat-panel-title class="panel-title" translate>
          queue.submit-settings
        </mat-panel-title>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <div class="flex flex-1 flex-row gap-4 lt-md:flex-col" formGroupName="submitStrategy">
          <div class="flex-1">
            <label class="group-label" translate>queue.submit-strategy</label>
            <mat-radio-group class="flex flex-1 flex-col" formControlName="type" required>
              @for (strategy of submitStrategies; track strategy) {
                <mat-radio-button [value]="strategy">
                  {{ queueSubmitStrategyTypesMap.get(queueSubmitStrategyTypes[strategy]).label | translate }}
                  <mat-icon
                    class="hint-icon"
                    matTooltip="{{ queueSubmitStrategyTypesMap.get(queueSubmitStrategyTypes[strategy]).hint | translate }}"
                    svgIcon="mdi:information-outline">
                  </mat-icon>
                </mat-radio-button>
              }
            </mat-radio-group>
          </div>
          @if (hideBatchSize) {
            <div class="flex-1">
              <label class="group-label" translate>queue.grouping-parameter</label>
              <mat-form-field class="mat-block" appearance="outline">
                <mat-label translate>queue.batch-size</mat-label>
                <input type="number" matInput formControlName="batchSize" required>
                @if (queueFormGroup.get('submitStrategy.batchSize').hasError('required')) {
                  <mat-error>
                    {{ 'queue.batch-size-required' | translate }}
                  </mat-error>
                }
                @if (queueFormGroup.get('submitStrategy.batchSize').hasError('min') &&
                  !queueFormGroup.get('submitStrategy.batchSize').hasError('required')) {
                  <mat-error>
                    {{ 'queue.batch-size-min-value' | translate }}
                  </mat-error>
                }
              </mat-form-field>
            </div>
          }
        </div>
      </ng-template>
    </mat-expansion-panel>
    <mat-expansion-panel [expanded]="true">
      <mat-expansion-panel-header>
        <mat-panel-title class="panel-title" translate>
          queue.processing-settings
        </mat-panel-title>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <div class="flex flex-1 flex-row gap-4 lt-md:flex-col" formGroupName="processingStrategy">
          <div class="flex-1">
            <label class="group-label" translate>queue.processing-strategy</label>
            <mat-radio-group class="flex flex-1 flex-col" formControlName="type" required>
              @for (strategy of processingStrategies; track strategy) {
                <mat-radio-button [value]="strategy">
                  {{ queueProcessingStrategyTypesMap.get(queueProcessingStrategyTypes[strategy]).label | translate }}
                  <mat-icon
                    class="hint-icon"
                    matTooltip="{{ queueProcessingStrategyTypesMap.get(queueProcessingStrategyTypes[strategy]).hint | translate }}"
                    svgIcon="mdi:information-outline">
                  </mat-icon>
                </mat-radio-button>
              }
            </mat-radio-group>
          </div>
          <div class="flex-1">
            <label class="group-label" translate>queue.retries-settings</label>
            <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
              <mat-label translate>queue.retries</mat-label>
              <input type="number" matInput formControlName="retries" required>
              <mat-hint></mat-hint>
              @if (queueFormGroup.get('processingStrategy.retries').hasError('required')) {
                <mat-error>
                  {{ 'queue.retries-required' | translate }}
                </mat-error>
              }
              @if (queueFormGroup.get('processingStrategy.retries').hasError('min') &&
                !queueFormGroup.get('processingStrategy.retries').hasError('required')) {
                <mat-error>
                  {{ 'queue.retries-min-value' | translate }}
                </mat-error>
              }
            </mat-form-field>
            <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
              <mat-label translate>queue.failure-percentage</mat-label>
              <input type="number" matInput formControlName="failurePercentage" required>
              <mat-hint></mat-hint>
              @if (queueFormGroup.get('processingStrategy.failurePercentage').hasError('required')) {
                <mat-error>
                  {{ 'queue.failure-percentage-required' | translate }}
                </mat-error>
              }
              @if (queueFormGroup.get('processingStrategy.failurePercentage').hasError('min') &&
                !queueFormGroup.get('processingStrategy.failurePercentage').hasError('required') &&
                !queueFormGroup.get('processingStrategy.failurePercentage').hasError('max')) {
                <mat-error>
                  {{ 'queue.failure-percentage-min-value' | translate }}
                </mat-error>
              }
              @if (queueFormGroup.get('processingStrategy.failurePercentage').hasError('max') &&
                !queueFormGroup.get('processingStrategy.failurePercentage').hasError('required') &&
                !queueFormGroup.get('processingStrategy.failurePercentage').hasError('min')) {
                <mat-error>
                  {{ 'queue.failure-percentage-max-value' | translate }}
                </mat-error>
              }
            </mat-form-field>
            <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
              <mat-label translate>queue.pause-between-retries</mat-label>
              <input type="number" matInput formControlName="pauseBetweenRetries" required>
              <mat-hint></mat-hint>
              @if (queueFormGroup.get('processingStrategy.pauseBetweenRetries').hasError('required')) {
                <mat-error>
                  {{ 'queue.pause-between-retries-required' | translate }}
                </mat-error>
              }
              @if (queueFormGroup.get('processingStrategy.pauseBetweenRetries').hasError('min') &&
                !queueFormGroup.get('processingStrategy.pauseBetweenRetries').hasError('required')) {
                <mat-error>
                  {{ 'queue.pause-between-retries-min-value' | translate }}
                </mat-error>
              }
            </mat-form-field>
            <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
              <mat-label translate>queue.max-pause-between-retries</mat-label>
              <input type="number" matInput formControlName="maxPauseBetweenRetries" required>
              <mat-hint></mat-hint>
              @if (queueFormGroup.get('processingStrategy.maxPauseBetweenRetries').hasError('required')) {
                <mat-error>
                  {{ 'queue.max-pause-between-retries-required' | translate }}
                </mat-error>
              }
              @if (queueFormGroup.get('processingStrategy.maxPauseBetweenRetries').hasError('min') &&
                !queueFormGroup.get('processingStrategy.maxPauseBetweenRetries').hasError('required')) {
                <mat-error>
                  {{ 'queue.max-pause-between-retries-min-value' | translate }}
                </mat-error>
              }
            </mat-form-field>
          </div>
        </div>
      </ng-template>
    </mat-expansion-panel>
    <mat-expansion-panel [expanded]="true">
      <mat-expansion-panel-header>
        <mat-panel-title class="panel-title" translate>
          queue.polling-settings
        </mat-panel-title>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <div class="flex flex-1 flex-col gap-2.5">
          <div class="flex-1">
            <label class="group-label" translate>queue.batch-processing</label>
            <div class="flex flex-1 flex-row gap-2.5 xs:flex-col">
              <mat-form-field class="mat-block flex-1" appearance="outline">
                <mat-label translate>queue.poll-interval</mat-label>
                <input type="number" matInput formControlName="pollInterval" required>
                @if (queueFormGroup.get('pollInterval').hasError('required')) {
                  <mat-error>
                    {{ 'queue.poll-interval-required' | translate }}
                  </mat-error>
                }
                @if (queueFormGroup.get('pollInterval').hasError('min') &&
                  !queueFormGroup.get('pollInterval').hasError('required')) {
                  <mat-error>
                    {{ 'queue.poll-interval-min-value' | translate }}
                  </mat-error>
                }
              </mat-form-field>
              <mat-form-field class="mat-block flex-1" appearance="outline">
                <mat-label translate>queue.partitions</mat-label>
                <input type="number" matInput formControlName="partitions" required>
                @if (queueFormGroup.get('partitions').hasError('required')) {
                  <mat-error>
                    {{ 'queue.partitions-required' | translate }}
                  </mat-error>
                }
                @if (queueFormGroup.get('partitions').hasError('min') &&
                  !queueFormGroup.get('partitions').hasError('required')) {
                  <mat-error>
                    {{ 'queue.partitions-min-value' | translate }}
                  </mat-error>
                }
              </mat-form-field>
            </div>
          </div>
          <div class="flex-1">
            <label class="group-label" translate>queue.immediate-processing</label>
            <div class="flex flex-1 flex-row gap-2.5 xs:flex-col">
              <mat-checkbox class="flex-1 gt-xs:flex gt-xs:items-stretch gt-xs:justify-center" formControlName="consumerPerPartition">
                {{ 'queue.consumer-per-partition' | translate }}
              </mat-checkbox>
              <mat-form-field class="mat-block flex-1" appearance="outline">
                <mat-label translate>queue.processing-timeout</mat-label>
                <input type="number" matInput formControlName="packProcessingTimeout" required>
                @if (queueFormGroup.get('packProcessingTimeout').hasError('required')) {
                  <mat-error>
                    {{ 'queue.pack-processing-timeout-required' | translate }}
                  </mat-error>
                }
                @if (queueFormGroup.get('packProcessingTimeout').hasError('min') &&
                  !queueFormGroup.get('packProcessingTimeout').hasError('required')) {
                  <mat-error>
                    {{ 'queue.pack-processing-timeout-min-value' | translate }}
                  </mat-error>
                }
              </mat-form-field>
            </div>
          </div>
        </div>
      </ng-template>
    </mat-expansion-panel>
  </mat-accordion>
  <div formGroupName="additionalInfo">
    <mat-checkbox class="flex-1" formControlName="duplicateMsgToAllPartitions" style="padding-bottom: 16px;">
      {{ 'queue.duplicate-msg-to-all-partitions' | translate }}
    </mat-checkbox>
    <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
      <mat-label translate>queue.custom-properties</mat-label>
      <textarea matInput formControlName="customProperties" cdkTextareaAutosize cdkAutosizeMinRows="1" rows="1"></textarea>
      <mat-hint translate>queue.custom-properties-hint</mat-hint>
    </mat-form-field>
    <mat-form-field class="mat-block" appearance="outline">
      <mat-label translate>queue.description</mat-label>
      <textarea matInput formControlName="description" cdkTextareaAutosize cdkAutosizeMinRows="1" rows="1"></textarea>
      <mat-hint translate>queue.description-hint</mat-hint>
    </mat-form-field>
  </div>
</form>
