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

Photos.Model.FacebookFullscreen = Class.create(
			Photos.Model.Fullscreen, Photos.Common.AutoListener, {
	initialize: function($super, albumID, albumName, index,
				accountID, contactName, initialIcon) {
		if (accountID === undefined || contactName === undefined) {
			throw "Invalid parameters to FacebookFullscreen()";
		}
		$super(albumID, albumName, index);
		this.contactName = contactName;
		this.accountID = accountID;
		this.initialIcon = initialIcon;
		this.bigArray = new Mojo.Model.BigArray(
					this._requestImagesList.bind(this));
		this.initialActivate = true;
	},

	aboutToActivate: function() {
		if (this.initialIcon && this.initialActivate) {
			this._centerCB(this.initialIcon);
		}
		// trigger a window move. would be nice to have a real api
		// for this.
		this.bigArray.slice(
				this.desiredIndex, this.desiredIndex + 1, true);
		// refresh at that new window.
		this.bigArray.requestFullWindow();
	},

	getDescription: function(callback) {
		this.detailsUpdate = callback;
	},

	getName: function($super, callback) {
		if (this.albumName) {
			callback(this.albumName);
			return;
		}
		
		function gotAlbumDetails(reply) {
			delete this.albumRequest;
			callback(reply.details && reply.details.name);
		}

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

	refreshDetails: function() {
		function gotImageDetails(reply) {
			delete this.detailRequest;
			delete this.details;
			if (!reply || !reply.returnValue || !reply.details) {
				Mojo.Log.error("Failed to get photo details.");
				return;
			}

			this.details = reply.details;
			this.detailsUpdate(this.details.description);
		}

		this.cancelDetailRequest();
		this.detailRequest = this.controller.serviceRequest(
			'palm://com.palm.photo.facebook', {
				method: 'getPhotoDetails',
				onSuccess: gotImageDetails.bind(this),
				parameters: {
					pid: this.centerImage.pid,
					accountTag: this.accountID
				}
		});
	},

	cancelAlbumRequest: function() {
		if (this.albumRequest) {
			this.albumRequest.cancel();
			delete this.albumRequest;
		}
	},

	cancelDetailRequest: function() {
		if (this.detailRequest) {
			this.detailRequest.cancel();
			delete this.detailRequest;
		}
	},

	_doCallback: function(callback, image) {
		var self = this;
		function clearCenter() {
			if (callback !== self._centerCB) {
				Mojo.Log.info("callback was not centerCB.");
				return false;
			}

			// a bug in imageview widget
			// where thumb === previous center url
			// does not trigger a reload
			delete self.initialActivate;
			return true;
		}

		if (!image) {
			if (clearCenter()) {
				delete this.centerImage;
				this.cancelDetailRequest();
			}
			return callback(null);
		}
		if (clearCenter()) {
			this.centerImage = image;
			this.refreshDetails();
			Mojo.Log.info("CENTER HIGH RES: " + image.url);
			return callback(image.url, null, this.name);
		}
		return callback(image.url, image.icon, this.name);
	},

	canComment: function() {
		return true;
	},

	viewComments: function(callback) {
		callback('comments', {
			model: new Photos.Model.FacebookComments(
				this.centerImage.icon, this.albumName,
				this.details ? this.details.description : "",
				this.accountID, this.albumID,
				this.centerImage.pid)
		});
	},

	cleanup: function() {
		this.removeAutoListeners();
		this.cancelDetailRequest();
	},

	runCallbacks: function() {
		var index = this.index;
		if (index < 0) {
			return;
		}
		var ret = this.bigArray.slice(index - 1, index + 2, true);
		if (index - 1 < 0) {
			this._doCallback(this._leftCB, null);
			this._doCallback(this._centerCB, ret[0]);
			this._doCallback(this._rightCB, ret[1]);
		} else {
			this._doCallback(this._leftCB, ret[0]);
			this._doCallback(this._centerCB, ret[1]);
			this._doCallback(this._rightCB, ret[2]);
		}
	},

	jumpTo: function($super, newIndex) {
		if (this.gotResponse) {
			return $super(newIndex);
		} else {
			this.desiredIndex = newIndex;
			return true;
		}
	},

	// Internal functions =======================================
	_responseConvert: function(offset, limit, response) {
		if (!response || !response.returnValue) {
			Mojo.Log.info("Error in getting service reply!");
			this.bigArray.setLength(0);
			this.index = 0;
			this.total = 0;
			return;
		}

		this.total = parseInt(response.count, 10);
		Mojo.Log.info("Parsed total photos to: " + this.total);
		this.bigArray.noticeUpdatedItems(offset, response.photos);
		if (!this.lengthSet) {
			this.lengthSet = true;
			this.bigArray.setLength(this.total);
		}

		// if we haven't gotten a response and our desiredIndex
		// fits the range, go ahead and know that this big array
		// now contains the items of the index we wanted originally,
		// then jump to it.
		if (!this.gotResponse && (this.desiredIndex !== undefined &&
			(offset <= this.desiredIndex &&
				this.desiredIndex < (offset + limit)))) {
			this.gotResponse = true;
			if (this.desiredIndex !== undefined) {
				this.jumpTo(this.desiredIndex);
				delete this.desiredIndex;
			}
		}
		this.runCallbacks();
	},

	_requestImagesList: function(offset, limit) {
		Mojo.Log.info("Requesting at offset:", offset, "limit:", limit);
		this.controller.serviceRequest(
			'palm://com.palm.photo.facebook', {
				method: "getPhotos",
				onSuccess: this._responseConvert.bind(
							this, offset, limit),
				parameters: {
					aid: this.albumID,
					accountTag: this.accountID,
					offset: offset,
					limit: limit
				}
		});
	}
});

