import { Component, ViewChild } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar, Splashscreen, GoogleAnalytics } from 'ionic-native';

import { Login } from '../pages/login/login';
import { Home } from '../pages/home/home';
import { CursosListar } from '../pages/cursos/listar';

import { Http, Headers, RequestOptions } from '@angular/http';
import { Storage } from '@ionic/storage';
import { Lipigas } from '../lipigas';
import * as global from '../global';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';
import { of } from 'rxjs/observable/of';
interface Window { APP: any; }
declare var FCMPlugin: any;


@Component({
  templateUrl: 'app.html',
  providers: [Lipigas]
})
export class LipigasPersonas {
  @ViewChild('#content') nav;
  public rootPage: any = null;
 
  constructor(public platform: Platform, private storage: Storage, private http: Http, private service: Lipigas) {
    this.initializeApp();
  }


  initializeApp() {

    this.platform.ready().then(() => {
      StatusBar.styleDefault();
      Splashscreen.hide();

      this.service.sync('native');

      if(typeof(FCMPlugin) !== "undefined"){
        FCMPlugin.getToken(function(t){
          console.log("Token: " + t);
          localStorage.setItem( "LipiPush" , t );
        }, function(e){
          console.log("Uh-Oh!\n"+e);
        });

        FCMPlugin.onNotification(function(d){
          if(d.wasTapped){  
            if (d.tipo == 'invitacion_curso') {
              window['APP'].homeController.publicFunc();
            }
          } else {
            
          }
        }, function(msg){
          console.log("Ok... " + msg);
        }, function(err){
          console.log("Err... " + err);
        });
      } else console.log("Notifications disabled, only provided in Android/iOS environment");

      /* google
      
      return GoogleAnalytics.startTrackerWithId("UA-92519180-1")
        .then(() => {
          console.log('Google analytics is ready now');
          return GoogleAnalytics.enableUncaughtExceptionReporting(true)
        }).then((_success) => {
          console.log("startTrackerWithId success")
        }).catch((_error) => {
          console.log("enableUncaughtExceptionReporting", _error)
        });
      
      */
    });

    if (localStorage.getItem("LipigasPersonas")) {
      this.rootPage = Home;
    }
    else {
      this.rootPage = Login; 
    }
  }

  openPage(page) {
    this.nav.setRoot(page.component);
  }

}
