import { Component, Inject, Input } from '@angular/core';
import { AuthenticationService } from '../../services/api/authentication.service';
import Swal from 'sweetalert2';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

@Component({
  standalone: true,
  imports: [CommonModule, FormsModule],
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrl: './login.component.css'
})
export class LoginComponent {

  username: string = '';
  password: string = '';

  waitRes: boolean = false;

  IsPwdHidden: boolean = true;

  constructor(private authService: AuthenticationService, @Inject(MAT_DIALOG_DATA) public data: any) {
    if (typeof (this.afterLogin) == typeof (data.afterLogin))
      this.afterLogin = data.afterLogin;
  }

  @Input() afterLogin: () => void = () => { }

  login(): void {
    this.waitRes = true;
    this.authService.login(this.username, this.password).subscribe({
      next: () => this.afterLogin(),
      error: (err) => {
        this.waitRes = false;
        Swal.fire({
          icon: "error",
          title: "Ungültiger Benutzername oder Passwort",
          text: "Bitte überprüfen Sie Ihre Anmeldedaten und versuchen Sie es erneut.",
        });
      },
      complete: () => this.waitRes = false
    })
  }
  onPasswordEyeClicked() {
    this.IsPwdHidden = !this.IsPwdHidden;
  }
}
