import { Component,ViewChild } from '@angular/core';
import {Platform, AlertController, Content} from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { Storage } from "@ionic/storage";
import { LoginPage } from '../pages/login/login';
import { AppVersion } from '@ionic-native/app-version';
import { AppUpdateService } from '../service/appUpdateService';
import { Keyboard } from '@ionic-native/keyboard';
import { Badge } from '@ionic-native/badge';
import {Response} from "@angular/http";
import {TabsPage} from "../pages/tabs/tabs";
import  { Geolocation } from "@ionic-native/geolocation";
import {AppService} from "../service/appHttpService";
@Component({
  templateUrl: 'app.html'
})
export class MyApp {

  @ViewChild(Content) content: Content;
  rootPage;

  user;
  constructor(platform: Platform, 
    statusBar: StatusBar, 
    splashScreen: SplashScreen,
    alertCtrl: AlertController,
    keyboard:Keyboard,
    appUpdateService: AppUpdateService,
    appVersion: AppVersion,
     private geolocation: Geolocation,
    badge:Badge,
    // public navCtrl: NavController,
    public storage: Storage,
    public appService: AppService
  ) {
    platform.ready().then(() => {

      statusBar.styleDefault();
      splashScreen.hide();
      //检测是否需要更新
      appVersion.getVersionNumber().then((version: string) => {
        appUpdateService.compariVersion().subscribe(res => {
            let data = res.json();
            if (data.code == 200) {
              if (data.latestVersion != null && data.latestVersion != version) {
                appUpdateService.detectionUpgrade(data.androidDownload, true); //提示升级
              }
          }
        });
      }).catch(err => {
        console.log('getVersionNumber:' + err);
      });
    });





    this.loadLogin();
  }


  loadLogin(){
      this.storage.get("userLoginInfo").then((value)=>{
          if(value!=null && value != ''){
              this.user = value;
              this.appService.ObserverHttpPost("/wisdomgroup/app/loginpost", this.user)
                  .subscribe((res: Response) => {
                          let data = res.json();
                          if (data["code"] == '200') {
                              //将当前登录用户存进缓存，便于后期逻辑操作
                              this.storage.set('user', data.data);
                              this.user.userid = data.data.id;
                              this.storage.set('userLoginInfo',this.user);
                              // return this.navCtrl.setRoot(TabsPage, { "user": this.user });
                              this.rootPage = TabsPage;
                          } else {
                              this.appService.alert('手机号或密码错误！');
                              this.rootPage = LoginPage;
                              // return this.navCtrl.setRoot(LoginPage);
                          }
                      }, error => {
                            this.rootPage = LoginPage;
                          this.appService.alert('网络异常！');
                      }
                  );
          }
          else{
              this.rootPage = LoginPage;
          }
      });
  }

}
