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 { User } from '../../../models/User';
import { Parrain } from '../../../models/Parrain';
import { Commercant } from '../../../models/Commercant';
import { FireImg } from '../../../models/FireImg';

import { AuthService } from '../../services/auth/auth.service';

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

export class ProfilComponent implements OnInit, OnDestroy {

  user: Observable<firebase.User>;
  localUser: any;
  parrain: Parrain;
  commercant: Commercant;
  profileData: FirebaseListObservable<any[]>;
  imageProfil: string;
  role: any;


  constructor(public afAuth: AngularFireAuth, public afDb: AngularFireDatabase,
              private router: Router, private authService: AuthService) {
    this.user = afAuth.authState;
    this.localUser = new User();
    this.parrain = new Parrain();
    this.commercant = new Commercant();
    this.imageProfil = 'https://ssl.prcdn.com/uk/people/default-profile.png?1406639312';

    this.user.subscribe(
      (auth) => {
        if (auth) {
          this.profileData = this.afDb.list('/' + auth.displayName + '/', {
              query: {
                orderByChild: 'uid',
                equalTo: auth.uid.toString()
              }
            }
          );
          this.profileData.forEach(utilisateur => {
            utilisateur.forEach(element => {
              if (auth.displayName === 'Parrain') {

                this.localUser = new Parrain();
                this.localUser = element;
                this.parrain = element;
                this.imageProfil = this.parrain.image.downloadURL;
                this.role = 'Parrain';
              } else if (auth.displayName === 'Commercant') {
                this.localUser = new Commercant();
                this.localUser = element;
                this.commercant = element;
                this.imageProfil = this.commercant.image.downloadURL;
                this.role = 'Commercant';
              }
            });
          });
        }
      }
    );
  }

  logout() {
    firebase.auth().signOut();
    this.authService.logout();
    this.user = null;
    this.router.navigate(['/']);
  }

  goToOpCom(){
    this.router.navigate(['/opCommercial']);
  }
  goToCarte(){
    this.router.navigate(['/carteParrainageComponent']);
  }
  goToEditProfile() {
    this.router.navigate(['/editProfil']);
  }

  ngOnInit() {
    try {
      this.user.subscribe(
        (auth) => {
          if (auth) {
            console.log(auth.uid);
          } else {
            this.router.navigate(['/']);
          }
        });
    } catch (e) {
      // No content response..
      console.log(e);
      // this.router.navigate(['/']);
    }
  }

  ngOnDestroy() {
    this.localUser = {
      uid: '',
      image: new FireImg(),
      email: '',
      emailVerified: false,
      password: '',
      status: ''
    };
    this.parrain = {
      uid: '',
      image: new FireImg(),
      email: '',
      emailVerified: false,
      password: '',
      familyname: '',
      firstname: '',
      sex: '',
      birthday: new Date(),
      telephone: '',
      idCard: '',
      secretCode: '',
      status: ''
    };
    this.commercant = {
      uid : '',
      image: new FireImg(),
      raisonSociale : '',
      commercialName : '',
      siret : '',
      email : '',
      emailVerified : false,
      password : '',
      dateInscription : new Date(),
      status : ''
    };
  }

}


