<!--

    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.

-->
<mat-tab-group [formGroup]="lwm2mConfigFormGroup" dynamicHeight>
  <mat-tab label="{{ 'device.lwm2m-security-config.client-tab' | translate }}">
    <ng-container formGroupName="client">
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>device.lwm2m-security-config.endpoint</mat-label>
        <input matInput type="text" formControlName="endpoint" required>
        @if (lwm2mConfigFormGroup.get('client.endpoint').hasError('required')) {
          <mat-error>{{ 'device.lwm2m-security-config.endpoint-required' | translate }}</mat-error>
        }
      </mat-form-field>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>device.lwm2m-security-config.mode</mat-label>
        <mat-select formControlName="securityConfigClientMode">
          @for (securityConfigClientMode of securityConfigLwM2MTypes; track securityConfigClientMode) {
            <mat-option [value]="securityConfigClientMode">
              {{ credentialTypeLwM2MNamesMap.get(securityConfigLwM2MType[securityConfigClientMode]) }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
      @if (lwm2mConfigFormGroup.get('client.securityConfigClientMode').value === securityConfigLwM2MType.PSK) {
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label>{{ 'device.lwm2m-security-config.identity' | translate }}</mat-label>
          <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                    matTooltip="{{ 'device.lwm2m-security-config.identity-tooltip' | translate }}">help</mat-icon>
          <input matInput type="text" formControlName="identity" required>
          @if (lwm2mConfigFormGroup.get('client.identity').hasError('required')) {
            <mat-error>{{ 'device.lwm2m-security-config.identity-required' | translate }}</mat-error>
          }
        </mat-form-field>
      }
      @if (lwm2mConfigFormGroup.get('client.securityConfigClientMode').value === securityConfigLwM2MType.RPK ||
      lwm2mConfigFormGroup.get('client.securityConfigClientMode').value === securityConfigLwM2MType.PSK) {
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label>{{ 'device.lwm2m-security-config.client-key' | translate }}</mat-label>
          <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                    matTooltip="{{ clientKeyTooltipNamesMap.get(lwm2mConfigFormGroup.get('client.securityConfigClientMode').value) | translate }}">help</mat-icon>
          <textarea matInput
                    cdkTextareaAutosize
                    cdkAutosizeMinRows="1"
                    cols="1"
                    formControlName="key"
                    required>
          </textarea>
          @if (lwm2mConfigFormGroup.get('client.key').hasError('required')) {
            <mat-error>{{ 'device.lwm2m-security-config.client-key-required' | translate }}</mat-error>
          }
        </mat-form-field>
      }
      @if (lwm2mConfigFormGroup.get('client.securityConfigClientMode').value === securityConfigLwM2MType.X509) {
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>device.lwm2m-security-config.client-public-key</mat-label>
          @if (lwm2mConfigFormGroup.get('client.cert').value) {
            <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                      matTooltip="{{ 'device.lwm2m-security-config.client-public-key-tooltip' | translate }}">help</mat-icon>
          }
          <textarea matInput
                    cdkTextareaAutosize
                    cdkAutosizeMinRows="1"
                    cols="1"
                    formControlName="cert">
          </textarea>
          <mat-hint translate>device.lwm2m-security-config.client-public-key-hint</mat-hint>
        </mat-form-field>
      }
      @if (deviceId) {
        <button mat-raised-button color="primary" type="button"
                (click)="rebootDevice(false)"
                [disabled]="lwm2mConfigFormGroup.get('client').invalid">
          {{ 'device.lwm2m-security-config.client-reboot' | translate }}
        </button>
      }
    </ng-container>
  </mat-tab>
  <mat-tab label="{{ 'device.lwm2m-security-config.bootstrap-tab' | translate }}">
    <div style="padding: 2px;" formGroupName="bootstrap">
      <mat-accordion multi="true" class="mat-body-2">
        <mat-expansion-panel>
          <mat-expansion-panel-header>
            <mat-panel-title>
              {{ 'device.lwm2m-security-config.bootstrap-server' | translate }}
            </mat-panel-title>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            <tb-device-credentials-lwm2m-server
              formControlName="bootstrapServer">
            </tb-device-credentials-lwm2m-server>
          </ng-template>
        </mat-expansion-panel>
        <mat-expansion-panel>
          <mat-expansion-panel-header>
            <mat-panel-title>
              {{ 'device.lwm2m-security-config.lwm2m-server' | translate }}
            </mat-panel-title>
          </mat-expansion-panel-header>
          <ng-template matExpansionPanelContent>
            <tb-device-credentials-lwm2m-server
              formControlName="lwm2mServer">
            </tb-device-credentials-lwm2m-server>
          </ng-template>
        </mat-expansion-panel>
      </mat-accordion>
    </div>
    @if (deviceId) {
      <button mat-raised-button color="primary" style="margin-top: 22px" type="button"
              (click)="rebootDevice(true)"
              [disabled]="lwm2mConfigFormGroup.get('client').invalid">
        {{ 'device.lwm2m-security-config.bootstrap-reboot' | translate }}
      </button>
    }
  </mat-tab>
</mat-tab-group>
