<!--

    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.

-->
<div class="profile-container flex flex-col gap-2">
  <mat-card appearance="outlined" class="profile-card flex flex-col">
    <mat-card-title style="margin-bottom: 8px;">
      <span class="mat-headline-5 card-title" translate>profile.jwt-token</span>
    </mat-card-title>
    <div class="flex flex-row items-center justify-between">
      <div class="token-text">{{ 'profile.token-valid-till' | translate }} <span class="date">{{ jwtTokenExpiration | date: 'yyyy-MM-dd HH:mm:ss' }}</span></div>
      <button mat-raised-button
              color="primary"
              type="button"
              (click)="copyToken()">
        <span>{{ 'profile.copy-jwt-token' | translate }}</span>
      </button>
    </div>
  </mat-card>
  <mat-card appearance="outlined" class="profile-card flex flex-col">
    <div class="flex flex-row items-center justify-between">
      <mat-card-title>
        <span class="mat-headline-5 card-title" translate>api-key.api-keys</span>
      </mat-card-title>
      <button mat-raised-button
              color="primary"
              type="button"
              (click)="openApiKeysTable()">
        <mat-icon>key</mat-icon>
        <span>{{ 'api-key.manage' | translate }}</span>
      </button>
    </div>
  </mat-card>
  <mat-card appearance="outlined" class="profile-card flex flex-col">
    <div class="change-password" tb-toast toastTarget="changePassword">
      <form #changePasswordForm="ngForm" [formGroup]="changePassword" (ngSubmit)="onChangePassword(changePasswordForm)">
        <div class="flex flex-row gap-6.25 xs:flex-col xs:gap-0">
          <div class="min-w-72.5 max-w-72.5 flex-1 basis-72.5 xs:min-w-0 xs:max-w-full xs:basis-full sm:min-w-62.5 sm:max-w-62.5 sm:basis-62.5">
            <h3 class="card-title" translate>profile.change-password</h3>
            <mat-form-field class="mat-block same-color" appearance="outline" color="primary">
              <mat-label translate>profile.current-password</mat-label>
              <input matInput type="password" name="current-password" formControlName="currentPassword" autocomplete="current-password"/>
              <tb-toggle-password [class.!hidden]="!changePassword.get('currentPassword').dirty && !changePassword.get('currentPassword').touched" matSuffix></tb-toggle-password>
              @if (changePassword.get('currentPassword').hasError('differencePassword')) {
                <mat-error>
                  {{ 'security.password-requirement.incorrect-password-try-again' | translate }}
                </mat-error>
              }
            </mat-form-field>
            <mat-form-field class="mat-block same-color" hideRequiredMarker appearance="outline" color="primary" subscriptSizing="dynamic">
              <mat-label translate>login.new-password</mat-label>
              <input matInput type="password" name="new-password" formControlName="newPassword" autocomplete="new-password" required/>
              <tb-toggle-password [class.!hidden]="!changePassword.get('newPassword').dirty && !changePassword.get('newPassword').touched" matSuffix></tb-toggle-password>
              <mat-hint></mat-hint>
              @if (changePassword.get('newPassword').errors
                && !changePassword.get('newPassword').hasError('alreadyUsed')
                && !changePassword.get('newPassword').hasError('hasWhitespaces')
                && !changePassword.get('newPassword').hasError('passwordSameAsOld')) {
                <mat-error>
                  {{ 'security.password-requirement.password-not-meet-requirements' | translate }}
                </mat-error>
              }
              @if (changePassword.get('newPassword').hasError('alreadyUsed')) {
                <mat-error>
                  {{ changePassword.get('newPassword').getError('alreadyUsed') }}
                </mat-error>
              }
              @if (changePassword.get('newPassword').hasError('passwordSameAsOld')) {
                <mat-error>
                  {{ 'security.password-requirement.password-should-difference' | translate }}
                </mat-error>
              }
              @if (changePassword.get('newPassword').hasError('hasWhitespaces')) {
                <mat-error>
                  {{ 'security.password-requirement.password-should-not-contain-spaces' | translate }}
                </mat-error>
              }
            </mat-form-field>
            <div class="flex flex-1 items-center justify-start gt-xs:!hidden">
              <ng-container *ngTemplateOutlet="passwordRequirements"></ng-container>
            </div>
            <mat-form-field class="mat-block same-color" hideRequiredMarker appearance="outline" color="primary">
              <mat-label translate>login.new-password-again</mat-label>
              <input matInput type="password" name="new-password" formControlName="newPassword2" autocomplete="new-password" required/>
              <tb-toggle-password [class.!hidden]="!changePassword.get('newPassword2').dirty && !changePassword.get('newPassword2').touched" matSuffix></tb-toggle-password>
              @if (changePassword.get('newPassword2').hasError('passwordsNotMatch')) {
                <mat-error>
                  {{ 'security.password-requirement.new-passwords-not-match' | translate }}
                </mat-error>
              }
            </mat-form-field>
          </div>
          <mat-divider [vertical]="true"></mat-divider>
          <div class="flex flex-1 items-start justify-start xs:!hidden">
            <ng-container *ngTemplateOutlet="passwordRequirements"></ng-container>
          </div>
        </div>
        <ng-template #passwordRequirements>
          @if (passwordPolicy) {
            <div class="password-requirements">
              <h3 class="card-title" translate>security.password-requirement.password-requirements</h3>
              <h4 class="mat-h4" translate>security.password-requirement.at-least</h4>
              @if (passwordPolicy.minimumUppercaseLetters > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('notUpperCase') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.uppercase-letter' | translate : {count: passwordPolicy.minimumUppercaseLetters} }}
                </p>
              }
              @if (passwordPolicy.minimumLowercaseLetters > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('notLowerCase') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.lowercase-letter' | translate : {count: passwordPolicy.minimumLowercaseLetters} }}
                </p>
              }
              @if (passwordPolicy.minimumDigits > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('notNumeric') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.digit' | translate : {count: passwordPolicy.minimumDigits} }}
                </p>
              }
              @if (passwordPolicy.minimumSpecialCharacters > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('notSpecial') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.special-character' | translate : {count: passwordPolicy.minimumSpecialCharacters} }}
                </p>
              }
              @if (passwordPolicy.minimumLength > 0) {
                <p class="mat-body">
                  <tb-icon class="tb-mat-20">
                    {{ changePassword.get('newPassword').hasError('minLength') ? 'mdi:circle-small' : 'mdi:check' }}
                  </tb-icon>
                  {{ 'security.password-requirement.character' | translate : {count: passwordPolicy.minimumLength} }}
                </p>
              }
              @if (passwordPolicy.maximumLength > 0) {
                <div class="password-requirements">
                  <h4 class="mat-h4" translate>security.password-requirement.at-most</h4>
                  <p class="mat-body">
                    <mat-icon class="tb-mat-20" [svgIcon]="changePassword.get('newPassword').hasError('maxLength') ? 'mdi:circle-small' : 'mdi:check'"></mat-icon>
                    {{ 'security.password-requirement.character' | translate : {count: passwordPolicy.maximumLength} }}
                  </p>
                </div>
              }
            </div>
          }
        </ng-template>
        <div class="flex flex-row gap-2" style="margin-top: 18px;" [class.!hidden]="!changePassword.dirty && !changePassword.touched">
          <button mat-button color="primary"
                  type="button"
                  (click)="discardChanges(changePasswordForm, $event)"
                  [disabled]="(isLoading$ | async)">
            {{ 'action.discard-changes' | translate }}
          </button>
          <button mat-raised-button color="primary"
                  type="submit"
                  [disabled]="(isLoading$ | async) || (!changePassword.valid && changePassword.touched)">
            {{ 'profile.change-password' | translate }}
          </button>
        </div>
      </form>
    </div>
  </mat-card>
  @if (allowTwoFactorProviders.length) {
    <mat-card appearance="outlined" class="profile-card">
      <mat-card-title style="margin-bottom: 20px;">
        <span class="mat-headline-5 card-title" translate>admin.2fa.2fa</span>
      </mat-card-title>
      <mat-card-subtitle style="margin-bottom: 40px;">
        <div class="mat-body-2 description" translate>security.2fa.2fa-description</div>
      </mat-card-subtitle>
      <h3 class="mat-h3 auth-title" translate>security.2fa.authenticate-with</h3>
      <form [formGroup]="twoFactorAuth">
        @for (provider of allowTwoFactorProviders; track trackByProvider($index, provider)) {
          <div class="provider">
            <h4 class="provider-title">{{ providersData.get(provider).name | translate }}</h4>
            <div class="flex flex-row items-start justify-between">
              @if (!twoFactorAuth.get(provider).value) {
                <div class="mat-body-2 description">
                  {{ providersData.get(provider).description | translate }}
                </div>
              } @else {
                <div class="mat-body-2 description">
                  {{ providersData.get(provider).activatedHint | translate: providerDataInfo(provider) }}
                </div>
              }
              <mat-slide-toggle [formControlName]="provider"
                                (mousedown)="confirm2FAChange($event, provider)">
              </mat-slide-toggle>
            </div>
            @if (twoFactorAuth.get(provider).value && provider !== twoFactorAuthProviderType.BACKUP_CODE && !activeSingleProvider) {
              <mat-checkbox [value]="provider"
                            [checked]="useByDefault === provider"
                            (change)="changeDefaultProvider($event, provider)"
                            [disabled]="(isLoading$ | async)"
                            >
                <span class="checkbox-label" translate>security.2fa.main-2fa-method</span>
              </mat-checkbox>
            }
            @if (twoFactorAuth.get(provider).value && provider === twoFactorAuthProviderType.BACKUP_CODE) {
              <button type="button"
                      mat-stroked-button color="primary"
                      (click)="generateNewBackupCode()"
                      >
                {{ 'security.2fa.get-new-code' | translate }}
              </button>
            }
          </div>
          @if (!$last) {
            <mat-divider></mat-divider>
          }
        }
      </form>
    </mat-card>
  }
</div>
