<!--

    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-expansion-panel [formGroup]="serverFormGroup" #serverPanel>
  <mat-expansion-panel-header>
    <div class="flex flex-1 flex-row items-center justify-start">
      <mat-panel-title>
        <div class="flex flex-1 flex-row items-center justify-start">
          <div style="min-width:150px">{{ (serverFormGroup.get('bootstrapServerIs').value ?
            'device-profile.lwm2m.bootstrap-server' : 'device-profile.lwm2m.lwm2m-server') | translate }}</div>
          @if (!serverPanel.expanded) {
            <div style="font-size:14px" class="no-wrap flex flex-row">
              <div style="margin-left:32px">{{ ('device-profile.lwm2m.short-id' | translate) + ': ' }}
                <span style="font-style: italic">{{ serverFormGroup.get('shortServerId').value ? serverFormGroup.get('shortServerId').value : '' }}</span>
              </div>
              <div style="margin-left:32px">{{ ('device-profile.lwm2m.mode' | translate) + ': ' }}
                <span style="font-style: italic">{{ credentialTypeLwM2MNamesMap.get(securityConfigLwM2MType[serverFormGroup.get('securityMode').value]) }}</span>
              </div>
            </div>
          }
        </div>
      </mat-panel-title>
      <span class="flex-full"></span>
      @if (!disabled) {
        <button mat-icon-button style="min-width: 40px;"
                type="button"
                (click)="removeServer.emit($event)"
                matTooltip="{{ 'action.remove' | translate }}"
                matTooltipPosition="above">
          <mat-icon>delete</mat-icon>
        </button>
      }
    </div>
  </mat-expansion-panel-header>
  <ng-template matExpansionPanelContent>
    <section>
      <div class="flex flex-row gap-2 xs:flex-col xs:gap-0">
        <mat-form-field class="flex-1" appearance="outline">
          <mat-label>{{ 'device-profile.lwm2m.mode' | translate }}</mat-label>
          <mat-select formControlName="securityMode">
            @for (securityMode of securityConfigLwM2MTypes; track securityMode) {
              <mat-option [value]="securityMode">
                {{ credentialTypeLwM2MNamesMap.get(securityConfigLwM2MType[securityMode]) }}
              </mat-option>
            }
          </mat-select>
        </mat-form-field>
        @if (!isBootstrap) {
          <mat-form-field class="flex-1" appearance="outline">
            <mat-label>{{ 'device-profile.lwm2m.short-id' | translate }}</mat-label>
            @if (!disabled) {
              <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                        matTooltip="{{ (isBootstrap ? 'device-profile.lwm2m.short-id-tooltip-bootstrap' : 'device-profile.lwm2m.short-id-tooltip') | translate }}">help</mat-icon>
            }
            <input matInput type="number" formControlName="shortServerId" required
                   [min]="isBootstrap ? undefined : shortServerIdMin"
                   [max]="isBootstrap ? undefined : shortServerIdMax">
            @if (serverFormGroup.get('shortServerId').hasError('required')) {
              <mat-error>{{ 'device-profile.lwm2m.short-id-required' | translate }}</mat-error>
            }
            @if (serverFormGroup.get('shortServerId').hasError('pattern')) {
              <mat-error>{{ (isBootstrap ? 'device-profile.lwm2m.short-id-pattern-bs' : 'device-profile.lwm2m.short-id-pattern') | translate }}</mat-error>
            }
            @if (serverFormGroup.get('shortServerId').hasError('min') ||
            serverFormGroup.get('shortServerId').hasError('max')) {
              <mat-error>{{ 'device-profile.lwm2m.short-id-range' | translate : {min: shortServerIdMin, max: shortServerIdMax} }}</mat-error>
            }
          </mat-form-field>
        }
      </div>
      <div class="flex flex-row gap-2 xs:flex-col xs:gap-0">
        <mat-form-field class="flex-1" appearance="outline">
          <mat-label>{{ 'device-profile.lwm2m.server-host' | translate }}</mat-label>
          <input matInput type="text" formControlName="host" required>
          @if (serverFormGroup.get('host').hasError('required')) {
            <mat-error>{{ 'device-profile.lwm2m.server-host-required' | translate }}</mat-error>
          }
        </mat-form-field>
        <mat-form-field class="flex-1" appearance="outline">
          <mat-label>{{ 'device-profile.lwm2m.server-port' | translate }}</mat-label>
          <input matInput type="number" formControlName="port" required min="0" max="65535" readonly>
          @if (serverFormGroup.get('port').hasError('required')) {
            <mat-error>{{ 'device-profile.lwm2m.server-port-required' | translate }}</mat-error>
          }
          @if (serverFormGroup.get('port').hasError('pattern')) {
            <mat-error>{{ 'device-profile.lwm2m.server-port-pattern' | translate }}</mat-error>
          }
          @if (serverFormGroup.get('port').hasError('min') ||
          serverFormGroup.get('port').hasError('max')) {
            <mat-error>{{ 'device-profile.lwm2m.server-port-range' | translate }}</mat-error>
          }
        </mat-form-field>
      </div>
      <div class="flex flex-row gap-2 xs:flex-col xs:gap-0">
        <mat-form-field class="flex-1" appearance="outline">
          <mat-label>{{ 'device-profile.lwm2m.client-hold-off-time' | translate }}</mat-label>
          @if (!disabled) {
            <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                      matTooltip="{{ 'device-profile.lwm2m.client-hold-off-time-tooltip' | translate }}">help</mat-icon>
          }
          <input matInput type="number" formControlName="clientHoldOffTime" required min="0">
          @if (serverFormGroup.get('clientHoldOffTime').hasError('required')) {
            <mat-error>{{ 'device-profile.lwm2m.client-hold-off-time-required' | translate }}</mat-error>
          }
          @if (serverFormGroup.get('clientHoldOffTime').hasError('min') ||
          serverFormGroup.get('clientHoldOffTime').hasError('pattern')) {
            <mat-error>{{ 'device-profile.lwm2m.client-hold-off-time-pattern' | translate }}</mat-error>
          }
        </mat-form-field>
        <mat-form-field class="flex-1" appearance="outline">
          <mat-label>{{ 'device-profile.lwm2m.account-after-timeout' | translate }}</mat-label>
          @if (!disabled) {
            <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                      matTooltip="{{ 'device-profile.lwm2m.account-after-timeout-tooltip' | translate }}">help</mat-icon>
          }
          <input matInput type="number" formControlName="bootstrapServerAccountTimeout" required min="0">
          @if (serverFormGroup.get('bootstrapServerAccountTimeout').hasError('required')) {
            <mat-error>{{ 'device-profile.lwm2m.account-after-timeout-required' | translate }}</mat-error>
          }
          @if (serverFormGroup.get('bootstrapServerAccountTimeout').hasError('min') ||
          serverFormGroup.get('bootstrapServerAccountTimeout').hasError('pattern')) {
            <mat-error>{{ 'device-profile.lwm2m.account-after-timeout-pattern' | translate }}</mat-error>
          }
        </mat-form-field>
      </div>
      @if (serverFormGroup.get('securityMode').value === securityConfigLwM2MType.RPK ||
      serverFormGroup.get('securityMode').value === securityConfigLwM2MType.X509) {
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label>{{ 'device-profile.lwm2m.server-public-key' | translate }}</mat-label>
          @if (!disabled) {
            <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                      matTooltip="{{ publicKeyOrIdTooltipNamesMap.get(serverFormGroup.get('securityMode').value) | translate }}">help</mat-icon>
          }
          <textarea matInput
                    cdkTextareaAutosize
                    cdkAutosizeMinRows="1"
                    cols="1" required
                    formControlName="serverPublicKey">
          </textarea>
          @if (serverFormGroup.get('serverPublicKey').hasError('required')) {
            <mat-error>{{ 'device-profile.lwm2m.server-public-key-required' | translate }}</mat-error>
          }
        </mat-form-field>
      }
      <div class="flex flex-row gap-2 xs:flex-col xs:gap-0">
        <mat-form-field class="flex-1" appearance="outline">
          <mat-label>{{ 'device-profile.lwm2m.lifetime' | translate }}</mat-label>
          <input matInput type="number" min="0" formControlName="lifetime" required>
          @if (serverFormGroup.get('lifetime').hasError('required')) {
            <mat-error>{{ 'device-profile.lwm2m.lifetime-required' | translate }}</mat-error>
          }
          @if (serverFormGroup.get('lifetime').hasError('pattern') ||
          serverFormGroup.get('lifetime').hasError('min')) {
            <mat-error>{{ 'device-profile.lwm2m.lifetime-pattern' | translate }}</mat-error>
          }
        </mat-form-field>
        <mat-form-field class="flex-1" appearance="outline">
          <mat-label>{{ 'device-profile.lwm2m.default-min-period' | translate }}</mat-label>
          @if (!disabled) {
            <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                      matTooltip="{{ 'device-profile.lwm2m.default-min-period-tooltip' | translate }}">help</mat-icon>
          }
          <input matInput type="number" min="0" formControlName="defaultMinPeriod" required>
          @if (serverFormGroup.get('defaultMinPeriod').hasError('required')) {
            <mat-error>{{ 'device-profile.lwm2m.default-min-period-required' | translate }}</mat-error>
          }
          @if (serverFormGroup.get('defaultMinPeriod').hasError('pattern') ||
          serverFormGroup.get('defaultMinPeriod').hasError('min')) {
            <mat-error>{{ 'device-profile.lwm2m.default-min-period-pattern' | translate }}</mat-error>
          }
        </mat-form-field>
      </div>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label>{{ 'device-profile.lwm2m.binding' | translate }}</mat-label>
        @if (!disabled) {
          <mat-icon class="mat-primary" aria-hidden="false" aria-label="help-icon" matSuffix style="cursor:pointer;"
                    matTooltip="{{ 'device-profile.lwm2m.binding-tooltip' | translate }}">help</mat-icon>
        }
        <mat-select formControlName="binding">
          @for (bindingMode of bindingModeTypes; track bindingMode) {
            <mat-option [value]="bindingMode">
              {{ bindingModeTypeNamesMap.get(bindingMode) | translate }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
      <mat-checkbox formControlName="notifIfDisabled" color="primary">
        {{ 'device-profile.lwm2m.notification-storing' | translate }}
      </mat-checkbox>
    </section>
  </ng-template>
</mat-expansion-panel>
