/**
 * @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.loopContent = params.loopContent;
		this.timeout = params.timeout || 10;
		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;
		try{
			var response = null;
			response = await this.loopContent();
			console.log(response);
			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);
		
		let times = 1;
		this.handle = setInterval(() => {
			times++;
			console.log('times'+times);
			if(times > this.timeout){
				this.clearTime();
				this.canDo = false;
				this.fincb.error('timeout');
				this.fincb.complete();
				this.intervalcb = null;
				this.fincb = null;
			}else{
				this.canDo = true;
			}
		},this.intervalTime);
	}
}

//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;