/**
 * @class
 * loop
 */


class Loop{
	constructor(params) {
       	this.handle = null;
		this.watcher = null;
		this.time = null;
		this.url = params.request.url;
		this.opts = params.request.opts;
		this.timeout = params.timeout | 30;
		this.fincb = params.fincb;
		this.intervalTime = params.interval.delay;
		this.intervalcb = params.interval.cb;
		this.stopcondition = params.stopcondition;
		this.canSend = true;
		this.canDo = true;
		this.i = 0;
    }
	
	requestsend(url, opts, needAuth, isPublic, i){
		let data = [0,0,0,0,0,0,0,1,0,0,0];
		console.log("请求时间"+this.i+'-'+new Date().getTime());
		setTimeout(() => {
			console.log("返回时间"+this.i+'-'+new Date().getTime());
			opts.success(data[this.i]);
		},100);
			
	}

	clearTime(){
		clearInterval(this.handle);
		clearInterval(this.watcher);
		clearTimeout(this.time);
	}
	
	async sendRequest(){
		this.canSend = false;
		var needAuth = this.opts.needAuth !== undefined ? this.opts.needAuth : true;
        var isDebug = this.opts.isDebug !== undefined ? this.opts.isDebug : false;
        var withoutUgen = this.opts.withoutUgen !== undefined ? this.opts.withoutUgen : false;
//		iot.business.api.send(this.url, {
//		iot.business.api.sendCustom(this.url, {
		var urlParams = {
				type: this.opts.type,
				data: this.opts.data
		};
		try{
			var response = null;
			if(withoutUgen){
				response = await iot.network.send(this.url, urlParams);
			}else{
				response = await iot.business.api.send(this.url, urlParams, needAuth, false, isDebug);
			}
			if(await this.stopcondition(response)){
				if(this.fincb!= null){
					this.fincb.success(response);
					this.fincb.complete();
				}
				this.clearTime();
				this.canSend = false;
			}else{
				if(this.intervalcb!= null){
					this.intervalcb.success(response);
				}
				this.canSend = true;
			}
		}
		catch (error){
			if(this.intervalcb!= null){
				if(this.intervalcb.error(error)){
					this.canSend = true;
				}else{
					if(this.fincb!= null){
						this.fincb.error(error);
						this.fincb.complete();
						this.clearTime();
					}
				}		
			}
		}
	}


	start(){
		console.log("开始");
		this.time = setTimeout(() => {
			this.clearTime();
			this.canDo = false;
			this.fincb.error('timeout');
			this.fincb.complete();
			console.log("超时");
			this.intervalcb = null;
			this.fincb = null;
		},this.timeout);
		console.log("第"+this.i+"次发送了请求");
		console.log("发送时间"+new Date().getTime());
		this.i++;
		this.sendRequest();
		this.watcher = setInterval(() => {
			if(this.canSend && this.canDo){
				this.canDo = false;
				console.log("第"+this.i+"次发送了请求");
				console.log("发送时间"+new Date().getTime());
				this.i++;
				this.sendRequest();
			}
		},1000);
		this.handle = setInterval(() => {
			this.canDo = true;
		},3000);
	}
}

//new Loop({
//	request: {
//		url: 'http://www.baidu.com',
//		opts: {
//			type: 'post',
//			data: {}
//		}
//	},
//	timeout: 6000,
//	interval:{
//		delay: 1000,
//		cb: {
//			success: (response) => {
//				console.log('intervalcbSuccess:'+response);	
//			},
//			error: (error) => {
//				console.log('intervalcbSuccess:'+error);
//			}
//		}
//	}, 
//	fincb: {
//		success: (response) => {
//			console.log('fincbSuccess:'+response);
//		},
//		error: (error) => {
//			console.log('fincbError:'+error);
//		},
//		complete: () => {}
//	},
//	stopcondition: (response) => {
//		if(response == 1){
//			return true;
//		}else{
//			return false;
//		}
//	}
//}).start();
export default Loop;
//module.exports =  Loop;