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

var TaskList = Class.create(Weave.Models.Model,
{
	load: function(scene, callback)
	{
		var self = this;
	    Weave.Services.request(Task._serviceId, 
		{
	        method: 'getTaskList',
	        parameters: 
			{
				id: this._getId(),
			},
		},
        function(response)
		{
			TaskList._fixup(response);
			self.merge(response);
			callback && callback(true, self);
		},
        function(response)
		{
			callback && callback(false, response);
	    });
		return this;
	},
	
	save: function(scene, callback)
	{
		var self = this;
		Weave.Services.request(TaskList._serviceId, 
		{
			method: 'saveTaskList',
			parameters: 
			{
				id: this._getId(),
				name: this.name,
				position: this.position,
				show: this.show + ":" + this.order,
			},
		},
		function(response)
		{
			if (!self.id) 
			{
				self.update(scene, { id: response.id });
			}
			callback && callback(true)
		},
		function()
		{
			callback && callback(false);
		});
	},
	
	remove: function(scene, callback)
	{
		Weave.Services.request(TaskList._serviceId,
		{
			method: 'deleteTaskList',
			parameters:
			{
				id: this._getId(),
			},
		},
		function()
		{
			callback && callback(true);
		},
		function()
		{
			callback && callback(false);
		});
	},

	move: function(scene, filter, to, from, callback)
	{
		Weave.Services.request(TaskList._serviceId,
		{
			method: 'moveTaskList',
			parameters:
			{
				id: this._getId(),
				filter: filter,
				fromIndex: from,
				toIndex: to,
			}
		},
		function()
		{
			callback && callback(true);
		},
		function()
		{
			callback && callback(false);
		});
	},
	
	setAllComplete: function(scene, completed, callback)
	{
		Weave.Services.request(TaskList._serviceId,
		{
			method: completed ? 'markTasksCompleted' : 'markTasksIncomplete',
			parameters:
			{
				taskListId: this._getId(),
			},
		},
		function()
		{
			callback && callback(true);
		},
		function()
		{
			callback && callback(false);
		});
	},
	
	purgeAllComplete: function(scene, callback)
	{
		Weave.Services.request(TaskList._serviceId,
		{
			method: 'purgeCompletedTasks',
			parameters:
			{
				taskListId: this._getId(),
			},
		},
		function()
		{
			callback && callback(true);
		},
		function()
		{
			callback && callback(false);
		});
	},
	
	setAllDueDate: function(scene, dueDate, callback)
	{
		Weave.Services.request(TaskList._serviceId,
		{
			method: 'setTasksDueDate',
			parameters:
			{
				taskListId: this._getId(),
				dueDate: dueDate ? dueDate.getTime() : null,
			},
		},
		function()
		{
			callback && callback(true);
		},
		function()
		{
			callback && callback(false);
		});
	},
	
	getTasks: function(scene, what, filter, order, offset, limit, callback)
	{
		Mojo.Log.info('getTasks %s %s %s', this.id, offset, limit);
		
		if (this.id === undefined)
		{
			callback(true, [], 0);
			return;
		}
		
		var self = this;
		Weave.Services.request(TaskList._serviceId,
		{
			method: 'getTasks',
			parameters:
			{
    			_new: true, // Use new optimize query with no notification
				offset: offset,
				limit: limit,
				filter: filter,
				taskListId: this._getId(),
				what: what,
				order: order,
			},
		},
		function(response)
		{
			var list = response.list;
			var len = list.length;
			for (var i = 0; i < len; i++)
			{
				list[i] = Task._fixup(list[i]);
				list[i].taskListId = self.id;
			}
			callback && callback(true, list, response.count);
		},
		function(response)
		{
			callback && callback(false, response);
		});
	},
	
	addExternalUpdateListener: function($super, scene, observer)
	{
		// If task list is saved (has an id), we observer it for any behind-the-back changes
		Mojo.Log.info("Setting sync on tasklist %d", this.id)
		if (this.id) 
		{
			if (!this._syncrequest) 
			{
				var self = this;
				this._syncrequest = new Weave.Services.ServiceObserver(scene, "palm://com.palm.tasks/getTasks", 
				{
					taskListId: this._getId(),
					what: 'show_all',
					offset: 0,
					limit: 0,
					_new: true,
				}, function(status)
				{
					Mojo.Log.info("Tasklist sync callback");
					self.externalUpdate(scene);
				}, { active: 5 });
			}
			$super(scene, observer);
		}
	},
	
	removeExternalUpdateListener: function($super, observer)
	{
		if (this._syncrequest) 
		{
			Mojo.Log.info("RemoveExternalUpdateListener");
			this._syncrequest.stop();
			this._syncrequest = null;
			$super(observer);
		}
	},
	
	_getId: function()
	{
		return TaskList.isAllTasks(this) ? undefined : this.id;
	},
});

Object.extend(TaskList,
{
	_serviceId: Task._serviceId,
	
	getAll: function(scene, filter, offset, limit, unfiled, callback)
	{
		Weave.Services.request(TaskList._serviceId, 
		{
	        method: 'getTaskLists',
			parameters: {
				_new: true,
				offset: offset,
				limit: limit,
				filter: filter,
				unfiled: unfiled,
			},
		},
        function(response)
		{
			var list = response.list;
			for (var i = 0; i < list.length; i++)
			{
				TaskList._fixup(list[i]);
			}
			callback && callback(true, response.list, response.count);
		},
		function(response)
		{
			callback && callback(false, response);
    	});
	},
	
	isNew: function(item)
	{
	    return !item.id;
	},
	
	isDefault: function(item)
	{
	    return (item.flags && ((item.flags & 1) != 0));
	},
	
	isEAS: function(item)
	{
		return (item.flags && ((item.flags & 0x10000) != 0));
	},
	
	isSpecial: function(item)
	{
		return (item == null || (item.flags && ((item.flags & 0x10001) != 0)));
	},
	
	isAllTasks: function(item)
	{
		return (item && item.id == 'alltasks');
	},
	
	getNameWithDefault: function(taskList)
	{
		var taskListName;
	    if (taskList) 
		{
			taskListName = taskList.name;
			if (!taskListName) 
			{
				if (taskList.id) 
				{
					if (TaskList.isEAS(taskList))
					{
						taskListName = $L('EAS');
					}
					else if (TaskList.isDefault(taskList)) 
					{
						taskListName = $L('Unfiled');
					}
					else
					{
						taskListName = $L('List name');
					}
				}
				else
				{
					taskListName = $L('List name');
				}
			}
	    }
	    else 
		{
	        taskListName = $L('All tasks');
	    }
	    return taskListName;
	},
	
	_fixup: function(list)
	{
		var disp = list.show ? list.show.split(":") : [];
		list.show = disp[0] ? disp[0] : "all";
		list.order = disp[1] ? disp[1] : "position";
	}
});