import { Component, OnInit, OnDestroy } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Router } from '@angular/router';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import * as firebase from 'firebase/app';
import { AngularFireAuth } from 'angularfire2/auth';
import { Parrain } from '../../../models/Parrain';
import { Commercant } from '../../../models/Commercant';
import { AuthService } from '../../services/auth/auth.service';


@Component({
  moduleId: module.id,
  selector: 'navigationBar',
  templateUrl: './navigationBar.component.html',
  styleUrls: ['./navigationBar.component.scss'],
  providers: [AuthService]
})

export class NavigationBarComponent implements OnInit, OnDestroy{

  user: Observable<firebase.User>;
  parrain: Parrain;
  commercant: Commercant;
  profileData: FirebaseListObservable<any[]>;
  navElement1: string;
  navElement2: string;


  constructor(public afAuth: AngularFireAuth, public afDb: AngularFireDatabase,
              private router: Router, private authService: AuthService) {
  }



  updateNavBar() {
    this.navElement1 = 'Register';
    this.navElement2 = 'Login';
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        if (user.displayName === 'Parrain') {
          this.localUser = user;
          this.navElement1 = 'Profil Parrain';
          this.navElement2 = 'Logout Parrain';
        } else if (user.displayName === 'Commercant') {
          this.localUser = user;
          this.navElement1 = 'Profil Commercant';
          this.navElement2 = 'Logout Commercant';
        }
      } else {
        // No user is signed in.
        this.element1 = 'Register';
        this.element2 = 'login';
      }
    });

  }

  logout() {
    this.authService.logout();
    this.updateNavBar();
    this.router.navigate(['/']);
  }

  ngOnInit() {

  }

  ngOnDestroy() {
  }
}

