
var Mp3Tunes = Base.extend({
	constructor: function() {
		this.sessionId = null;
	},

	login: function(user, pass, callbacks) {
		new Mp3Tunes.LoginAction(this, user, pass, callbacks).execute();
	},

	getAccountData: function() {
		response = request("accountData");
	},

	getAlbums: function() { },

	getTracks: function(page, count, callbacks) {
		new Mp3Tunes.GetTracksAction(this, page, count, callbacks).execute();
	},

	getArtists: function() { },

	getPlaylists: function(callbacks) {
		new Mp3Tunes.GetPlaylistsAction(this, callbacks).execute();
	},

	getPlaylistTracks: function(playlist, callbacks) {
		new Mp3Tunes.GetPlaylistTracksAction(this, playlist.playlistId, callbacks).execute();
	},

	getTrackList: function() { },

	createPlaylist: function(playlistName, callbacks) {
		new Mp3Tunes.CreatePlaylistAction(this, playlistName, callbacks).execute();
	},

	deletePlaylist: function(playlist, callbacks) {
		new Mp3Tunes.DeletePlaylistAction(this, playlist.playlistId, callbacks).execute();
	},

	addTrack: function(playlist, track, callbacks) {
		new Mp3Tunes.AddTrackAction(this, playlist.playlistId, track.trackFileKey, callbacks).execute();
	},

	deleteTrack: function(playlist, track, callbacks) {
		new Mp3Tunes.DeleteTrackAction(this, playlist.playlistId, track.trackFileKey, callbacks).execute();
	},

	getBrowseTokens: function(type, callbacks) {
		new Mp3Tunes.GetBrowseTokensAction(this, type, callbacks).execute();
	},

	getTracks: function(type, token, callbacks) {
		new Mp3Tunes.GetTracksAction(this, type, token, callbacks).execute();
	},

	sessionId: null
},
{
	partnerToken: 6002911585,
	baseUrl: "http://www.mp3tunes.com/3rdparty/WiiTunes/API/",
	apiScript: "Api.php",
	lockerScript: "Locker.php"
});

Mp3Tunes.Action = AjaxAction.extend({
	mp3tunes: null,
	parameters: [],
	action: "",
	callbacks: null,

	constructor: function(mp3tunes, parameters, callbacks) {
		this.mp3tunes   = mp3tunes;
		this.parameters = parameters;
		this.callbacks  = callbacks;
	},

	getRequestUrl: function() {
		var requestUrl = Mp3Tunes.baseUrl + Mp3Tunes.apiScript + "?" + "command=" + encodeURIComponent(this.action) + "&";
		//var requestUrl = "http://ws.mp3tunes.com/api/v1/" + encodeURIComponent(this.action) + "?";

		for (key in this.parameters) {
			requestUrl += key + "=" + encodeURIComponent(this.parameters[key]) + "&";
		}

		requestUrl += "partner_token=" + Mp3Tunes.partnerToken;

		if (this.mp3tunes.sessionId != null) {
			requestUrl += "&sid=" + this.mp3tunes.sessionId;
		}

		return requestUrl;
	}
});

Mp3Tunes.LoginAction = Mp3Tunes.Action.extend({
	action: "login",

	constructor: function(mp3tunes, _username, _password, callbacks) {
		this.base(mp3tunes, { username: _username, password: _password }, callbacks);
	},

	onSuccess: function(response) {
		if (response.status != 1) {
			this.onFailure(response);
			return;
		}

		this.mp3tunes.sessionId = response.session_id;

		if (this.callbacks && this.callbacks.onLoginSuccess) {
			this.callbacks.onLoginSuccess(response);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onLoginFailure) {
			this.callbacks.onLoginFailure(response.errorMessage);
		}
	}
});

Mp3Tunes.GetTracksAction = Mp3Tunes.Action.extend({
	action: "lockerData",

	constructor: function(mp3tunes, _set, _count, callbacks) {
		this.base(mp3tunes, { type: "track", set: _set, count: _count }, callbacks);
	},

	onSuccess: function(response) {
		if (response.trackList === undefined) {
			response.errorMessage = "Mp3Tunes did not respond with a list of tracks.";
			this.onFailure(response);
		}

		if (this.callbacks && this.callbacks.onTrackList) {
			this.callbacks.onTrackList(response.trackList);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onTrackListFailure) {
			this.callbacks.onTrackListFailure(response.errorMessage);
		}
	}
});

