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

Photos.Scene.Comments = Class.create(Photos.Scene.Base, {
	setup: function($super) {
		$super();

		this.commentTextWidgetModel = {disabled: false};
		this.commentsListModel = {items: []};

		this.commentListWidget = this.controller.get('comment_list');
		this.commentTextWidget =
			this.controller.get('comment-text-widget');
		this.commentTextElement =
			this.commentTextWidget.querySelector(
				'[name=commentTextElement]');
		this.commentExpander = this.controller.get("comment_expander");
		
		this.controller.setupWidget('comment-text-widget', {
			textFieldName: "commentTextElement",
			hintText: $L('Enter comment here...'),
			multiline: true,
			enterSubmits: true,
			requiresEnterKey: true,
			changeOnKeyPress: true
		}, this.commentTextWidgetModel);
		
		this.controller.setupWidget('comment_list', {
			itemTemplate: this.templatePrefix + 'comments/entry',
			swipeToDelete: false
		}, this.commentsListModel);

		this.controller.get('comment_album_preview').style.background =
			"url(" + this.sceneModel.getPreview() + ")";
		this.controller.get('comment_description').update(
			$L("#{title} #{caption}").interpolate({
				title: this.sceneModel.getTitle(),
				caption: this.sceneModel.getCaption()
		}));
		
		this.autoListen(this.commentExpander,
			Mojo.Event.tap, this.expand.bind(this));
		this.autoListen(this.controller.get('SendButton'),
			Mojo.Event.tap, this.postComment.bind(this));
		this.autoListen(this.controller.document,
			'keyup', this.handleTextAreaKeyUp.bind(this));

		this.controller.get('comment-text-container').hide();
		this.controller.get('comment_container').hide();
		this.initialLoad = true;
	},

	aboutToActivate: function($super, callback) {
		$super(callback);

		if (!this.initalLoad) {
			delete this.initialLoad;
			this.requestComments();
		}
		this.adjustTextArea();
	},

	activate: function($super) {
		$super();
		this.commentTextWidget.mojo.focus();
	},

	expand: function() {
		if (this.expanding || this.expanded) {
			return;
		}

		this.expanding = true;

		this.commentExpander.addClassName("selected");
		this.commentListWidget.addClassName("shift");

		var self = this;
		this.controller.window.setTimeout(function() {
			self.expanded = true;
			self.expanding = false;
			var transition = self.controller.prepareTransition(
				Mojo.Transition.crossFade);
			self.commentsListModel.items = self.completeComments;
			self.controller.modelChanged(self.commentsListModel);
			
			self.commentListWidget.removeClassName("preview");
			self.commentListWidget.removeClassName("shift");
			self.commentExpander.addClassName("hide");
			transition.run();
		}, 700);
	},

	requestComments: function(scrollToBottom) {
		function translateComments(results, total, canComment) {
			if (canComment) {
				this.controller.get(
					'comment-text-container').show();
			}
			this.controller.get('comment_container').show();
			this.controller.get('comment_loading').hide();
			this.completeComments = results;
			this.completeCommentsTotal = total;
			this.controller.get("showmore-button").update(
				$L("Show All #{total}").interpolate({
					total: total
			}));

			if (this.expanded) {
				this.commentsListModel.items = results;
			} else {
				var displayed = Math.min(results.length, 2);
				this.commentsListModel.items =
					results.slice(-2);
				if (displayed < total) {
					this.commentExpander.show();
					this.commentListWidget.addClassName(
								"preview");
				} else {
					this.commentListWidget.removeClassName(
								"preview");
					this.commentExpander.hide();
				}
			}
			this.controller.modelChanged(this.commentsListModel);
			if (scrollToBottom) {
				this.adjustTextArea(true);
				this.controller.getSceneScroller().mojo.revealBottom();
			}
		}

		this.sceneModel.request(
			0, 250, translateComments.bind(this));
	},

	postComment: function() {

		function done(success) {
			if (!success) {
				Mojo.Log.error("Failed to post comment!");
			} else {
				this.commentTextWidget.mojo.setValue("");
			}
			this.commentTextWidgetModel.disabled = false;
			this.controller.modelChanged(
					this.commentTextWidgetModel);
			this.commentTextWidget.mojo.focus();

			if (success) {
				this.adjustTextArea(true);
				this.controller.getSceneScroller().mojo.revealBottom();
			}
			this.requestComments(true);
		}

		this.commentTextWidgetModel.disabled = true;
		this.controller.modelChanged(this.commentTextWidgetModel);
		this.sceneModel.postComment(
			this.commentTextWidget.mojo.getValue(),
			done.bind(this));
	},
	
	handleTextAreaKeyUp: function(event) {
		if (event && Mojo.Char.isEnterKey(event.keyCode) &&
			!this.commentTextWidgetModel.disabled) {
			this.postComment();
			Event.stop(event);
		}
		this.adjustTextArea();
	},
	
	adjustTextArea: function(force) {
		var height = this.controller.get(
					'comment-text-container').offsetHeight;
		if(force || (height === this.textFieldHeight)) {
			return;
		}
		Mojo.Log.info("Adjusting textfield offset to: " + height);
		this.commentListWidget.setStyle(
			{"padding-bottom": height + "px"});
		this.controller.sceneScroller.mojo.adjustBy(
			0, (this.textFieldHeight - height));
		this.textFieldHeight = height;
	}
});

