<!--

    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="tb-two-factor-auth-login-content mat-app-background tb-dark flex flex-row items-center justify-center"
     style="width: 100%;">
  <mat-card appearance="raised" class="tb-two-factor-auth-login-card flex-initial">
    <mat-card-header>
      <mat-card-title class="mat-headline-5 flex flex-row items-center justify-start">
        <button mat-icon-button type="button" (click)="cancelLogin()">
          <mat-icon>chevron_left</mat-icon>
        </button>
        {{ 'login.verify-your-identity' | translate }}
      </mat-card-title>
    </mat-card-header>
    <mat-card-content>
      @if (!selectedProvider) {
        <div class="providers-container tb-default flex flex-col gap-2">
          <p class="mat-body" translate>login.select-way-to-verify</p>
          @for (provider of allowProviders; track provider) {
            <button type="button" mat-stroked-button class="provider" (click)="selectProvider(provider)">
              <mat-icon class="tb-mat-18" svgIcon="{{ providersData.get(provider).icon }}"></mat-icon>
              {{ providersData.get(provider).name | translate }}
            </button>
          }
        </div>
      }
      @if (selectedProvider) {
        <form [formGroup]="verificationForm" (ngSubmit)="sendVerificationCode()">
          <fieldset [disabled]="isLoading$ | async">
            <div tb-toast class="flex flex-col">
              <p class="mat-body">{{ providerDescription }}</p>
              <div class="code-block flex flex-row gap-2">
                <mat-form-field class="mat-block tb-autofilled flex-1" appearance="outline">
                  <input matInput formControlName="verificationCode"
                         required [maxlength]="maxLengthInput" type="text"
                         [attr.inputmode]="inputMode" [pattern]="pattern"
                         autocomplete="one-time-code"
                         placeholder="{{ providersData.get(selectedProvider).placeholder | translate }}"/>
                  @if (verificationForm.get('verificationCode').getError('required') ||
                    verificationForm.get('verificationCode').getError('minlength') ||
                    verificationForm.get('verificationCode').getError('maxlength') ||
                    verificationForm.get('verificationCode').getError('pattern')) {
                    <mat-error>
                      {{ 'security.2fa.dialog.verification-code-invalid' | translate }}
                    </mat-error>
                  }
                  @if (verificationForm.get('verificationCode').getError('incorrectCode')) {
                    <mat-error>
                      {{ 'security.2fa.dialog.verification-code-incorrect' | translate }}
                    </mat-error>
                  }
                  @if (verificationForm.get('verificationCode').getError('tooManyRequest')) {
                    <mat-error>
                      {{ 'security.2fa.dialog.verification-code-many-request' | translate }}
                    </mat-error>
                  }
                </mat-form-field>
              </div>
              <span style="height: 50px;"></span>
              <button mat-flat-button
                      color="accent"
                      [disabled]="(isLoading$ | async) || verificationForm.invalid"
                      type="submit">
                {{ 'action.continue' | translate }}
              </button>
              <span style="height: 16px;"></span>
              <div class="action-row flex flex-row items-center justify-between gap-2">
                @if (showResendAction) {
                  <div class="action-resend flex flex-1 items-center justify-center">
                    @if (countDownTime) {
                      <div class="timer">
                        {{ 'login.resend-code-wait' | translate : {time: countDownTime} }}
                      </div>
                    }
                    <button mat-button class="flex-1"
                            [class.!hidden]="hideResendButton"
                            (click)="sendCode($event)"
                            type="button">
                      {{ 'login.resend-code' | translate }}
                    </button>
                  </div>
                }
                @if (allowProviders.length > 1) {
                  <button mat-button class="flex-1"
                          type="button"
                          (click)="selectProvider(null)">
                    {{ 'login.try-another-way' | translate }}
                  </button>
                }
              </div>
            </div>
          </fieldset>
        </form>
      }
    </mat-card-content>
  </mat-card>
</div>
