import TVoxSDK from '@telenia/tvox-client-sdk';
import { Component, OnInit } from '@angular/core';
import {
  FormBuilder,
  FormGroup,
  FormsModule,
  ReactiveFormsModule,
} from '@angular/forms';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { CommonModule } from '@angular/common';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { Router, RouterModule } from '@angular/router';
import { TVoxService } from '../tvox-client-sdk.service';
import { AuthService } from '../auth.service';

@Component({
  selector: 'app-login',
  imports: [
    CommonModule,
    MatSelectModule,
    MatInputModule,
    MatFormFieldModule,
    MatButtonModule,
    MatCardModule,
    FormsModule,
    ReactiveFormsModule,
    MatProgressSpinnerModule,
    RouterModule,
  ],
  templateUrl: './login.component.html',
  styleUrl: './login.component.scss',
})
export class LoginComponent implements OnInit {
  loginForm: FormGroup;
  protected client!: TVoxSDK.Client;

  constructor(
    private fb: FormBuilder,
    private tvoxService: TVoxService,
    private router: Router,
    private authService: AuthService
  ) {
    this.loginForm = this.fb.group({
      loginType: 'classic',
      username: '',
      password: '',
      token: '',
    });
  }

  ngOnInit() {
    this.client = this.tvoxService.getClient();
  }

  login() {
    let loginType = this.loginForm.get('loginType')?.value;
    let username = this.loginForm.get('username')?.value;
    let password = this.loginForm.get('password')?.value;
    let token = this.loginForm.get('token')?.value;

    if (loginType == 'classic') {
      this.client
        .login({ username: username, password: password }) // metodo di login con username e password
        .then(() => {
          this.authService.login();
          this.router.navigate(['/dashboard']);
        })
        .catch((error) => {
          console.error(
            'Errore durante il login con username e password:',
            error
          );
        });
    } else if (loginType == 'token') {
      this.client
        .login({ externalSessionId: token }) // metodo di login con token
        .then(() => {
          this.authService.login();
          this.router.navigate(['/dashboard']);
        })
        .catch((error) => {
          console.error(
            'Errore durante il login con externalSessionId:',
            error
          );
        });
    } else {
      this.client
        .login({ jwt: token }) // metodo di login con token jwt
        .then(() => {
          // per ulteriori informazioni su questa modalità di login consultare la documentazione tecnica -> http://documentation.teleniasoftware.com/tvox_client_sdk/index.html#interface-jwtcredential
          this.authService.login();
          this.router.navigate(['/dashboard']);
        })
        .catch((error) => {
          console.error('Errore durante il login con token jwt:', error);
        });
    }
  }
}
