/**
* v_login.js
* Version: 0.1
* User: wy
* Date: 2017-09-06
* Copyright(c)  2017. U-GEN Tech.Co,Ltd. All Rights Reserved.
* 登录页面
*/

import {iot, uPublic, uComponents} from '../../public/public.js';
import Update from '../../public/update.js';

iot.ready(init);
function init() {
    //通用 input、按钮、dialog、loading组件
    uPublic.componentsInit(['u-loading']);
    const app = new Vue({
        data:{  
            //配置组件
            componentsConfig: initComponentsConfig()   
        },
        mounted(){           
			resolve(this);
        },
        methods:{
        	
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }               
    }
}

function resolve(self){
	console.log(self)
	uComponents.showloading(self);
	cheackSdsDevice(self);
}

//检验在sds中，该用户是否绑定设备
function cheackSdsDeviceExit(self){
	uComponents.showLoading();
	iot.business.sds.getDevicesByUser({
	    success: (response) => {
	    	console.log(response);
	    	let data = uPublic.checkResponseData(response.data);
			if(data){
				//sds,绑定关系已存在
				cheackCloudDeviceExit(self, data.sn);
			}else{
				//sds,关系不存在
				linkToAddDevice();
			}
	    },
	    error: (error) => {
	    	console.log(error);
	    	uPublic.openRequestErrorAlert(self);
	    	uComponents.hideLoading(self);
	    },
		complete: () => {}
	});
}

//检验在云端中，该用户是否绑定设备
function cheackCloudDeviceExit(self, deviceId){
	iot.business.api.send('lock/getDeviceProductId', {
		data: {
			device_id: deviceId
		},
		success: (response) => {
			console.log(response);
			let data = uPublic.checkResponseData(response.data);
			if(data){
				//云端,绑定关系已存在
				linkToIndex(self,data);
			}else{
				//云端,关系不存在
				linkToAddDevice();
			}
		},
		error: (error) => {
			console.log(error);
			uComponents.hideLoading(self);
		},
		complete: () => {}
	});
}

//页面跳转到首页
async function linkToIndex(self, data){
	let updateInfo = await iot.storage.getMap('updateInfo');
	
	let newUpdateInfo = data.updateInfo;
	
	for(let i=0; i<newUpdateInfo.length; i++){
		let curVersion = newUpdateInfo[i].version;
		let oldVersion = '0.0.0';
		for(let j=0; j<updateInfo.length; j++){
			if(newUpdateInfo[i].product_id == updateInfo[j].product_id){
				oldVersion = updateInfo[j].version
			}
		}
		// 这里传入Update参数
		let update = new Update(curVersion,oldVersion,newUpdateInfo.url);
	    
        // 重写 update中的方法，在下面方法中可以对更新app的相关生命周期进行操作
        update.onDownloadStart = ()=>{
            console.log("index page log onDownloadStart");
            uComponents.hideLoading(self);
        }

        update.onDownloadSuccess = ()=>{
            console.log("index page log onDownloadSuccess");
        }

        update.onInstallStart = ()=>{
            console.log("index page log onInstallStart");
        }

        update.onInstallSuccess = ()=>{
            console.log("index page log onInstallSuccess");
            //资源包安装成功，跳转到首页
            iot.navigator.openWindow({
				url: '../device/index.html',
		        id: 'device',
		        styles: {
		        	popGesture: 'none'
		        },
		        extras: {
		        	needClose: true
		        }
		    });
		    //更新app缓存的productId和对应版本
		    iot.storage.setMap('updateInfo', newUpdateInfo, (response) => {
				console.log(res);
		    }, () => {});
        }

        update.onError = ()=>{
            console.log("index page log onError");
            uComponents.hideLoading(self);
        }

        update.startUpdate();
		console.log(update);
	}
}

//页面跳转到添加设备页面
function linkToAddDevice(){
	iot.navigator.openWindow({
		url: '../addDevice/index.html',
        id: 'device',
        styles: {
        	popGesture: 'none'
        },
        extras: {
        	needClose: true
        }
    });
}
