/* Copyright 2009 Palm, Inc.  All rights reserved. */

var Task = Class.create(Weave.Models.Model,
{
	load: function(scene, callback)
	{
		var self = this;
	    Weave.Services.request(Task._serviceId, 
		{
	        method: 'getTask',
	        parameters: 
			{
				id: this.id,
				fields: ['taskList']
			},
		},
        function(response)
		{
			self.merge(response);
			callback && callback(true, Task._fixup(response));
		},
        function(response)
		{
			callback && callback(false, response);
	    });
		return this;
	},
	
	save: function(scene, callback)
	{
		var self = this;
		Mojo.Log.info("Save task %j", this);
		Weave.Services.request(Task._serviceId, 
		{
			method: 'saveTask',
			parameters: 
			{
				id: this.id,
				subject: this.subject,
				completed: this.completed,
				taskListId: this.getTaskListId(),
				dueDate: this.dueDate,
				priority: this.priority,
				notes: this.notes,
				alarm: this.alarm,
				reminder: this.reminder,
				position: this.position,
			},
		},
		function(response)
		{
				if (!self.id) 
				{
					self.update(self, { id: response.id });
				}
				callback && callback(true, response);
			},
			function(response)
			{
				callback && callback(false, response);
		});
	},
	
	remove: function(scene, callback)
	{
	    Weave.Services.request(Task._serviceId, {
	        method: 'deleteTask',
	        parameters: 
			{
				id: this.id
			},
		},
        function()
		{
			callback && callback(true);
		},
		function()
		{
			callback && callback(false);
	    });
	},
	
	getPositionIndex: function(scene, callback)
	{
	    Weave.Services.request(Task._serviceId, 
		{
	        method: 'getTaskPositionIndex',
			parameters:
			{
				priority: this.priority,
				taskListId: this.getTaskListId(),
			},
		},
        function(response)
		{
			callback && callback(true, response.nextPosition, response.index);
		},
        function(response)
		{
			callback && callback(false, response);
	    });
	},
	
	move: function(scene, what, filter, to, from, callback)
	{
		Weave.Services.request(Task._serviceId,
		{
			method: 'moveTask',
			parameters:
			{
				id: this.id,
				taskListId: this.getTaskListId(),
				filter: filter,
				what: what,
				fromIndex: from,
				toIndex: to,
			},
		},
		function()
		{
			callback && callback(true);
		},
		function()
		{
			callback && callback(false);
		});
	},
	
	getTaskListId: function()
	{
		return this.taskListId == 'alltasks' ? undefined : this.taskListId;
	},
});

Object.extend(Task,
{
	_serviceId: "palm://com.palm.tasks",
	
	nameFromItem: function(item)
	{
	    return item.subject || $L('Task name...');
	},
	
	priorityName: function(priority)
	{
	    switch (priority) 
		{
	        case 1:
	            return 'high';
	        case 3:
	            return 'low';
	        default:
	            return 'normal';
	    }
	},
	
	// Fixup a task because everything is a string
	_fixup: function(task)
	{
		task.completed = parseInt(task.completed) ? true : false;
		task.dueDate = task.dueDate ? parseInt(task.dueDate) : null;
		task.priority = parseInt(task.priority);
		task.position = task.position ? parseInt(task.position) : null;
		return task;
	}
});


