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

Photos.Model.FacebookThumbs = Class.create(Photos.Model.Thumbs, {
	initialize: function($super, albumID, albumName,
				accountID, contactName, albumDescription) {
		if (accountID === undefined || contactName === undefined) {
			throw "Invalid parameters to FacebookThumbs()";
		}
		$super(albumID, albumName);
		this.accountID = accountID;
		this.contactName = contactName;
		this.albumDescription = albumDescription;
	},

	newFullscreenModel: function(index, icon) {
		Mojo.Log.info("Pushing fullscreen: ",
			this.albumID, this.albumName, index,
			this.accountID, this.contactName);
		return new Photos.Model.FacebookFullscreen(
			this.albumID, this.albumName, index,
			this.accountID, this.contactName, icon);
	},

	getDescription: function(callback) {
		if (this.albumDescription) {
			callback(this.albumDescription);
		} else {
			this.descriptionUpdate = callback;
			this.requestInfo();
		}
	},

	getName: function($super, callback) {
		if (this.albumName) {
			callback(this.albumName);
		} else {
			this.nameUpdate = callback;
			this.requestInfo();
		}
	},

	requestInfo: function() {
		if (this.nameRequest ||
			(this.albumName && this.albumDescription)) {
			return;
		}

		function gotAlbumDetails(reply) {
			delete this.nameRequest;
			this.nameUpdate((reply.details && reply.details.name) ||
				this.albumName ||
				"");
			this.descriptionUpdate(
				(reply.details && reply.details.description) ||
				this.albumDescription ||
				"");
		}

		this.nameRequest = this.controller.serviceRequest(
			'palm://com.palm.photo.facebook', {
				method: 'getAlbumDetails',
				onSuccess: gotAlbumDetails.bind(this),
				parameters: {
					accountTag: this.accountID,
					aid: this.albumID
				}
		});
	},

	newCommentsModel: function() {
		Mojo.Log.info("Pushing comments: ",
			this.albumID, this.albumName, this.accountID);
		return new Photos.Model.FacebookComments(
			'dunno.jpg', this.albumName, "album caption...",
			this.accountID, this.albumID);
	},

	showAlbumName: function() {
		return true;
	},

	allowComments: function() {
		return false;
	},

	useRowRequester: function() {
		return false;
	},

	request: function(offset, limit, done) {
		this.controller.serviceRequest(
			'palm://com.palm.photo.facebook', {
				method: "getPhotos",
				onSuccess: this.translate.bind(this, done),
				parameters: {
					aid: this.albumID,
					accountTag: this.accountID,
					offset: offset,
					limit: limit
				}
		});
	},

	translate: function(done, response) {
		if (!response || !response.photos) {
			done([], 0);
			return;
		}

		for (var i = 0, len = response.photos.length; i < len; i++) {
			var image = response.photos[i];
			image.thumb = image.icon;
			image.imagePath = image.url;
		}
		done(response.photos, parseInt(response.count, 10));
	}
});

