import { Component, OnInit } from '@angular/core';
import { AngularFire, FirebaseAuthState } from "angularfire2";
import { UserService } from "../shared/user.service";
import { Router } from "@angular/router";

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css'],
  providers: [UserService]
})
export class LoginComponent implements OnInit {
  private _isLoggedIn: boolean;
  private currentAuthState: FirebaseAuthState | null;

  verifyingInfo: boolean;

  constructor(
      private angularFire: AngularFire,
      private userService: UserService,
      private router: Router) {
    this.angularFire.auth.subscribe(this.authObserver_.bind(this))
  }

  ngOnInit() { }

  get isLoggedIn() {
    return this._isLoggedIn;
  }

  login() {
    this.angularFire.auth.login();
  }

  authObserver_(firebaseAuthState) {
    this._isLoggedIn = firebaseAuthState != null;
    this.currentAuthState = firebaseAuthState;
    if (this._isLoggedIn) {
      this.verifyingInfo = true;
      this.userService.getById(this.currentAuthState.uid).then(
        (user) => {
          if (user) {
            return this.router.navigate(['/']);
          } else 

        }, (error) => {
          console.log(error);
          this.verifyingInfo = false;
        }
      ).then(() => {
        this.verifyingInfo = false;
      });
    }
  }

}

{
            return this.router.navigate(['/user/start']);
          }