import { Component, ViewChild } from '@angular/core';
import { Platform, MenuController, Nav, App, ToastController } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { WalkthroughPage } from '../pages/walkthrough/walkthrough';
import { FirebaseLoginPage } from '../pages/firebase-integration/firebase-login/firebase-login';
import { FirebaseService } from '../../src/pages/firebase-integration/firebase-integration.service';
import { FirebaseAuthService } from '../../src/pages/firebase-integration/firebase-auth.service';
import { Storage } from '@ionic/storage';
import { TabsNavigationPage } from '../pages/tabs-navigation/tabs-navigation';
import { FcmProvider } from '../providers/fcm/fcm';
import { tap } from 'rxjs/operators';
import { Badge } from '@ionic-native/badge/ngx';
import * as firebase from 'firebase/app';

@Component({
  selector: 'app-root',
  templateUrl: 'app.html'
})

export class MyApp {

  @ViewChild(Nav) nav: Nav;

  // make WalkthroughPage the root (or first) page  
  //rootPage: any = WalkthroughPage;
  rootPage: any;
  textDir: string = "ltr";

  pushPages: Array<{title: any, icon: string, component: any}>;
  pages: Array<{title: any, icon: string, component: any}>;

  constructor(
    platform: Platform,
    public menu: MenuController,
    public app: App,
    public splashScreen: SplashScreen,
    public statusBar: StatusBar,
    public toastCtrl: ToastController,
    public firebaseService: FirebaseService,
    public fAuthService: FirebaseAuthService,
    private Storage: Storage,
    fcm: FcmProvider,
    private badge: Badge,

  ) {
    
/*     this.Storage.get('introShown').then((result) => {
      if(result){
        this.rootPage = FirebaseLoginPage;
      } else {
        this.rootPage = WalkthroughPage;
      }
    }); */
    
    platform.ready().then(() => {
      console.log('platform ready')
      //reset badge count
      this.badge.clear();
      this.statusBar.backgroundColorByHexString('#ffffff'); 

      this.fAuthService.afAuth.authState
        .subscribe(
          user => {
            console.log('check user logged in')
            if (user) {
              this.rootPage = TabsNavigationPage;
              //this.splashScreen.hide();
            } else {
              this.Storage.get('introShown').then((result) => {
                if(result){
                  this.rootPage = FirebaseLoginPage;
                  //this.splashScreen.hide();
                } else {
                  this.rootPage = WalkthroughPage;
                  this.Storage.set('introShown', true);
                  //this.splashScreen.hide();
                }
              });
            }
          },
          () => {
            this.rootPage = WalkthroughPage;
            //this.splashScreen.hide();
          }
        );

 this.splashScreen.hide();

      /* let self=this
      console.log('is logged in?') 
        firebase.auth().onAuthStateChanged(function(user) {
          if (user) {
            //self.nav.push(TabsNavigationPage);
            //self.rootPage = TabsNavigationPage;
            //self.app.getRootNavs()[0].setRoot(TabsNavigationPage)
            self.nav.setRoot(TabsNavigationPage);
            console.log('user logged in')
          } else{
            console.log('user not logged in')
            self.Storage.get('introShown').then((result) => {
              if(result){
                //walkthrough seen
                self.rootPage = FirebaseLoginPage;
                self.app.getRootNavs()[0].setRoot(FirebaseLoginPage)
              } else {
                //walkthrough not seen
                self.rootPage = WalkthroughPage;
                self.app.getRootNavs()[0].setRoot(WalkthroughPage)
                self.Storage.set('introShown', true);
              }
            });
          }
          }); */

      // Listen to incoming messages
      fcm.listenToNotifications().pipe(
        tap(msg => {
          // show a toast
          let messageText: string;
            if (platform.is('android')) {
            messageText = msg.body;
            }
            if (platform.is('ios')) {
            messageText = msg.body;
            //messageText = msg.aps.alert;
            }
            // show a toast
            const toast = this.toastCtrl.create({
            message: messageText,
            duration: 2000,
            position: 'top'
            });
            toast.present();
        })
      )
      .subscribe();
        
    }

);
  }  

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

  pushPage(page) {
    this.menu.close();
    this.app.getRootNav().push(page.component);
  }

