require('../../../source/Dr.js');

Dr.loadScript('../../../source/common/logic/job_center.js', function () {
	Dr.log("All script loaded");
	Dr.LoadAll();

	var JobCenter = Dr.Get('Logic.JobCenter');
	var JobBase = Dr.Get('Logic.JobBase');

	// define test job
	var JobTest = function (callback, data) {
		JobBase.call(this, callback, data);
		this.id = Dr.generateId();
	};
	JobTest.prototype = Object.create(JobBase.prototype);
	JobTest.prototype.constructor = JobTest;
	JobTest.prototype.start = function () {
		Dr.log('[JobTest] start', this.id, this.data);
		var _this = this;
		Dr.createDelayCallback(function () {
			if (_this.data === 3)
				_this.end(JobBase.status.ERROR);
			else
				_this.end(JobBase.status.END);
		}, 0.5);
	};


	var callback = function (status) {
		Dr.log('[callback] status:', status, 'arguments:', arguments);
	};
	var job_callback = function (status) {
		Dr.log('[job_callback] status:', status, 'arguments:', arguments);
		if (status == JobCenter.status.ERROR) {
			Dr.log('get error!!! omit');
		}
		return true; //all continue
	};

	var job_data_list = [1, 2, 3, 4, 5, 6, 7, 8];
	var job_center = new JobCenter(job_data_list, JobTest);
	job_center.start(callback);

	var job_data_list = [101, 102, 103, 104, 105, 106, 107, 108];
	var job_center = new JobCenter(job_data_list, JobTest);
	job_center.start(callback, job_callback);

	// Dr.startREPL();
});