import { Component, ViewChild } from '@angular/core';
import { Platform, Nav, AlertController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Storage } from '@ionic/storage';

import { HomePage } from '../pages/home/home';
import { DevicesPage } from '../pages/devices/devices';
import { ConfigurationPage } from '../pages/configuration/configuration';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;
  rootPage:any = HomePage;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,
    private alertCtrl: AlertController,
  private storage: Storage) {
    /*platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
    });*/
  }

  openDevicesPage() {
    this.nav.push(DevicesPage);
  }

  openConfigurationPage() {
    this.nav.push(ConfigurationPage);
  }

  closeSession() {
    let alert = this.alertCtrl.create({
      title: 'Confirmación',
      message: 'Realmente desea cerrar Sesión?',
      buttons: [{
        text: 'Si',
        handler: () => {
          this.storage.set('authentication', '');
          this.storage.set('remember', false);
          this.storage.set('rootDevice', null);
          this.storage.set('startAlarm', false);
          this.storage.set('time', 30);
      	  this.nav.setRoot(HomePage);
        }
      },{
        text: 'No',
        handler: () => {
          alert.dismiss();
          return false;
        }
      }]
    });
    alert.present();
  }

}
