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

var Folder = Class.create({
	initialize: function() {
	}
});

// On the service side, these are referred to as Use
Folder.TYPE_INBOX = 0;
Folder.TYPE_SENT = 1;
Folder.TYPE_DRAFTS = 2;
Folder.TYPE_TRASH = 3;
Folder.TYPE_JUNK = 4;
Folder.TYPE_OUTBOX = 5;
Folder.TYPE_GENERIC = 6;
Folder.TYPE_ALLINBOXES = 7;

Folder.SYNC_ONLINE = 1;    // email is pushed immediately 
Folder.SYNC_SCHEDULED = 2;
Folder.SYNC_OFFLINE = 3;
Folder.SYNC_NEW = 5;       // used for outbox (new messages in the folder initiate the sync)
Folder.SYNC_ONCE = 256;
Folder.SYNC_VERIFY = 512;

// Constants for how the list should be sorted.
Folder.SORT_BY_DATE_DESC = 0;
Folder.SORT_BY_DATE_ASC = 1;
Folder.SORT_BY_SENDER_ASC = 2;
Folder.SORT_BY_SENDER_DESC = 3;
Folder.SORT_BY_SUBJECT_ASC = 4;
Folder.SORT_BY_SUBJECT_DESC = 5;

Folder.STATUS_OFFLINE_MANUAL = 1;  // offer to sync now
Folder.STATUS_OFFLINE_SCHEDULED = 2; // offer to sync now
Folder.STATUS_WAIT_RETRY = 3; // offer to sync now (should we say hold long to retry?)
Folder.STATUS_SYNCING = 4;
Folder.STATUS_PUSHING = 5;
Folder.STATUS_LOGIN_FAILED = 6; // offer to go to acct page
Folder.STATUS_TRANSIENT_ERROR = 7; // just show alert icon
Folder.STATUS_ACCOUNT_UNAVAILABLE = 8; // live.com can lock you out for 15 minutes


Folder.identifier = 'palm://com.palm.mail';

/**
 * This will be an async mojo call
 * @param {Object} folderId
 * @param {Object} callback
 */
Folder.getFolder = function(folderId, callback) {
	//console.log("MojoRequest: messageList");
	var request = new Mojo.Service.Request(Folder.identifier, {
		method: 'messageList',
		parameters: {'folder':folderId},
		onSuccess: callback
	});
	return request;
}


Folder.getSmartFolders = function(controller, callback, error) {
	controller.serviceRequest(Folder.identifier, {
		method: 'smartFolders',
		parameters: {},
		onSuccess: callback,
		onFailure: error
	});
}


Folder.subscribeAllInboxesFolder = function(callback) {
	var request = new Mojo.Service.Request(Folder.identifier, {
		method: 'allInboxesFolder',
		parameters: {'subscribe':true},
		onSuccess: callback
	});
	return request;
}

Folder.subscribeAccountFolderList = function(accountId, filter, callback) {
	//console.log("MojoRequest: folderList");
	var request = new Mojo.Service.Request(Folder.identifier, {
  		method: 'folderList',
  		parameters: {'subscribe':true, 'account': accountId, 'folderFilter': filter},
  		onSuccess: callback
 	});
	return request;
}

Folder.getImapServerFolderList = function(accountId, callback) {
	var request = new Mojo.Service.Request(Folder.identifier, {
  		method: 'imapServerFolderList',
  		parameters: {'account': accountId},
  		onSuccess: callback
 	});
	return request;
}


Folder.setImapServerFolder = function(folderId, type, callback) {
	var request = new Mojo.Service.Request(Folder.identifier, {
  		method: 'setImapServerFolderUse',
  		parameters: {folder:folderId, usage:type},
 	});
	return request;
}

Folder.subscribeFavoriteFolderList = function(filter, callback, error) {
	//console.log("MojoRequest: subscribeFavoriteFolderList");
	var request = new Mojo.Service.Request(Folder.identifier, {
  			method: 'favoriteFolderList',
  			parameters: {'subscribe':true, 'folderFilter': filter},
			onSuccess: callback,
			onFailure: error
 		});
	return request;
}


Folder.getFolderAccountDetail = function(folderId, callback, error) {
	//console.log("MojoRequest: folderAccountDetails");
	return new Mojo.Service.Request(Folder.identifier, {
			method: 'folderAccountDetails',
			parameters: {'folder':folderId, 'subscribe':false},
			onSuccess: callback,
			onFailure: error
		});
}

Folder.subscribeFolderDetail = function(folderId, filter, callback, error) {
	//console.log("MojoRequest: folderDetail");
	var request = new Mojo.Service.Request(Folder.identifier, {
			method: 'folderDetail',
			parameters: {'folder':folderId, 'messagesFilter': filter, 'subscribe':true},
			onSuccess: callback,
			onFailure: error
		});
	return request;
}

Folder.folderStatus = function(folderId, filter, callback, error) {
	//console.log("MojoRequest: folderStatus");
	var request = new Mojo.Service.Request(Folder.identifier, {
			method: 'folderStatus',
			parameters: {'folder':folderId, 'messagesFilter': filter},
			onSuccess: callback,
			onFailure: error
		});
	return request;
}

Folder.syncFolder = function(folderId, force, callback, error) {
	//console.log("MojoRequest: syncFolder");
	var request = new Mojo.Service.Request(Folder.identifier, {
			method: 'syncFolder',
			parameters: {'folder':folderId, 'force':force},
			onSuccess: callback,
			onFailure: error
		});
	return request;
}

Folder.emptyFolder = function(folderId, callback, error) {
	//console.log("MojoRequest: emptyFolder");
	var request = new Mojo.Service.Request(Folder.identifier, {
			method: 'emptyFolder',
			parameters: {'folder':folderId},
			onSuccess: callback,
			onFailure: error
		});
	return request;
}

Folder.stopSyncFolder = function(folderId) {
	//console.log("MojoRequest: stopSyncFolder");
	var request = new Mojo.Service.Request(Folder.identifier, {
			method: 'stopSyncFolder',
			parameters: {'folder':folderId}
		});
	return request;
}

Folder.setFavorite = function(folderId, onoff) {
	//console.log("MojoRequest: setFavoriteFolder");
	var request = new Mojo.Service.Request(Folder.identifier, {
			method: 'setFavoriteFolder',
			parameters: {'folder':folderId, 'value':onoff}
		});
	return request;
}

Folder.getFolderType = function(use) {
	switch (parseInt(use)) {
		case Folder.TYPE_INBOX: return "inbox";
		case Folder.TYPE_SENT: return "sent";
		case Folder.TYPE_DRAFTS: return "drafts";
		case Folder.TYPE_TRASH: return "trash";
		case Folder.TYPE_JUNK: return "junk";
		case Folder.TYPE_OUTBOX: return "outbox";
		default: return "generic";
	}
}

Folder.setSortType = function(folderId, type) {
	var request = new Mojo.Service.Request(Folder.identifier, {
			method: 'setFolderSortType',
			parameters: {'folder':folderId, 'sortType':type}
		});
	return request;
}

Folder.getSortType = function(folderId, success, failure) {
	var request = new Mojo.Service.Request(Folder.identifier, {
			method: 'getFolderSortType',
			parameters: {'folder':folderId},
			onSuccess: success,
			onFailure: failure
		});
	return request;
}