var paths = require('../paths');

var Scheduler = require(paths.lib + 'scheduler');
var Task = require(paths.lib + 'task');
var jobs = require(paths.jobs);
var Logger = require('./logger');
var moment = require('moment');
/**
	@class Worker
	@extends Scheduler
	@extends Db
*/
var Worker = function(name){
	Scheduler.apply(this,arguments);
	
	/** @private **/
	var _self = this;
	var logger = new Logger();
	
	/** @public **/
	this.name = name;

	
	/** @public **/
	this.createTask = function(hash){
		
		if(!hash) return false;
		
		var _worker = this;
		
		// Instance of tasks to be configured
		var task  = new Task(hash);
		
		// Add method to execute job
		task.execute = jobs[task.job.name];
		
		// Listening Task events
		
		task.once("start", logger.start);
		task.once("schedule", logger.schedule);
		task.once("unschedule", logger.unschedule);
		task.once("error", logger.error);

		task.once("end", function(id, date){
			var id = id || task.id;
			var date = date || task.date;
			logger.end(id, function(){
				if( task.repeat ){
					logger.repeat(id, date, task.repeat);
				}
			});
		});
		console.log('listeners count', task.listeners('schedule').length);
		return task;
	};
	
	/** @public **/	
	this.switcher = function(doc){
		console.log(_self);
		// convert document to real task
		var task = _self.createTask(doc);

		// Decide what to do with a task
		if( task.isWaiting() ){
			if ( task.isDelayed() ){
				_self.executeTask(task);
			} else _self.schedule(task);

		} else if( task.isReschedule() ){
			_self.reschedule(task);
		
		} else if( doc.deleted || doc.status === 'done' || task.isCancel()){
			_self.unschedule(task);
		}
	};
	
	/** @private **/	
	var __construct = function(_self){
		logger.init(_self.name, _self.switcher);
		
		// Start timer extented from scheduler
		_self.startClock();
		
		function exit(){
			logger.resetAll(_self.name, function(err, res){
				if (err) console.log(err);
				else console.log('All tasks reseted');
				process.exit();
			});
		}
		
		// When was to exit, change all scheduled docs to waiting again
		process.on('SIGINT', function () {
			console.log('SIGINT');
			exit();
		});
		process.on('SIGTERM', function () {
			console.log('SIGTERM');
			exit();
		});
	}(this);
};

/**
** @static
*/
Worker.getWorker = function(name){
	return new Worker(name);
};
module.exports = Worker;
@@ -1246,28 +1246,31 @@
 %7B%0A%09%09
-console.log(
+(function(doc, 
 _se
-l
 f)
-;%0A
+%7B%0A%09
 %09%09//
@@ -1300,16 +1300,17 @@
 al task%0A
+%09
 %09%09var ta
@@ -1340,16 +1340,17 @@
 oc);%0A%0A%09%09
+%09
 // Decid
@@ -1376,16 +1376,17 @@
  task%0A%09%09
+%09
 if( task
@@ -1401,16 +1401,17 @@
 ng() )%7B%0A
+%09
 %09%09%09if ( 
@@ -1434,16 +1434,17 @@
  )%7B%0A%09%09%09%09
+%09
 _self.ex
@@ -1460,16 +1460,17 @@
 (task);%0A
+%09
 %09%09%09%7D els
@@ -1496,16 +1496,17 @@
 sk);%0A%0A%09%09
+%09
 %7D else i
@@ -1531,16 +1531,17 @@
 le() )%7B%0A
+%09
 %09%09%09_self
@@ -1559,19 +1559,18 @@
 (task);%0A
-%09%09
 %0A
+%09
 %09%09%7D else
@@ -1635,16 +1635,17 @@
 ())%7B%0A%09%09%09
+%09
 _self.un
@@ -1662,17 +1662,36 @@
 ask);%0A%09%09
-%7D
+%09%7D%0A%09%09%7D)(doc, _self);
 %0A%09%7D;%0A%09%0A%09

isolate context