import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { VirtualTimeScheduler } from 'rxjs';
import { ApiService } from './api.service';
import { Tab1Service } from '../tab1/tab1.service';
import { User } from '../interfaces/user';
import { UserLogin } from '../interfaces/user-login';
import { FeedService } from './feed.service';


@Injectable({
  providedIn: 'root'
})
export class LoginService {

  user: User;

  constructor(private apiService: ApiService, private router: Router, private tab1service: Tab1Service, private feedService: FeedService) {
    this.user = {
      id: 0,
      discotecaID: 0,
      userType: 0,
      username: '',
    };
  }

  validateUser(login: string, password: string): void{

    let userlogin: UserLogin = {
      loginUser: login,
      loginPassword: password,
    }

    this.apiService.validateUser(userlogin)
      .subscribe(user => {
        this.user = user[0];  
        if (this.user.userType==0)
          this.tab1service.getDiscoteca(this.user.discotecaID);
        else if (this.user.userType==1) 
          this.feedService.initFeed();

      })

  }


}
