/**
 * @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 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
        var isDebug = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
//		var send = this.opts.isCustom ? iot.business.api.sendCustom : iot.business.api.send;
//		iot.business.api.send(this.url, {
//		iot.business.api.sendCustom(this.url, {
		try{
			let response = await iot.business.api.send(this.url, {
				type: this.opts.type,
				data: this.opts.data
			}, needAuth, false, isDebug);
			if(this.intervalcb!= null){
				this.intervalcb.success(response);
			}
			if(await this.stopcondition(response)){
				if(this.fincb!= null){
					this.fincb.success(response);
					this.fincb.complete();
				}
				this.clearTime();
				this.canSend = false;
			}else{
				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);
		this.sendRequest();
		this.watcher = setInterval(() => {
			if(this.canSend && this.canDo){
				this.canDo = false;
				console.log("发送了请求");
				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;