import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { IApiService } from '../../services/ApiService/ApiService';
import { NbToastrService } from '@nebular/theme';

@Component({
  selector: 'mylogin',
  templateUrl: './login.component.html',
})
export class MyLoginComponent implements OnInit{

  redirectDelay: number = 0;
  showMessages: any = {};
  strategy: string = '';

  errors: string[] = [];
  messages: string[] = [];
  user: any = {};
  submitted: boolean = false;
  socialLinks: any[] = [];
  rememberMe = false;

  constructor(
    private router : Router,  
    private api : IApiService,
    private toastr: NbToastrService,
    private route: ActivatedRoute
  ){}

  ngOnInit(){
    this.api.checkLogin().then(loggedin => {
      if(loggedin){
        this.router.navigateByUrl("/")
      }
    });
  }

  async login(){
    const login = await this.api.login(this.user.name, this.user.password)
    if(!login){
      this.toastr.danger("Login failed", "Error")
      return
    }else{
      this.route.queryParamMap.subscribe(params => {
        this.router.navigate([params.get('redirect')])        
      })
    }
  }
}