Mp3Tunes.GetPlaylistsAction = Mp3Tunes.Action.extend({
	action: "lockerData",

	constructor: function(mp3tunes, callbacks) {
		this.base(mp3tunes, { type: "playlist" }, callbacks);
	},

	onSuccess: function(response) {
		if (response.playlistList === undefined) {
			response.errorMessage = "Mp3Tunes did not respond with a list of playlists.";
			this.onFailure(response);
		}

		if (this.callbacks && this.callbacks.onPlayLists) {
			this.callbacks.onPlayLists(response.playlistList);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onPlayListsFailure) {
			this.callbacks.onPlayListsFailure(response.errorMessage);
		}
	}
});

Mp3Tunes.GetPlaylistTracksAction = Mp3Tunes.Action.extend({
	action: "lockerData",

	constructor: function(mp3tunes, id, callbacks) {
		this.base(mp3tunes, { type: "track", playlist_id: id }, callbacks);
	},

	onSuccess: function(response) {
		if (response.trackList === undefined) {
			response.errorMessage = "Mp3Tunes did not respond with a list of tracks for the playlist.";
			this.onFailure(response);
		}

		if (this.callbacks && this.callbacks.onPlaylistTracks) {
			this.callbacks.onPlaylistTracks(response.trackList);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onPlaylistTracksError) {
			this.callbacks.onPlaylistTracksError(response.errorMessage);
		}
	}
});

Mp3Tunes.CreatePlaylistAction = Mp3Tunes.Action.extend({
	action: "playlistAdd",

	constructor: function(mp3tunes, playlistTitle, callbacks) {
		this.base(mp3tunes, { title: playlistTitle }, callbacks);
	},

	onSuccess: function(response) {
		if (this.callbacks && this.callbacks.onPlaylistCreated) {
			this.callbacks.onPlaylistCreated(response);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onPlaylistCreateFailure) {
			this.callbacks.onPlaylistCreateFailure(response.errorMessage);
		}
	}
});

Mp3Tunes.DeletePlaylistAction = Mp3Tunes.Action.extend({
	action: "playlistDelete",

	constructor: function(mp3tunes, playlistId, callbacks) {
		this.base(mp3tunes, { playlist_id: playlistId }, callbacks);
	},

	onSuccess: function(response) {
		if (this.callbacks && this.callbacks.onPlaylistDeleted) {
			this.callbacks.onPlaylistDeleted(response);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onPlaylistDeleteFailure) {
			this.callbacks.onPlaylistDeleteFailure(response.errorMessage);
		}
	}
});

Mp3Tunes.AddTrackAction = Mp3Tunes.Action.extend({
	action: "playlistTrackAdd",

	constructor: function(mp3tunes, playlistId, fileKey, callbacks) {
		this.base(mp3tunes, { playlist_id: playlistId, file_key: fileKey }, callbacks);
	},

	onSuccess: function(response) {
		if (this.callbacks && this.callbacks.onAddTrack) {
			this.callbacks.onAddTrack(response);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onAddTrackFailure) {
			this.callbacks.onAddTrackFailure(response.errorMessage);
		}
	}
});

Mp3Tunes.DeleteTrackAction = Mp3Tunes.Action.extend({
	action: "playlistTrackDelete",

	constructor: function(mp3tunes, playlistId, fileKey, callbacks) {
		this.base(mp3tunes, { playlist_id: playlistId, file_key: fileKey }, callbacks);
	},

	onSuccess: function(response) {
		if (this.callbacks && this.callbacks.onDeleteTrack) {
			this.callbacks.onDeleteTrack(response);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onDeleteTrackFailure) {
			this.callbacks.onDeleteTrackFailure(response.errorMessage);
		}
	}
});

Mp3Tunes.GetBrowseTokensAction = Mp3Tunes.Action.extend({
	action: "lockerData",

	constructor: function(mp3tunes, type, callbacks) {
		this.base(mp3tunes, { type: type.toLowerCase() + "_token" }, callbacks);
	},

	onSuccess: function(response) {
		if (this.callbacks && this.callbacks.onRetrieveTokens) {
			this.callbacks.onRetrieveTokens(response.tokenList);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onRetrieveTokensFailure) {
			this.callbacks.onRetrieveTokensFailure(response.errorMessage);
		}
	}
});

Mp3Tunes.GetTracksAction = Mp3Tunes.Action.extend({
	action: "lockerData",

	constructor: function(mp3tunes, type, _token, callbacks) {
		this.base(mp3tunes, { type: type.toLowerCase(), token: _token }, callbacks);
	},

	onSuccess: function(response) {
		if (this.callbacks && this.callbacks.onRetrieveTracks) {
			var tracksList = eval("response." + this.parameters.type + "List");

			this.callbacks.onRetrieveTracks(tracksList);
		}
	},

	onFailure: function(response) {
		if (this.callbacks && this.callbacks.onRetrieveTracksFailure) {
			this.callbacks.onRetrieveTracksFailure(response.errorMessage);
		}
	}
});

