<!--

    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 class="tb-form-panel no-gap no-border" [class.no-padding]="isAdd" [formGroup]="entityForm">
  <div class="tb-form-row tb-standard-fields no-border no-padding">
    <mat-form-field class="flex" appearance="outline">
      <mat-label translate>admin.oauth2.title</mat-label>
      <input matInput formControlName="title">
      @if (entityForm.get('title').hasError('required')) {
        <mat-error>
          {{ 'admin.oauth2.client-title-required' | translate }}
        </mat-error>
      }
      @if (entityForm.get('title').hasError('maxlength')) {
        <mat-error>
          {{ 'admin.oauth2.client-title-max-length' | translate }}
        </mat-error>
      }
    </mat-form-field>
  </div>
  <div class="tb-form-row tb-standard-fields no-border no-padding column-xs">
    <div class="tb-flex" formGroupName="additionalInfo">
      <mat-form-field class="flex" appearance="outline">
        <mat-label translate>admin.oauth2.provider</mat-label>
        <mat-select formControlName="providerName">
          @for (provider of templateProvider; track provider) {
            <mat-option [value]="provider">
              {{ provider }}
            </mat-option>
          }
        </mat-select>
      </mat-form-field>
    </div>
    <mat-form-field class="flex" floatLabel="always" appearance="outline">
      <mat-label translate>admin.oauth2.allowed-platforms</mat-label>
      <mat-select formControlName="platforms" multiple placeholder="{{ 'admin.oauth2.all-platforms' | translate }}">
        @for (platform of platformTypes; track platform) {
          <mat-option [value]="platform">
            {{ platformTypeTranslations.get(platform) | translate }}
          </mat-option>
        }
      </mat-select>
    </mat-form-field>
  </div>
  <div class="tb-form-row tb-standard-fields no-border no-padding column-xs">
    <mat-form-field class="flex" appearance="outline">
      <mat-label translate>admin.oauth2.client-id</mat-label>
      <input matInput formControlName="clientId">
      @if (entityForm.get('clientId').hasError('required')) {
        <mat-error>
          {{ 'admin.oauth2.client-id-required' | translate }}
        </mat-error>
      }
      @if (entityForm.get('clientId').hasError('maxlength')) {
        <mat-error>
          {{ 'admin.oauth2.client-id-max-length' | translate }}
        </mat-error>
      }
    </mat-form-field>
    <mat-form-field class="flex" appearance="outline">
      <mat-label translate>admin.oauth2.client-secret</mat-label>
      <input matInput formControlName="clientSecret">
      @if (entityForm.get('clientSecret').hasError('required')) {
        <mat-error>
          {{ 'admin.oauth2.client-secret-required' | translate }}
        </mat-error>
      }
      @if (entityForm.get('clientSecret').hasError('maxlength')) {
        <mat-error>
          {{ 'admin.oauth2.client-secret-max-length' | translate }}
        </mat-error>
      }
    </mat-form-field>
  </div>
  <mat-expansion-panel class="configuration-panel mat-elevation-z0" [expanded]="advancedExpanded">
    <mat-expansion-panel-header>
      <mat-panel-title class="flex items-center justify-start" translate>
        admin.oauth2.advanced-settings
      </mat-panel-title>
    </mat-expansion-panel-header>
    <ng-template matExpansionPanelContent>
      <section class="tb-form-panel no-border no-padding">
        <div class="tb-form-row column-xs no-border no-padding space-between">
          <span></span>
          <tb-toggle-select appearance="fill" [(ngModel)]="generalSettingsMode" [ngModelOptions]="{ standalone: true }">
            <tb-toggle-option [value]="true">{{ "admin.oauth2.general" | translate }}</tb-toggle-option>
            <tb-toggle-option [value]="false">{{ 'admin.oauth2.mapper' | translate }}</tb-toggle-option>
          </tb-toggle-select>
        </div>
        @if (generalSettingsMode) {
          <section class="tb-form-panel no-gap no-border no-padding" style="padding-bottom: 16px">
            <div class="tb-form-row tb-standard-fields no-border no-padding column-xs">
              <mat-form-field class="flex" appearance="outline">
                <mat-label translate>admin.oauth2.access-token-uri</mat-label>
                <input matInput formControlName="accessTokenUri">
                @if (entityForm.get('accessTokenUri').hasError('required')) {
                  <mat-error>
                    {{ 'admin.oauth2.access-token-uri-required' | translate }}
                  </mat-error>
                }
                @if (entityForm.get('accessTokenUri').hasError('pattern')) {
                  <mat-error>
                    {{ 'admin.oauth2.uri-pattern-error' | translate }}
                  </mat-error>
                }
              </mat-form-field>
              <mat-form-field class="flex" appearance="outline">
                <mat-label translate>admin.oauth2.authorization-uri</mat-label>
                <input matInput formControlName="authorizationUri" required>
                @if (entityForm.get('authorizationUri').hasError('required')) {
                  <mat-error>
                    {{ 'admin.oauth2.authorization-uri-required' | translate }}
                  </mat-error>
                }
                @if (entityForm.get('authorizationUri').hasError('pattern')) {
                  <mat-error>
                    {{ 'admin.oauth2.uri-pattern-error' | translate }}
                  </mat-error>
                }
              </mat-form-field>
            </div>
            <div class="tb-form-row tb-standard-fields no-border no-padding column-xs">
              <mat-form-field class="flex" appearance="outline">
                <mat-label translate>admin.oauth2.jwk-set-uri</mat-label>
                <input matInput formControlName="jwkSetUri">
                @if (entityForm.get('jwkSetUri').hasError('pattern')) {
                  <mat-error>
                    {{ 'admin.oauth2.uri-pattern-error' | translate }}
                  </mat-error>
                }
              </mat-form-field>
              <mat-form-field class="flex" appearance="outline">
                <mat-label translate>admin.oauth2.user-info-uri</mat-label>
                <input matInput formControlName="userInfoUri">
                @if (entityForm.get('userInfoUri').hasError('pattern')) {
                  <mat-error>
                    {{ 'admin.oauth2.uri-pattern-error' | translate }}
                  </mat-error>
                }
              </mat-form-field>
            </div>
            <div class="tb-form-row tb-standard-fields no-border no-padding">
              <mat-form-field class="flex" appearance="outline">
                <mat-label translate>admin.oauth2.client-authentication-method</mat-label>
                <mat-select formControlName="clientAuthenticationMethod">
                  @for (clientAuthenticationMethod of clientAuthenticationMethods; track clientAuthenticationMethod) {
                    <mat-option
                      [value]="clientAuthenticationMethod">
                      {{ clientAuthenticationMethod | uppercase }}
                    </mat-option>
                  }
                </mat-select>
              </mat-form-field>
            </div>
            @if (isCustomProvider()) {
              <div class="grid gt-xs:grid-cols-2 gt-xs:gap-4">
                <mat-form-field class="flex" floatLabel="always" appearance="outline">
                  <mat-label translate>admin.oauth2.login-button-label</mat-label>
                  <input matInput formControlName="loginButtonLabel"
                         placeholder="{{ 'admin.oauth2.login-button-label-placeholder' | translate }}">
                  @if (entityForm.get('loginButtonLabel').hasError('required')) {
                    <mat-error>
                      {{ 'admin.oauth2.login-button-label-required' | translate }}
                    </mat-error>
                  }
                </mat-form-field>
                <div class="tb-form-row justify-between" style="height: auto; margin-bottom: 22px;">
                  <div translate>admin.oauth2.login-button-icon</div>
                  <tb-material-icon-select asBoxInput
                                           iconClearButton
                                           allowedCustomIcon
                                           formControlName="loginButtonIcon">
                  </tb-material-icon-select>
                </div>
              </div>
            }
            <section formGroupName="mapperConfig" style="margin-bottom: 22px">
              <div class="tb-form-row no-border no-padding">
                <mat-slide-toggle class="mat-slide" formControlName="allowUserCreation">
                  {{ 'admin.oauth2.allow-user-creation' | translate }}
                </mat-slide-toggle>
              </div>
              <div class="tb-form-row no-border no-padding">
                <mat-slide-toggle class="mat-slide" formControlName="activateUser">
                  {{ 'admin.oauth2.activate-user' | translate }}
                </mat-slide-toggle>
              </div>
            </section>
            <tb-string-items-list
              formControlName="scope"
              appearance="outline"
              label="{{ 'admin.oauth2.scope' | translate }}"
              requiredText="{{ 'admin.oauth2.scope-required' | translate }}"
              required>
            </tb-string-items-list>
          </section>
        }
        @if (!generalSettingsMode) {
          <section class="tb-form-panel no-border no-padding no-gap">
            <div class="tb-form-row tb-standard-fields no-border no-padding">
              <mat-form-field class="flex" appearance="outline">
                <mat-label translate>admin.oauth2.user-name-attribute-name</mat-label>
                <input matInput formControlName="userNameAttributeName">
                @if (entityForm.get('userNameAttributeName').hasError('required')) {
                  <mat-error>
                    {{ 'admin.oauth2.user-name-attribute-name-required' | translate }}
                  </mat-error>
                }
              </mat-form-field>
            </div>
            <section class="tb-form-panel no-border no-padding no-gap" formGroupName="mapperConfig">
              <div class="tb-form-row tb-standard-fields no-border no-padding">
                <mat-form-field class="flex" appearance="outline">
                  <mat-label translate>admin.oauth2.type</mat-label>
                  <mat-select formControlName="type">
                    @for (mapperType of mapperTypes; track mapperType) {
                      <mat-option
                        [value]="mapperType">
                        {{ mapperType }}
                      </mat-option>
                    }
                  </mat-select>
                </mat-form-field>
              </div>
              @if (entityForm.get('mapperConfig.type').value !== mapperType.CUSTOM && entityForm.get('mapperConfig.basic')) {
                <section class="tb-form-panel no-border no-padding no-gap" formGroupName="basic">
                  @if (entityForm.get('mapperConfig.type').value !== mapperType.GITHUB) {
                    <div class="tb-form-row tb-standard-fields no-border no-padding">
                      <mat-form-field class="flex" appearance="outline">
                        <mat-label translate>admin.oauth2.email-attribute-key</mat-label>
                        <input matInput formControlName="emailAttributeKey">
                        @if (entityForm.get('mapperConfig.basic.emailAttributeKey').hasError('required')) {
                          <mat-error>
                            {{ 'admin.oauth2.email-attribute-key-required' | translate }}
                          </mat-error>
                        }
                        @if (entityForm.get('mapperConfig.basic.emailAttributeKey').hasError('maxlength')) {
                          <mat-error>
                            {{ 'admin.oauth2.email-attribute-key-max-length' | translate }}
                          </mat-error>
                        }
                      </mat-form-field>
                    </div>
                  }
                  <div class="tb-form-row tb-standard-fields no-border no-padding column-xs">
                    <mat-form-field class="flex" appearance="outline">
                      <mat-label translate>admin.oauth2.first-name-attribute-key</mat-label>
                      <input matInput formControlName="firstNameAttributeKey">
                      @if (entityForm.get('mapperConfig.basic.firstNameAttributeKey').hasError('maxlength')) {
                        <mat-error>
                          {{ 'admin.oauth2.first-name-attribute-key-max-length' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                    <mat-form-field class="flex" appearance="outline">
                      <mat-label translate>admin.oauth2.last-name-attribute-key</mat-label>
                      <input matInput formControlName="lastNameAttributeKey">
                      @if (entityForm.get('mapperConfig.basic.lastNameAttributeKey').hasError('maxlength')) {
                        <mat-error>
                          {{ 'admin.oauth2.last-name-attribute-key-max-length' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                  </div>
                  <div class="tb-form-row tb-standard-fields no-border no-padding column-xs">
                    <mat-form-field class="flex" appearance="outline">
                      <mat-label translate>admin.oauth2.tenant-name-strategy</mat-label>
                      <mat-select formControlName="tenantNameStrategy">
                        @for (tenantNameStrategy of tenantNameStrategies; track tenantNameStrategy) {
                          <mat-option
                            [value]="tenantNameStrategy">
                            {{ tenantNameStrategy }}
                          </mat-option>
                        }
                      </mat-select>
                    </mat-form-field>
                    <mat-form-field class="flex"
                                    appearance="outline"
                                    [class.!hidden]="entityForm.get('mapperConfig.basic.tenantNameStrategy').value !== 'CUSTOM'">
                      <mat-label translate>admin.oauth2.tenant-name-pattern</mat-label>
                      <input matInput formControlName="tenantNamePattern"
                             [required]="entityForm.get('mapperConfig.basic.tenantNameStrategy').value === 'CUSTOM'">
                      @if (entityForm.get('mapperConfig.basic.tenantNamePattern').hasError('required')) {
                        <mat-error>
                          {{ 'admin.oauth2.tenant-name-pattern-required' | translate }}
                        </mat-error>
                      }
                      @if (entityForm.get('mapperConfig.basic.tenantNamePattern').hasError('maxlength')) {
                        <mat-error>
                          {{ 'admin.oauth2.tenant-name-pattern-max-length' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                  </div>
                  <div class="tb-form-row tb-standard-fields no-border no-padding">
                    <mat-form-field class="flex" appearance="outline">
                      <mat-label translate>admin.oauth2.customer-name-pattern</mat-label>
                      <input matInput formControlName="customerNamePattern">
                      @if (entityForm.get('mapperConfig.basic.customerNamePattern').hasError('maxlength')) {
                        <mat-error>
                          {{ 'admin.oauth2.customer-name-pattern-max-length' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                  </div>
                  <div class="tb-form-row tb-standard-fields no-border no-padding column-xs">
                    <mat-form-field class="flex" appearance="outline">
                      <mat-label translate>admin.oauth2.default-dashboard-name</mat-label>
                      <input matInput formControlName="defaultDashboardName">
                      @if (entityForm.get('mapperConfig.basic.defaultDashboardName').hasError('maxlength')) {
                        <mat-error>
                          {{ 'admin.oauth2.default-dashboard-name-max-length' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                    <mat-slide-toggle class="tb-flex mat-slide" style="margin-bottom: 16px"
                                      formControlName="alwaysFullScreen">
                      {{ 'admin.oauth2.always-fullscreen' | translate}}
                    </mat-slide-toggle>
                  </div>
                </section>
              }
              @if (entityForm.get('mapperConfig.type').value === mapperType.CUSTOM && entityForm.get('mapperConfig.custom')) {
                <section class="tb-form-panel no-border no-padding no-gap" formGroupName="custom">
                  <div class="tb-form-row tb-standard-fields no-border no-padding">
                    <mat-form-field class="flex" appearance="outline">
                      <mat-label translate>admin.oauth2.url</mat-label>
                      <input matInput formControlName="url">
                      @if (entityForm.get('mapperConfig.custom.url').hasError('required')) {
                        <mat-error>
                          {{ 'admin.oauth2.url-required' | translate }}
                        </mat-error>
                      }
                      @if (entityForm.get('mapperConfig.custom.url').hasError('pattern')) {
                        <mat-error>
                          {{ 'admin.oauth2.url-pattern' | translate }}
                        </mat-error>
                      }
                      @if (entityForm.get('mapperConfig.custom.url').hasError('maxlength')) {
                        <mat-error>
                          {{ 'admin.oauth2.url-max-length' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                  </div>
                  <div class="tb-form-row tb-standard-fields no-border no-padding column-xs">
                    <mat-form-field class="flex" appearance="outline">
                      <mat-label translate>common.username</mat-label>
                      <input matInput formControlName="username" autocomplete="new-username">
                      @if (entityForm.get('mapperConfig.custom.username').hasError('maxlength')) {
                        <mat-error>
                          {{ 'admin.oauth2.username-max-length' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                    <mat-form-field class="flex" appearance="outline">
                      <mat-label translate>common.password</mat-label>
                      <input matInput formControlName="password" autocomplete="new-password">
                      @if (entityForm.get('mapperConfig.custom.password').hasError('maxlength')) {
                        <mat-error>
                          {{ 'admin.oauth2.password-max-length' | translate }}
                        </mat-error>
                      }
                    </mat-form-field>
                  </div>
                  <mat-slide-toggle class="tb-flex mat-slide" style="margin-bottom: 16px"
                                    formControlName="sendToken">
                    {{ 'admin.oauth2.send-token' | translate}}
                  </mat-slide-toggle>
                </section>
              }
            </section>
          </section>
        }
      </section>
    </ng-template>
  </mat-expansion-panel>
</section>
