import { Component, OnInit } from '@angular/core';

import {MatSnackBar} from '@angular/material/snack-bar';
import { Router } from '@angular/router';

import { FormControl, Validators } from '@angular/forms';

import { InstructorData } from '../../../structures/instructor-data';

import { CredentialsInstructor } from '../../../structures/credentials-instructor';

import { LoginService } from '../../../services/login.service';

import * as Globals from '../../../globals';

@Component({
  selector: 'app-instructor-login-form',
  templateUrl: './login-form.component.html',
  styleUrls: ['./login-form.component.scss']
})
export class LoginFormComponent implements OnInit {

  model: CredentialsInstructor = { email: "", password: "" };

  hidePassword: boolean = true;

  constructor(private loginService: LoginService, private snackBar: MatSnackBar, private router: Router) { }

  ngOnInit(): void {
  }

  logIn(): void {
    this.loginService.logInInstructor({email: this.model.email, password: this.model.password}).subscribe(
      resp => {
        //this.afterLogIn(resp);
        if(resp.instructor?.ID != undefined && resp.instructor?.email != undefined) {
          this.snackBar.open('Přihlášení bylo úspěšné', 'x', {duration: Globals.snackbarSuccessTime, panelClass:'snackBarSuccess'}); 
          var afterLoginPage = Globals.getAfterLoginPage('instructor', resp.instructor.role);      
          this.router.navigate([afterLoginPage.path]);
        } else {
          this.snackBar.open('Přihlášení bylo neúspěšné', 'x', {duration: Globals.snackbarErrorTime, panelClass:'snackBarError'});
        }
      },
      error => this.snackBar.open('Přihlášení bylo neúspěšné', 'x', {duration: Globals.snackbarErrorTime, panelClass:'snackBarError'})
    );
  }

}
