<!--

    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]="credentialsConfigFormGroup" class="tb-form-panel stroked">
  <mat-expansion-panel class="tb-credentials-config-panel-group tb-settings" expanded="true">
    <mat-expansion-panel-header>
      <mat-panel-title translate>rule-node-config.credentials</mat-panel-title>
      <mat-panel-description>
        {{ credentialsTypeTranslationsMap.get(credentialsConfigFormGroup.get('type').value) | translate }}
      </mat-panel-description>
    </mat-expansion-panel-header>
    <ng-template matExpansionPanelContent>
      <mat-form-field class="mat-block" subscriptSizing="dynamic" appearance="outline">
        <mat-label translate>rule-node-config.credentials-type</mat-label>
        <mat-select formControlName="type" required>
          @for (credentialsType of allCredentialsTypes; track credentialsType) {
            <mat-option [value]="credentialsType"
                        [disabled]="credentialsType === 'cert.PEM' && disableCertPemCredentials">
              {{ credentialsTypeTranslationsMap.get(credentialsType) | translate }}
            </mat-option>
          }
        </mat-select>
        @if (credentialsConfigFormGroup.get('type').hasError('required')) {
          <mat-error>
            {{ 'rule-node-config.credentials-type-required' | translate }}
          </mat-error>
        }
      </mat-form-field>
      @if (credentialsConfigFormGroup.get('type').value !== 'anonymous') {
        <section class="flex flex-col">
        @switch (credentialsConfigFormGroup.get('type').value) {
            @case("basic") {
              <mat-form-field class="mat-block" appearance="outline">
                <mat-label translate>rule-node-config.username</mat-label>
                <input required matInput formControlName="username">
                @if (credentialsConfigFormGroup.get('username').hasError('required')) {
                  <mat-error>
                    {{ 'rule-node-config.username-required' | translate }}
                  </mat-error>
                }
              </mat-form-field>
              <mat-form-field class="mat-block" appearance="outline">
                <mat-label translate>rule-node-config.password</mat-label>
                <input type="password" [required]="passwordFieldRequired" matInput formControlName="password" autocomplete="new-password">
                <tb-toggle-password matSuffix></tb-toggle-password>
                @if (credentialsConfigFormGroup.get('password').hasError('required')) {
                  <mat-error>
                    {{ 'rule-node-config.password-required' | translate }}
                  </mat-error>
                }
              </mat-form-field>
            }
          @case("cert.PEM") {
            <div class="tb-form-hint tb-primary-fill mb-4">{{ 'rule-node-config.credentials-pem-hint' | translate }}</div>
            <tb-file-input formControlName="caCert"
                           inputId="caCertSelect"
                           [existingFileName]="credentialsConfigFormGroup.get('caCertFileName').value"
                           (fileNameChanged)="credentialsConfigFormGroup.get('caCertFileName').setValue($event)"
                           label="{{'rule-node-config.ca-cert' | translate}}"
                           noFileText="rule-node-config.no-file"
                           dropLabel="{{'rule-node-config.drop-file' | translate}}">
            </tb-file-input>
            <tb-file-input formControlName="cert"
                           inputId="CertSelect"
                           [existingFileName]="credentialsConfigFormGroup.get('certFileName').value"
                           (fileNameChanged)="credentialsConfigFormGroup.get('certFileName').setValue($event)"
                           label="{{'rule-node-config.cert' | translate}}"
                           noFileText="rule-node-config.no-file"
                           dropLabel="{{'rule-node-config.drop-file' | translate}}">
            </tb-file-input>
            <tb-file-input style="padding-bottom: 8px;"
                           formControlName="privateKey"
                           inputId="privateKeySelect"
                           [existingFileName]="credentialsConfigFormGroup.get('privateKeyFileName').value"
                           (fileNameChanged)="credentialsConfigFormGroup.get('privateKeyFileName').setValue($event)"
                           label="{{'rule-node-config.private-key' | translate}}"
                           noFileText="rule-node-config.no-file"
                           dropLabel="{{'rule-node-config.drop-file' | translate}}">
            </tb-file-input>
            <mat-form-field class="mat-block">
              <mat-label translate>rule-node-config.private-key-password</mat-label>
              <input type="password" matInput formControlName="password" autocomplete="new-password">
              <tb-toggle-password matSuffix></tb-toggle-password>
            </mat-form-field>
          }
        }
        </section>
        }
    </ng-template>
  </mat-expansion-panel>
</section>
