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 {NoUserMsg} from "./app.constants";
import {AppVersion} from "@ionic-native/app-version";
import {AppUpdateService} from "../core/appUpdate.service";
import {MobileAccessibility} from "@ionic-native/mobile-accessibility";

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

    rootPage: any;
    showSplash = true;

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

    noUserMsg = NoUserMsg;

    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 mobileAccess: MobileAccessibility,
                private splashScreen: SplashScreen, private storage: Storage, private loginSer: LoginService) {
        this.platform.ready().then(() => {
            this.getLoad();

            //app字体不跟随手机字体大小变化
            this.mobileAccess.usePreferredTextZoom(false);
            this.splashScreen.show();
            this.statusBar.show();
            this.statusBar.overlaysWebView(false);
            this.statusBar.backgroundColorByHexString('#343435');
            this.statusBar.styleLightContent();
        });
    }


    //app启动图
    getLoad() {
        let res;
        if (res.data) {
            this.load.imgUrl = res.data.SourceUrl;
            this.load.httpUrl = res.data.SubTitle;
            timer(3000).subscribe(() => {
                this.showSplash = false;
            });
        } else {
            this.splashScreen.hide();
            this.showSplash = false;
        }
        timer(4000).subscribe(() => this.checkVersion())
        this.checkAuth();
    }

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

    //鉴权
    checkAuth() {
        //骏客app权限校验
        const req = <any>this.getRequest.getParams();
        // alert("req:" + JSON.stringify(req));
        if (req.source != undefined && req.source) {
            const source = req.source;
            const token = req.token;
        } else {
            this.checkLogin();
        }
    }

    checkLogin() {
        this.storage.get('Authorization').then(value => {
            if (value) {
                this.rootPage = TabsPage;
            } else {
                this.rootPage = LoginPage;
            }
        });
    }

    //用户是否同步
    userAsync(res) {
        if (res.data.UserId == '00000000-0000-0000-0000-000000000000') {  //用户不存在
            this.rootPage = LoginPage;
            this.commonSer.alert(this.noUserMsg);
        } else {   //用户存在
            this.storage.set('user', res.data);
            timer(300).subscribe(e => {
                this.rootPage = TabsPage;
            })
        }
    }

    //检测版本
    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);
        }
    }
}