  logout(){
    this.fAuthService.doLogout()
    .then((res) => {
      this.menu.close();
      this.nav.setRoot(FirebaseLoginPage);
      //this.Storage.set('loggedIn','No');
      this.toastCtrl.create({
        message: `Logged Out Successfully`,
        duration: 3000
      }).present();

    }, (error) => {
      //console.log("Logout error", error);
    });
  }

}
{
  "name": "privatedirect",
  "version": "1.2.14",
  "author": "Private Direct",
  "homepage": "https://www.PDonBoard.com",
  "private": true,
  "scripts": {
    "start": "ionic-app-scripts serve",
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint"
  },
  "dependencies": {
    "@agm/core": "^1.0.0-beta.2",
    "@angular/cli": "^6.2.3",
    "@angular/common": "^6.1.8",
    "@angular/compiler": "^6.1.8",
    "@angular/compiler-cli": "^6.1.8",
    "@angular/core": "^6.1.8",
    "@angular/forms": "^6.1.8",
    "@angular/http": "^6.1.8",
    "@angular/platform-browser": "^6.1.8",
    "@angular/platform-browser-dynamic": "^6.1.8",
    "@firebase/messaging": "^0.3.6",
    "@ionic-native/admob-free": "^5.0.0-beta.20",
    "@ionic-native/app-rate": "^5.0.0-beta.20",
    "@ionic-native/badge": "^5.0.0-beta.20",
    "@ionic-native/call-number": "^5.0.0-beta.20",
    "@ionic-native/core": "^5.0.0-beta.20",
    "@ionic-native/email-composer": "^5.0.0-beta.20",
    "@ionic-native/fcm": "^5.0.0-beta.20",
    "@ionic-native/firebase": "^5.0.0-beta.20",
    "@ionic-native/in-app-browser": "^5.0.0-beta.20",
    "@ionic-native/keyboard": "^5.0.0-beta.20",
    "@ionic-native/native-storage": "^5.0.0-beta.20",
    "@ionic-native/push": "^5.0.0-beta.20",
    "@ionic-native/social-sharing": "^5.0.0-beta.20",
    "@ionic-native/splash-screen": "^5.0.0-beta.20",
    "@ionic-native/status-bar": "^5.0.0-beta.20",
    "@ionic/lab": "^1.0.9",
    "@ionic/pro": "^2.0.3",
    "@ionic/storage": "^2.2.0",
    "@ngx-translate/core": "^10.0.2",
    "@ngx-translate/http-loader": "^3.0.1",
    "@types/core-js": "^2.5.0",
    "@types/googlemaps": "^3.30.13",
    "@types/jquery": "3.3.8",
    "angular2-text-mask": "^9.0.0",
    "angularfire2": "^5.0.1",
    "bourbon": "^5.1.0",
    "cordova-admob-sdk": "^0.18.0",
    "cordova-android": "^7.1.0",
    "cordova-browser": "^5.0.4",
    "cordova-ios": "4.5.5",
    "cordova-plugin-admob-free": "^0.18.0",
    "cordova-plugin-badge": "^0.8.7",
    "cordova-plugin-device": "^2.0.2",
    "cordova-plugin-email-composer": "^0.8.15",
    "cordova-plugin-file": "^6.0.1",
    "cordova-plugin-file-transfer": "^1.7.1",
    "cordova-plugin-firebase": "^1.1.4",
    "cordova-plugin-inappbrowser": "^3.0.0",
    "cordova-plugin-ionic": "^5.2.4",
    "cordova-plugin-ionic-keyboard": "^2.1.2",
    "cordova-plugin-ionic-webview": "^2.1.4",
    "cordova-plugin-nativestorage": "^2.3.2",
    "cordova-plugin-openwith-ios": "^1.0.3",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-whitelist": "^1.3.3",
    "cordova-plugin-x-socialsharing": "^5.4.1",
    "cordova-promise-polyfill": "^0.0.2",
    "emojione": "^3.1.7",
    "es6-promise-plugin": "^4.2.2",
    "firebase": "^5.5.1",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.5",
    "firebase-tools": "^4.2.1",
    "google-libphonenumber": "^3.1.14",
    "iatacodes": "^7.0.1",
    "ionic-angular": "^3.9.2",
    "ionic2-auto-complete": "^1.6.2-alpha",
    "ionicons": "^3.0.0",
    "jquery": "^3.3.1",
    "jquery-autocomplete": "^1.2.8",
    "jquery-ui": "^1.12.1",
    "libphonenumber-js": "^1.4.6",
    "linkifyjs": "^2.1.7",
    "moment": "^2.22.2",
    "mx.ferreyra.callnumber": "0.0.2",
    "npm-install-peers": "^1.2.1",
    "phonegap-plugin-push": "^2.2.3",
    "platform": "^1.3.5",
    "rxjs": "^6.3.2",
    "rxjs-compat": "^6.3.2",
    "sw-toolbox": "^3.6.0",
    "text-mask-addons": "^3.8.0",
    "videogular2": "^6.2.2",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.1.11",
    "@types/webpack-env": "^1.13.6",
    "typescript": "2.7.2"
  },
  "cordovaPlugins": [
    "cordova-plugin-device",
    "cordova-plugin-whitelist",
    "cordova-plugin-splashscreen",
    "cordova-plugin-statusbar",
    "cordova-plugin-firebase"
  ],
  "cordovaPlatforms": [
    {
      "platform": "ios",
      "version": "",
      "locator": "ios"
    }
  ],
  "config": {
    "ionic_sass": "./config/sass.config.js"
  },
  "cordova": {
    "platforms": [
      "android",
      "browser",
      "ios"
    ],
    "plugins": {
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-inappbrowser": {},
      "cordova-plugin-nativestorage": {},
      "cordova-plugin-x-socialsharing": {},
      "cordova-plugin-email-composer": {},
      "mx.ferreyra.callnumber": {},
      "cordova-plugin-whitelist": {},
      "cordova-plugin-ionic-keyboard": {},
      "cordova-plugin-firebase": {},
      "cordova-plugin-badge": {},
      "call-number": {},
      "cordova-plugin-file": {},
      "cordova-plugin-ionic": {
        "APP_ID": "ee062ef3",
        "CHANNEL_NAME": "master",
        "UPDATE_METHOD": "background",
        "WARN_DEBUG": "true",
        "UPDATE_API": "https://api.ionicjs.com",
        "MAX_STORE": "2",
        "MIN_BACKGROUND_DURATION": "30"
      }
    }
  }
}
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.pdonboard.privatedirect" version="1.2.14" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>Private Direct</name>
    <description>Free rewards for private flyers.</description>
    <author email="admin@pdonboard.com" href="https://www.pdonboard.com/">PD on Board</author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <allow-navigation href="*" />
    <allow-navigation href="http://localhost:8080/*" />
    <feature name="CDVWKWebViewEngine">
        <param name="ios-package" value="CDVWKWebViewEngine" />
    </feature>
    <preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine" />
    <preference name="orientation" value="portrait" />
    <preference name="DisallowOverscroll" value="true" />
    <preference name="webviewbounce" value="false" />
    <preference name="UIWebViewBounce" value="false" />
    <preference name="BackupWebStorage" value="none" />
    <preference name="SplashMaintainAspectRatio" value="true" />
    <preference name="FadeSplashScreen" value="true" />
    <preference name="FadeSplashScreenDuration" value="2000" />
    <preference name="SplashShowOnlyFirstTime" value="false" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="30000" />
    <preference name="AutoHideSplashScreen" value="false" />
    <preference name="ShowSplashScreen" value="true" />
    <preference name="ShowSplashScreenSpinner" value="true" />
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
        <icon height="57" src="resources/ios/icon/icon.png" width="57" />
        <icon height="114" src="resources/ios/icon/icon@2x.png" width="114" />
        <icon height="40" src="resources/ios/icon/icon-40.png" width="40" />
        <icon height="80" src="resources/ios/icon/icon-40@2x.png" width="80" />
        <icon height="120" src="resources/ios/icon/icon-40@3x.png" width="120" />
        <icon height="50" src="resources/ios/icon/icon-50.png" width="50" />
        <icon height="100" src="resources/ios/icon/icon-50@2x.png" width="100" />
        <icon height="60" src="resources/ios/icon/icon-60.png" width="60" />
        <icon height="120" src="resources/ios/icon/icon-60@2x.png" width="120" />
        <icon height="180" src="resources/ios/icon/icon-60@3x.png" width="180" />
        <icon height="72" src="resources/ios/icon/icon-72.png" width="72" />
        <icon height="144" src="resources/ios/icon/icon-72@2x.png" width="144" />
        <icon height="76" src="resources/ios/icon/icon-76.png" width="76" />
        <icon height="152" src="resources/ios/icon/icon-76@2x.png" width="152" />
        <icon height="167" src="resources/ios/icon/icon-83.5@2x.png" width="167" />
        <icon height="29" src="resources/ios/icon/icon-small.png" width="29" />
        <icon height="58" src="resources/ios/icon/icon-small@2x.png" width="58" />
        <icon height="87" src="resources/ios/icon/icon-small@3x.png" width="87" />
        <splash height="1136" src="resources/ios/splash/Default-568h@2x~iphone.png" width="640" />
        <splash height="1334" src="resources/ios/splash/Default-667h.png" width="750" />
        <splash height="2208" src="resources/ios/splash/Default-736h.png" width="1242" />
        <splash height="1242" src="resources/ios/splash/Default-Landscape-736h.png" width="2208" />
        <splash height="1536" src="resources/ios/splash/Default-Landscape@2x~ipad.png" width="2048" />
        <splash height="2048" src="resources/ios/splash/Default-Landscape@~ipadpro.png" width="2732" />
        <splash height="768" src="resources/ios/splash/Default-Landscape~ipad.png" width="1024" />
        <splash height="2048" src="resources/ios/splash/Default-Portrait@2x~ipad.png" width="1536" />
        <splash height="2732" src="resources/ios/splash/Default-Portrait@~ipadpro.png" width="2048" />
        <splash height="1024" src="resources/ios/splash/Default-Portrait~ipad.png" width="768" />
        <splash height="960" src="resources/ios/splash/Default@2x~iphone.png" width="640" />
        <splash height="480" src="resources/ios/splash/Default~iphone.png" width="320" />
        <icon height="1024" src="resources/ios/icon/icon-1024.png" width="1024" />
        <splash height="2732" src="resources/ios/splash/Default@2x~universal~anyany.png" width="2732" />
    </platform>
    <feature name="StatusBar">
        <param name="ios-package" onload="true" value="CDVStatusBar" />
    </feature>
    <plugin name="cordova-plugin-inappbrowser" spec="^1.7.2" />
    <plugin name="cordova-plugin-nativestorage" spec="^2.3.1" />
    <plugin name="cordova-plugin-x-socialsharing" spec="^5.3.2" />
    <plugin name="cordova-plugin-email-composer" spec="^0.8.15" />
    <plugin name="mx.ferreyra.callnumber" spec="0.0.2" />
    <plugin name="cordova-plugin-device" spec="^1.1.7" />
    <plugin name="cordova-plugin-statusbar" spec="^2.4.1" />
    <plugin name="cordova-plugin-whitelist" spec="^1.3.3" />
    <plugin name="cordova-plugin-ionic-keyboard" spec="^2.1.2" />
    <plugin name="cordova-plugin-splashscreen" spec="^4.1.0" />
    <plugin name="cordova-plugin-firebase" spec="^1.0.5" />
    <plugin name="cordova-plugin-badge" spec="^0.8.7" />
    <plugin name="cordova-plugin-file" spec="^6.0.1" />
    <plugin name="cordova-plugin-ionic" spec="^5.1.5">
        <variable name="APP_ID" value="ee062ef3" />
        <variable name="CHANNEL_NAME" value="master" />
        <variable name="UPDATE_METHOD" value="background" />
        <variable name="WARN_DEBUG" value="true" />
        <variable name="UPDATE_API" value="https://api.ionicjs.com" />
        <variable name="MAX_STORE" value="2" />
        <variable name="MIN_BACKGROUND_DURATION" value="30" />
    </plugin>
    <engine name="android" spec="^7.1.0" />
    <engine name="browser" spec="^5.0.4" />
    <engine name="ios" spec="4.5.5" />
</widget>