<!--

    Copyright © 2016-2023 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.

-->
<section class="tb-rate-limits-form" [formGroup]="rateLimitsListFormGroup">
  <div fxFlex [formGroup]="rateLimit" *ngFor="let rateLimit of rateLimitsFormArray.controls; let $index = index">
    <div class="tb-rate-limits-operation" *ngIf="$index > 0 && rateLimitsFormArray.controls.length > 1" translate>
      tenant-profile.rate-limits.but-less-than
    </div>
    <div fxFlex fxLayout="row" fxLayoutGap="8px">
      <mat-form-field fxFlex hideRequiredMarker appearance="fill" class="mat-block">
        <mat-label translate>tenant-profile.rate-limits.number-of-messages</mat-label>
        <input matInput placeholder="{{ 'tenant-profile.rate-limits.number-of-messages' | translate }}"
               type="number" min="1" step="1" formControlName="value" required>
        <mat-error *ngIf="rateLimit.get('value').hasError('required')">
          {{ 'tenant-profile.rate-limits.number-of-messages-required' | translate }}
        </mat-error>
        <mat-error *ngIf="rateLimit.get('value').hasError('min')">
          {{ 'tenant-profile.rate-limits.number-of-messages-min' | translate }}
        </mat-error>
      </mat-form-field>
      <mat-form-field fxFlex hideRequiredMarker appearance="fill" class="mat-block">
        <mat-label translate>tenant-profile.rate-limits.per-seconds</mat-label>
        <input matInput placeholder="{{ 'tenant-profile.rate-limits.per-seconds' | translate }}"
               type="number" min="1" step="1" formControlName="time" required>
        <mat-error *ngIf="rateLimit.get('time').hasError('required')">
          {{ 'tenant-profile.rate-limits.per-seconds-required' | translate }}
        </mat-error>
        <mat-error *ngIf="rateLimit.get('time').hasError('min')">
          {{ 'tenant-profile.rate-limits.per-seconds-min' | translate }}
        </mat-error>
      </mat-form-field>
      <button mat-icon-button type="button" color="primary"
              class="tb-rate-limits-button"
              *ngIf="!rateLimitsFormArray.disabled"
              (click)="removeRateLimits($index)"
              [disabled]="disabled"
              [matTooltip]="'tenant-profile.rate-limits.remove-limit' | translate" matTooltipPosition="above">
        <mat-icon>remove_circle_outline</mat-icon>
      </button>
    </div>
  </div>
  <button mat-raised-button color="primary" *ngIf="!rateLimitsFormArray.disabled"
          (click)="addRateLimits()"
          [disabled]="disabled"
          [matTooltip]="'tenant-profile.rate-limits.add-limit' | translate" matTooltipPosition="above">
    <span translate>tenant-profile.rate-limits.add-limit</span>
  </button>
  <div fxFlex fxLayoutGap="8px" fxLayout="column" class="tb-rate-limits-preview" [ngClass]="{'tb-rate-limits-preview-short': !disabled}">
    <span translate>tenant-profile.rate-limits.preview</span>
    <tb-rate-limits-text [rateLimitsArray]="rateLimitsArray" [disabled]="disabled"></tb-rate-limits-text>
  </div>
</section>
