/**
* v_index.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 unotify from '../../public/unotify.js';

iot.ready(init);
function init() {
    //通用 dialog、loading组件
    uPublic.componentsInit(['u-dialog','u-loading','u-button']);
    uPublic.closeSlideBack();
    const app = new Vue({
        data:{          
            //配置组件
            componentsConfig: initComponentsConfig(),
            clientId: null,
            showLoginButtonFlag: true
        },
        mounted(){
        	if(window.iotDebug){
                iotDebug.push('start: Debug');
           	}
        	var self = this;
        	unotify.getCid(function(ret){
        		console.log(ret);
        		self.setClientId(ret);
        		if (plus.os.name == 'Android') {
	                iot.permission.checkPermission(['READ_EXTERNAL_STORAGE'], function (res) {
	                    console.log('success', res);
						loginButtonTap(self);
	                }, function (err) {
	                    console.log('error', err);
	                });
	            }else{
	            	loginButtonTap(self);
	            }
        	},function(err){
        		console.log(err);
        	})
        },
        methods:{
        	getShowLoginButtonFlag(){
        		return this.showLoginButtonFlag;
        	},
        	setShowLoginButtonFlag(flag){
        		this.showLoginButtonFlag = flag;
        	},
        	onLoginButtonTap(){
				uComponents.showLoading(this);
        		loginButtonTap(this);
        	},
        	getClientId(){
        		return this.clientId;
        	},
        	setClientId(id){
        		this.clientId = id;
        	}
        }   
    }).$mount('#app');
}

//配置组件参数
function initComponentsConfig() {
    return {
    	//登录 button 参数
        loginButton: {   
            initParam: {
                class: 'custom-guide-button',
                text: Vue.t('btn.guideLogin')
            }
        },        
        dialog: {
            initParam: {
                class: 'custom-dialog'
            }
        },
        loading: {
            initParam: {
                class: 'custom-loading'
            }
        }
    }
}

function loginButtonTap(self){
//	if(window.iotDebug){
//      iotDebug.push('start: initSDSLogin');
//  }
//	window.SDSPlugin.login({},(res)=>{
//		console.log(res);
////		alert(res);
//		var data = uPublic.checkResponseData(res.data);
//	    if(data){
////	    	alert("sds登陆成功");
//	    	cloudsLogin(self, data.UserID);
//	    }
//	},(err)=>{
//		alert(error);
//		console.log(err);
//	})
	iot.business.sds.login({
	    data: {
	        userinfo: {}
	    },
	    success: (response) => {
	    	console.log(response);
	    	if(window.iotDebug){
		        iotDebug.push('end: SDSloginSuccees');
		   	}
	    	var data = uPublic.checkResponseData(response.data);
	    	if(data){
	    		self.setShowLoginButtonFlag(false);
	    		if(data.hasOwnProperty('UserID')){
	    			console.log("sds登陆成功了");
	    			cloudsLogin(self, data.UserID);
	    		}
	    	}else{
	    		
	    	}
	    },
	    error: (error) => {
	    	console.log(error);
    		if(window.iotDebug){
                iotDebug.push('end: sds登陆失败'+JSON.stringify(error));
           	}
	    	if (window.iotDebug) {
                iotDebug.upload(iot,'sds登陆失败');
            }
	    },
	    complete: () => {
	    	uComponents.hideLoading(self);
	    }
	});
}

function cloudsLogin(self, id){
	uComponents.showLoading(self);
	if(window.iotDebug){
        iotDebug.push('start: cloudsLogin');
   	}
	iot.business.api.send('user/sds_login',{
		data: {
			username: id,
			clienId: self.getClientId()
		},
		success: (response) => {
			if(window.iotDebug){
		        iotDebug.push('end: cloudsLogin');
		   	}
    		if (window.iotDebug) {
                iotDebug.upload(iot,'登陆流程时间');
            }
    		console.log(response);
    		var data = uPublic.checkResponseData(response.data);
    		var infoArray = [];
    		if(data){
    			infoArray.push(['utoken', data.utoken]);
	            infoArray.push(['userinfo', data]);
	    		iot.business.websql.setMaps(infoArray, () =>{
	    			iot.navigator.openWindow({
				        url: '../device/index.html',
				        id: 'device'
				    });
	    		}, (error) => {
	    			console.log(error);
	    		});
			    plus.navigator.closeSplashscreen();
    		}
		},
		error: (error) => {
			console.log(error);
    		if(window.iotDebug){
                iotDebug.push('end: 云端登陆失败'+JSON.stringify(error));
           	}
	    	if (window.iotDebug) {
                iotDebug.upload(iot,'云端登陆失败');
            }
			uPublic.openRequestErrorAlert(self);
		},
		complete: () => {}
	},false);
}