<!--

    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]="restApiCallConfigForm" class="flex flex-col gap-4">
  <div class="tb-form-panel stroked">
    <div class="tb-form-panel-title" translate>rule-node-config.request-url</div>
    <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
      <mat-label translate>rule-node-config.url</mat-label>
      <input required matInput formControlName="restEndpointUrlPattern">
      @if (restApiCallConfigForm.get('restEndpointUrlPattern').hasError('required')) {
      <mat-error>
        {{ 'rule-node-config.endpoint-url-pattern-required' | translate }}
      </mat-error>
    }
      <mat-hint translate>rule-node-config.url-hint</mat-hint>
    </mat-form-field>
    <mat-expansion-panel class="tb-settings tb-form-panel stroked no-gap" [expanded]="isQueryParamsExpanded">
      <mat-expansion-panel-header>
        {{ 'rule-node-config.query-params' | translate }}
        <div [tb-hint-tooltip-icon]="'rule-node-config.query-params-hint' | translate" tooltipPosition="above"></div>
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <tb-kv-list-config
          formControlName="queryParams"
          keyText="rule-node-config.query-params-name"
          keyRequiredText="rule-node-config.query-params-name-required"
          valText="rule-node-config.query-params-value"
          valRequiredText="rule-node-config.query-params-value-required"
          emptyText="rule-node-config.query-params-empty">
        </tb-kv-list-config>
      </ng-template>
    </mat-expansion-panel>
  </div>
  <mat-form-field class="mat-block" appearance="outline">
    <mat-label translate>rule-node-config.request-method</mat-label>
    <mat-select formControlName="requestMethod">
      @for (requestType of httpRequestTypes; track requestType) {
        <mat-option [value]="requestType">
          {{ requestType }}
        </mat-option>
      }
    </mat-select>
  </mat-form-field>
  <div class="tb-form-panel stroked">
    <mat-expansion-panel class="tb-settings" [expanded]="isHeadersExpanded">
      <mat-expansion-panel-header>
        {{ 'rule-node-config.headers' | translate }}
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <div class="tb-form-hint tb-primary-fill mb-4" [innerHTML]="'rule-node-config.headers-hint' | translate | safe: 'html'"></div>
        <tb-kv-map-config
          required="false"
          formControlName="headers"
          [keyText]="'rule-node-config.header' | translate"
          [keyRequiredText]="'rule-node-config.header-required' | translate"
          [valText]="'rule-node-config.value' | translate"
          [valRequiredText]="'rule-node-config.value-required' | translate"
          [emptyText]="'rule-node-config.headers-empty' | translate">
        </tb-kv-map-config>
      </ng-template>
    </mat-expansion-panel>
  </div>
  <tb-credentials-config formControlName="credentials"></tb-credentials-config>
  <div class="tb-form-panel stroked">
    <mat-expansion-panel class="tb-settings">
      <mat-expansion-panel-header>
        {{ 'rule-node-config.advanced-settings' | translate }}
      </mat-expansion-panel-header>
      <ng-template matExpansionPanelContent>
        <div class="tb-form-panel stroked column">
          <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="enableProxy">
            {{ 'rule-node-config.enable-proxy' | translate }}
          </mat-slide-toggle>
          @if (restApiCallConfigForm.get('enableProxy').value) {
            <div class="flex flex-col gap-4">
              <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="useSystemProxyProperties">
                {{ 'rule-node-config.use-system-proxy-properties' | translate }}
              </mat-slide-toggle>
              @if (!restApiCallConfigForm.get('useSystemProxyProperties').value) {
                <div>
                  <div class="gt-sm:flex gt-sm:flex-row gt-sm:gap-2">
                    <mat-form-field class="md-block gt-sm:max-w-50% gt-sm:flex-full" appearance="outline">
                      <mat-label translate>rule-node-config.proxy-host</mat-label>
                      <input matInput required formControlName="proxyHost">
                      @if (restApiCallConfigForm.get('proxyHost').hasError('required')) {
                        <mat-error>
                          {{ 'rule-node-config.proxy-host-required' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                    <mat-form-field class="mat-block gt-sm:max-w-50% gt-sm:flex-full" appearance="outline">
                      <mat-label translate>rule-node-config.proxy-port</mat-label>
                      <input matInput required formControlName="proxyPort" type="number" step="1">
                      @if (restApiCallConfigForm.get('proxyPort').hasError('required')) {
                        <mat-error>
                          {{ 'rule-node-config.proxy-port-required' | translate }}
                        </mat-error>
                      }
                      @if (restApiCallConfigForm.get('proxyPort').hasError('min') || restApiCallConfigForm.get('proxyPort').hasError('max')) {
                        <mat-error>
                          {{ 'rule-node-config.proxy-port-range' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                  </div>
                  <mat-form-field class="mat-block" appearance="outline">
                    <mat-label translate>rule-node-config.proxy-user</mat-label>
                    <input matInput formControlName="proxyUser">
                  </mat-form-field>
                  <mat-form-field class="mat-block" appearance="outline">
                    <mat-label translate>rule-node-config.proxy-password</mat-label>
                    <input matInput type="password" formControlName="proxyPassword">
                    <tb-toggle-password matSuffix></tb-toggle-password>
                  </mat-form-field>
                </div>
              }
            </div>
          }
        </div>
        <div class="tb-form-row">
          <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="parseToPlainText">
            <div [tb-hint-tooltip-icon]="'rule-node-config.parse-to-plain-text-hint' | translate" tooltipPosition="above">
              {{ 'rule-node-config.parse-to-plain-text' | translate }}
            </div>
          </mat-slide-toggle>
        </div>
        <div class="tb-form-panel stroked column">
          <mat-slide-toggle class="mat-slide fixed-title-width" formControlName="ignoreRequestBody">
            {{ 'rule-node-config.ignore-request-body' | translate }}
          </mat-slide-toggle>
          @if(!restApiCallConfigForm.get('ignoreRequestBody').value) {
            <div class="tb-form-hint tb-primary-fill mb-4" [innerHTML]="'rule-node-config.request-body-template-hint' | translate | safe: 'html'"></div>
            <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline" floatLabel="always">
              <mat-label translate>rule-node-config.request-body-template</mat-label>
              <textarea matInput
                        formControlName="requestBodyTemplate"
                        class="tb-enable-vertical-resize"
                        cdkTextareaAutosize
                        cdkAutosizeMinRows="3"></textarea>
            </mat-form-field>
          }
        </div>
        <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
          <mat-label translate>rule-node-config.read-timeout</mat-label>
          <input type="text" min="0" [max]="IntLimit" inputmode="numeric" pattern="[0-9]*" matInput formControlName="readTimeoutMs">
          <mat-hint translate>rule-node-config.read-timeout-hint</mat-hint>
          @if (restApiCallConfigForm.get('readTimeoutMs').hasError('max')) {
            <mat-error>
              {{ 'rule-node-config.int-range' | translate }}
            </mat-error>
          }
        </mat-form-field>
        <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
          <mat-label translate>rule-node-config.max-parallel-requests-count</mat-label>
          <input type="text" min="0" [max]="IntLimit" inputmode="numeric" pattern="[0-9]*" matInput formControlName="maxParallelRequestsCount">
          <mat-hint translate>rule-node-config.max-parallel-requests-count-hint</mat-hint>
          @if (restApiCallConfigForm.get('maxParallelRequestsCount').hasError('max')) {
            <mat-error>
              {{ 'rule-node-config.int-range' | translate }}
            </mat-error>
          }
        </mat-form-field>
        <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
          <mat-label translate>rule-node-config.max-response-size</mat-label>
          <input type="text" min="1" [max]="MemoryBufferSizeInKbLimit" inputmode="numeric" pattern="[0-9]*" matInput formControlName="maxInMemoryBufferSizeInKb">
          <mat-hint translate>rule-node-config.max-response-size-hint</mat-hint>
          @if (restApiCallConfigForm.get('maxInMemoryBufferSizeInKb').hasError('min') || restApiCallConfigForm.get('maxInMemoryBufferSizeInKb').hasError('max')) {
            <mat-error>
              {{ 'rule-node-config.memory-buffer-size-range' | translate: { max: MemoryBufferSizeInKbLimit } }}
            </mat-error>
          }
        </mat-form-field>
      </ng-template>
    </mat-expansion-panel>
  </div>
</section>
