import { Component } from '@angular/core';
import {AlertController, IonicPage, NavController, NavParams} from 'ionic-angular';
import { AppService } from '../../service/appHttpService';
import { AppVersion } from '@ionic-native/app-version';
import { AppUpdateService } from '../../service/appUpdateService';
import { Storage } from "@ionic/storage";
import {LoginPage} from "../login/login";

@IonicPage()
@Component({
  selector: 'page-version',
  templateUrl: 'version.html'
})

export class VersionPage {
  version: string = '';
  latestVersion: string = '';
  isLatest:boolean = true;
  pusNumber:boolean;
  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams,
    public appService:AppService,
    private appUpdateService: AppUpdateService,
    private appVersion: AppVersion,
    private storage:Storage,
    private alertCtrl: AlertController
  ) {
    
  }

  ionViewWillEnter() {
    this.getversion();
    this.getAppPush();

      this.appUpdateService.compariVersion().subscribe(res => {
          var data = res.json();
          if (data.code == 200) {
              this.latestVersion = data.latestVersion;
          }
      });
  }

  //获取推送开关
  getAppPush(){
        this.appService.ObserverHttpGet('/wisdomgroup/modules/common/file/getAppPush',null)
            .subscribe(
                (res)=>{
                        this.pusNumber = res.json();
                }
            )
  }

  //保存推送开关
    savePush(){
        this.appService.ObserverHttpGetAdd('/wisdomgroup/modules/common/file/addAppPush/',this.pusNumber)
            .subscribe(
                (res)=>{}
            )
    }

   //获取最新版本信息
   getversion() {
    //检测是否需要更新
    this.appVersion.getVersionNumber().then((version: string) => {
      this.version = version;
      this.appUpdateService.compariVersion().subscribe(res => {
        var data = res.json();
        if (data.code == 200) {
          this.latestVersion = data.latestVersion;
          if (data.latestVersion != null && data.latestVersion != version) {
            this.isLatest = false;
          }
        }
      });
    }).catch(err => {
      console.log('getVersionNumber:' + err);
    });
  }

  //更新
  update(){
      if(this.isLatest){
          const alert = this.alertCtrl.create({
              subTitle: '已是最新版本',
              buttons: ['确定']
          });
          alert.present();
      }else{
          const confirm = this.alertCtrl.create({
              message: '有最新版本，是否更新?',
              buttons: [
                  {
                      text: '取消',
                      handler: () => {
                          console.log('Disagree clicked');
                      }
                  },
                  {
                      text: '确定',
                      handler: () => {
                          this.appUpdateService.downloadApp("/wisdomgroup/app/download");
                      }
                  }
              ]
          });
          confirm.present();
      }
  }


    //后台退出
    logoutApp() {
        let length = this.navCtrl.length();
        this.navCtrl.remove(length);
        this.storage.clear();
        this.appService.ObserverHttpGet("/wisdomgroup/app/logout", null)
            .subscribe((res: Response) => {
                    let data = res.json();
                    this.navCtrl.push('LoginPage');
                }, error => {
                    this.appService.alert('网络异常！');
                }
            );
    }
}
