<!--

    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.

-->
<section [formGroup]="calculateDeltaConfigForm">
  <div class="gt-sm:flex gt-sm:flex-row gt-sm:gap-5.5">
    <mat-form-field class="mat-block flex-1" appearance="outline">
      <mat-label>{{ 'rule-node-config.input-value-key' | translate }}</mat-label>
      <input matInput formControlName="inputValueKey">
      @if (calculateDeltaConfigForm.get('inputValueKey').hasError('required') ||
        calculateDeltaConfigForm.get('inputValueKey').hasError('pattern')) {
        <mat-error>
          {{ 'rule-node-config.input-value-key-required' | translate }}
        </mat-error>
      }
    </mat-form-field>
    <mat-form-field class="mat-block flex-1" appearance="outline">
      <mat-label>{{ 'rule-node-config.output-value-key' | translate }}</mat-label>
      <input matInput formControlName="outputValueKey">
      @if (calculateDeltaConfigForm.get('outputValueKey').hasError('required') ||
        calculateDeltaConfigForm.get('outputValueKey').hasError('pattern')) {
        <mat-error>
          {{ 'rule-node-config.output-value-key-required' | translate }}
        </mat-error>
      }
    </mat-form-field>
  </div>
  <mat-form-field class="mat-block flex-1" appearance="outline">
    <mat-label>{{ 'rule-node-config.number-of-digits-after-floating-point' | translate }}</mat-label>
    <input type="number" min="0" max="15" step="1" matInput formControlName="round">
    @if (calculateDeltaConfigForm.get('round').hasError('min')) {
      <mat-error>
        {{ 'rule-node-config.number-of-digits-after-floating-point-range' | translate }}
      </mat-error>
    }
    @if (calculateDeltaConfigForm.get('round').hasError('max')) {
      <mat-error>
        {{ 'rule-node-config.number-of-digits-after-floating-point-range' | translate }}
      </mat-error>
    }
  </mat-form-field>
  <div class="tb-form-panel no-padding no-border">
    <div tb-hint-tooltip-icon="{{ 'rule-node-config.failure-if-delta-negative-tooltip' | translate }}"
         class="tb-form-row same-padding">
      <mat-slide-toggle class="mat-slide margin" formControlName="tellFailureIfDeltaIsNegative">
        {{ 'rule-node-config.failure-if-delta-negative' | translate }}
      </mat-slide-toggle>
    </div>
    <div tb-hint-tooltip-icon="{{ 'rule-node-config.use-caching-tooltip' | translate :
                       { inputValueKey: calculateDeltaConfigForm.get('inputValueKey').valid ?
                                calculateDeltaConfigForm.get('inputValueKey').value : 'rule-node-config.input-value-key' | translate } }}"
      class="tb-form-row same-padding">
      <mat-slide-toggle class="mat-slide margin" formControlName="useCache">
        {{ 'rule-node-config.use-caching' | translate }}
      </mat-slide-toggle>
    </div>
    <div class="tb-form-panel stroked" [class.no-padding-bottom]="calculateDeltaConfigForm.get('addPeriodBetweenMsgs').value">
      <div tb-hint-tooltip-icon="{{ 'rule-node-config.add-time-difference-between-readings-tooltip' | translate:
                           { periodValueKey: (calculateDeltaConfigForm.get('periodValueKey').valid &&
                           calculateDeltaConfigForm.get('addPeriodBetweenMsgs').value) ? calculateDeltaConfigForm.get('periodValueKey').value :
                           'periodInMs' } }}"
        class="tb-form-row no-border no-padding">
        <mat-slide-toggle class="mat-slide" formControlName="addPeriodBetweenMsgs">
          {{ 'rule-node-config.add-time-difference-between-readings' | translate:
          { inputValueKey: calculateDeltaConfigForm.get('inputValueKey').valid ?
          calculateDeltaConfigForm.get('inputValueKey').value : 'rule-node-config.input-value-key' | translate } }}
        </mat-slide-toggle>
      </div>
      @if (calculateDeltaConfigForm.get('addPeriodBetweenMsgs').value) {
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label>{{ 'rule-node-config.period-value-key' | translate }}</mat-label>
          <input required matInput formControlName="periodValueKey">
          @if (calculateDeltaConfigForm.get('periodValueKey').hasError('required')) {
            <mat-error>
              {{ 'rule-node-config.period-value-key-required' | translate }}
            </mat-error>
          }
        </mat-form-field>
      }
    </div>
    <div tb-hint-tooltip-icon="{{ calculateDeltaConfigForm.get('addPeriodBetweenMsgs').value ? ('rule-node-config.exclude-zero-deltas-time-difference-hint'  | translate :{
            outputValueKey: calculateDeltaConfigForm.get('outputValueKey').valid ? calculateDeltaConfigForm.get('outputValueKey').value : 'rule-node-config.output-value-key' | translate,
            periodValueKey: calculateDeltaConfigForm.get('periodValueKey').valid ? calculateDeltaConfigForm.get('periodValueKey').value : 'rule-node-config.period-value-key' | translate
            }) : ('rule-node-config.exclude-zero-deltas-hint' | translate :{ outputValueKey: calculateDeltaConfigForm.get('outputValueKey').valid ? calculateDeltaConfigForm.get('outputValueKey').value : 'rule-node-config.output-value-key' | translate }) }}"
      class="tb-form-row same-padding">
      <mat-slide-toggle class="mat-slide margin" formControlName="excludeZeroDeltas">
        {{ 'rule-node-config.exclude-zero-deltas' | translate }}
      </mat-slide-toggle>
    </div>
  </div>
</section>
