/**
 * @class
 * unotify
 */
import uloop from './uloop';

class unotify{
	//在一个周期内获取cid
	static  getCid(successCallback, errorCallback){
		this.UpdateCid((response) => {
			successCallback(response);
		}, (error) => {
			errorCallback(error);
		});
	}
	//周期性获取cid,周期可调整
	static getCidPeriotic(times){
//		var self = this;
//		return new Promise(function(resolve, reject){
//			var i = 0;
//			var cid = null;
//			var handle = null;
//			i++;
//			cid = self.getCidOnce(i);
//			console.log("第"+i+"次"+"获取的cid为"+cid);
//			if(cid != null && cid != 'null' || i >= times){
//				window.clearInterval(handle);
//				resolve(cid);
//			}else{
//				handle = setInterval(function(){
//					i++;
//					cid = self.getCidOnce(i);
//					console.log("第"+i+"次"+"获取的cid为"+cid);
//					if(cid != null && cid != 'null' || i >= times){
//						window.clearInterval(handle);
//						resolve(cid);
//					}
//				},1000);
//			}
//		});

		var self = this;
		return new Promise(function(resolve, reject){
			new uloop({
				loopContent: () => {
		//			var urlParams = {
		//					type: 'get',
		//					data: {},
		//			};
		//			return iot.network.send('http://192.168.2.75:3000/get', urlParams);
					console.log(self.getCidOnce());
					return self.getCidOnce();
				},
				timeout: times,
				interval:{
					delay: 1000,
					cb: {
						success: (response) => {
							console.log('intervalcbSuccess:'+JSON.stringify(response));	
						},
						error: (error) => {
							console.log('intervalcbError:'+JSON.stringify(error));
						}
					}
				}, 
				fincb: {
					success: (response) => {
						console.log('fincbSuccess:'+JSON.stringify(response));	
						resolve(response);
					},
					error: (error) => {
				    	console.log('fincbSuccess:'+JSON.stringify(error));
				    	if(error == 'timeout'){
				    		resolve(null);
				    	}
					},
					complete: () => {}
				},
				stopcondition: async (response) => {
		//			if(response.data.msg == 5){
		//				return true;
		//			}	
					if(response != null && response != 'null' ){
						return true;
					}
				}
			}).start();
		});
	}
	//单次获取cid
	static getCidOnce(i){
		var clientInfo = plus.push.getClientInfo();
		if(clientInfo && clientInfo.hasOwnProperty('clientid') && clientInfo.clientid){
//			if(i == 6){
				return(clientInfo.clientid);
//			}else{
//				return(null);
//			}
		}else{
			return(null);
		}
	}
	//长期不断循环获取cid,并更新cid
	static async UpdateCid(successCallback, errorCallback){
		var cid = null;
		var oldCid = null;
		try{
			cid = await this.getCidPeriotic(9);
			if(cid != null && cid != oldCid){
				iot.business.api.send('user/updateCid',{
					data: {
						clienId: cid
					},
					success: (response) => {
						if(typeof successCallback == 'function'){
							successCallback('cid updateSuccess');
						}
					},
					error: (error) => {
						console.log(error);
						errorCallback(error);
					}
				}, true, false);
			}else{
				successCallback('cid not updated');
			}
		}
		catch (err){
			console.log(err);
			if(typeof errorCallback == 'function'){
				errorCallback(err);
			}
		}
	}
}
export default unotify;