import {Component, ElementRef, ViewChild} from '@angular/core';
import {NavParams, Platform} from 'ionic-angular';
import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';

import {TabsPage} from '../pages/tabs/tabs';
import {LoginPage} from "../pages/login/login";
import {Storage} from "@ionic/storage";
import {LoginService} from "../pages/login/login.service";
import {CommonService} from "../core/common.service";
import {timer} from "rxjs/observable/timer";
import {GetRequestService} from "../secret/getRequest.service";
import {AppVersion} from "@ionic-native/app-version";
import {AppUpdateService} from "../core/appUpdate.service";
import {MobileAccessibility} from "@ionic-native/mobile-accessibility";
import {HomePage} from "../pages/home/home";
import {GlobalData} from "../core/GlobleData";

@Component({
    templateUrl: 'app.html'
})
export class MyApp {
    @ViewChild('loadImg') loadImg: ElementRef;

    rootPage: any;
    showSplash = false;

    load = {
        imgUrl: null,
        httpUrl: null
    };

    app = {
        UpdateTips: false,
        AppUrl: '',
        UpdateText: '',
    };

    constructor(private platform: Platform, private statusBar: StatusBar, private commonSer: CommonService,
                private getRequest: GetRequestService, private appVersion: AppVersion,
                private appUpdate: AppUpdateService,
                private globle: GlobalData,
                private mobileAccess: MobileAccessibility,
                private splashScreen: SplashScreen, private storage: Storage, private loginSer: LoginService) {
        this.platform.ready().then(() => {
            //app字体不跟随手机字体大小变化
            this.mobileAccess.usePreferredTextZoom(false);
            this.statusBar.show();
            this.statusBar.overlaysWebView(false);
            this.statusBar.backgroundColorByHexString('#5991CA');
            this.statusBar.styleLightContent();
            this.storage.get("token").then(
                (value) => {
                    this.globle.token = value;
                    this.storage.get('mineInfo').then(
                        (value) => {
                            this.globle.userObj = value;
                            if (value) {
                                this.rootPage = TabsPage;
                            } else {
                                this.rootPage = LoginPage;
                            }
                            this.splashScreen.hide();
                        }
                    );
                    this.storage.get('area').then(
                        (value) => {
                            this.globle.area = value;
                        }
                    )
                }
            );
        });
    }

    imgLoad() {
        timer(500).subscribe(() => this.splashScreen.hide());
        console.log('图片加载完成')
    }

    //检测版本
    checkVersion() {
        let versionCode;
        let platform;
        if (this.platform.is('ios')) platform = 'IOS';
        if (this.platform.is('android')) platform = 'android';
        this.appVersion.getVersionNumber().then((version: string) => {
            versionCode = version.split('.').join('');
            const data = {
                code: platform
            };
            let res;
            const onlineVersion = res.data.AppVersion.split('.').join('');
            if (versionCode != onlineVersion) {
                this.app.UpdateTips = true;
                this.app.AppUrl = res.data.AppUrl;
                this.app.UpdateText = res.data.UpdateText;
            }
        }).catch(err => {
            console.log(err);
        });
    }


    //打开链接
    openUrl() {
        if (this.load.httpUrl) {
            this.commonSer.openUrlByBrowser(this.load.httpUrl);
        }
    }
}
