import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router } from '@angular/router';

import { Observable } from 'rxjs/Observable';

import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';

import * as firebase from 'firebase/app';
import { AngularFireAuth } from 'angularfire2/auth';
import { AuthService } from '../../../services/auth/auth.service';
import { OpCommercialService } from '../../../services/opCommercial/op-commercial.service';
import { Parrain } from '../../../../models/Parrain';
import { Commercant } from '../../../../models/Commercant';
import { CarteParrainage } from '../../../../models/CarteParrainage';

@Component({
  selector: 'app-carte-parrainage',
  templateUrl: './carte-parrainage.component.html',
  styleUrls: ['./carte-parrainage.component.scss'],
  providers: [AuthService, OpCommercialService]
})
export class CarteParrainageComponent implements OnInit, OnDestroy {

  user: Observable<firebase.User>;
  parrain: Parrain;
  commercant: Commercant;
  carteParr: CarteParrainage;
  carteParrData: FirebaseListObservable<any[]>;
  profileData: FirebaseListObservable<any[]>;
  commList: FirebaseListObservable<any[]>;
  databaseRef: any;
  statutAffiche: string;
  hideme = {};

  constructor(public afAuth: AngularFireAuth, public afDb: AngularFireDatabase,
              private router: Router) {
    this.hideme = {}; // init is required
    this.carteParr = new CarteParrainage();
    this.databaseRef = firebase.database().ref();
    this.user = afAuth.authState;
    this.statutAffiche = 'Toutes';

    this.parrain = new Parrain();
    this.user.subscribe(
      (auth) => {
        if (auth) {

          this.carteParrData = this.afDb.list('/CarteParrainage/', {
              query: {
                orderByChild: 'parrainCarte/uid',
                equalTo: auth.uid.toString()
              }
            }
          ).map((array) => array.reverse()) as FirebaseListObservable<any[]>;;
          this.carteParrData.forEach(car => {
            car.forEach(elementCar => {
              //this.updateCarteStatus(elementCar);
              this.commList = this.afDb.list('/Commercant/', {
                  query: {
                    orderByChild: 'uid',
                    equalTo: elementCar.commercantCarte.uid
                  }
                }
              );

              this.commList.forEach(commerc => {
                commerc.forEach(elementCommerc => {
                  this.commercant = elementCommerc;
                });
              });
            });
          });

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


        }
      }
    );
  }

  activeCarte(afficheStatus: string){
    this.statutAffiche = afficheStatus;
  }

  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() {}
}
