//  @import com.hedgehoglab.application.service.BaseService;
//  @import com.hedgehoglab.solomon.service.exceptions.NoContactsFoundException;



function TasksService(scope){
    BaseService.call(this,scope);
    var _this=this;

//    (new Thread()).start(1000);
    
    this.get_tasks_for_contact=function(contact_id,callback){
        _this.request({
            uri:"/api/contacts/"+contact_id+"/tasks"
            ,type:"GET"
            ,callback:function(data){
                if(data && data.task){
                    if(!$.isArray(data.task)){
                        data.task=[data.task]
                    }
                }
                callback(data);
            }
        });
    };


	this.get_tasks_lookup_for_contact=function(contact_id,callback){
		_this.get_tasks_for_contact(contact_id,function(e){
			_tasks_lookup={};
			if(e){
				$.each(e.task,function(i,n){
					_tasks_lookup[n.id]=n;
				});

				// _deals_array.deals=e.deal;
				
			}
			callback(_tasks_lookup);
		});
    };

    this.get_tasks_for_user=function(user_id,callback,asThread){
        _this.request({
            uri:"/api/users/"+user_id+"/tasks"
            ,type:"GET"
            ,callback:function(data){
                 if(data && data.task){
                    if(!$.isArray(data.task)){
                        data.task=[data.task]
                    }
                }
                callback(data);
            }
        });
    };
    
    this.get_tasks_for_user_with_filter=function(user_id,task_type,from_date,to_date,complete,callback){
        var query="?";
        if (task_type!=null) query = query + "t=" + task_type + "&";
        if (from_date!=null) query = query + "fd=" + from_date + "&";
        if (to_date!=null) query = query + "td=" + to_date + "&";
        if (complete!=null) query = query + "c=" + complete + "&";
    	$.getJSON("/api/users/"+user_id+"/tasks"+query,{},function(data){
            callback(data);
        });
    };

    this.get = function(filter,callback){
        var query="?";
        if (filter.type!=null) query = query + "t=" + filter.type + "&";
        if (filter.from_date!=null) query = query + "fd=" + filter.from_date + "&";
        if (filter.to_date!=null) query = query + "td=" + filter.to_date + "&";
        if (filter.complete!=null) query = query + "c=" + filter.complete + "&";

//    	$.getJSON("/api/users/"+user_id+"/tasks"+query,{},function(data){
//            callback(data);
//        });
        _this.request({
            uri:"/api/users/"+filter.user+"/tasks"+query,
            type:"GET",
            callback:function(data){
                callback(data);
            }
        });
    };

	this.get_task_types_lookiup=function(callback){
		_this.get_task_types(function(e){
			var _tasks_lookup={};
			if(e){
				$.each(e.taskType,function(task_type_index,task_type){
					_tasks_lookup[task_type.id]=task_type;
				});
				
			}
			callback(_tasks_lookup);
		});
	};
	
    this.get_task_types=function(callback){
        _this.request({
            uri:"/api/task-types"
            ,type:"GET"
            ,callback:function(data){
                callback(data);
            }
        });
	};
	
	this.update_task=function(task,callback){
		   _this.request({
               uri:"/api/tasks/"+task.id,
               data:task,
               type:"PUT",
               callback:function(e){
//					alert("hehehe");
					callback(e);
				}
           });
    };
    
    this.add_task=function(task,callback){
           _this.request({
             uri:"/api/tasks",
             data:task,
             type:"POST",
             callback:callback
         });
    };

    // TODO refactor this? get task model from server?
    // TODO return task? why callback?
    this.empty_task=function(callback){
        var task={
            "id":null,
            "title":"New Task",
            "assignedTo":null,
            "dueOn":null,
            "complete":false,
            "type":null,
            "createdAt":(new Date()),
            "contact":null,
            "createdBy":null
            };
        callback(task);
        // return task;
    };
};