package com.tootsville.tootsbook.client.panel.wallobjects;

import org.gwtwidgets.client.wrap.Callback;
import org.gwtwidgets.client.wrap.Effect;
import org.gwtwidgets.client.wrap.EffectOption;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.tootsville.tootsbook.client.BookServiceAsync;
import com.tootsville.tootsbook.client.TootsBook;
import com.tootsville.tootsbook.client.panel.ErrorMessage;
import com.tootsville.tootsbook.client.util.DisplayText;
import com.tootsville.tootsbook.client.util.Message;
import com.tootsville.tootsbook.client.util.Post;
import com.tootsville.tootsbook.client.util.TimeStampLabel;
import com.tootsville.tootsbook.client.util.ShadowUser;

/**
 * 
 * @author <a href="mailto:twheys@gmail.com@resinteractive.com">Tim Heys</a>
 * 
 */
public class Comment extends FocusPanel {
	/**
	 * 
	 */
	protected final Post post;

	/**
	 * <pre>
	 * twheys@gmail.com Jan 18, 2010
	 * </pre>
	 * 
	 * A Comment WRITEME...
	 * 
	 * @param newPost WRITEME
	 */
	public Comment (final Post newPost) {
		setStyleName ("comment-icons-parent");
		post = newPost;
		post.setContainingPanel (this);

		final ShadowUser shadowUser = post.getAuthor ();
		final HorizontalPanel commentBox = new HorizontalPanel ();
		commentBox.setWidth ("100%");
		final Label comment = new Label (post.getMessage ());
		comment.setStyleName ("primary-text");

		final TimeStampLabel timeStamp = new TimeStampLabel (newPost
				.getTimeStamp ());
		timeStamp.setStyleName ("comment-timestamp add-comment-link");

		final VerticalPanel commentPane = new VerticalPanel ();
		commentPane
		.setVerticalAlignment (HasVerticalAlignment.ALIGN_TOP);
		commentPane.add (comment);

		commentPane
		.setVerticalAlignment (HasVerticalAlignment.ALIGN_BOTTOM);
		commentPane
		.setHorizontalAlignment (HasHorizontalAlignment.ALIGN_RIGHT);
		commentPane.add (timeStamp);
		commentPane.setSize ("100%", "70px");

		final VerticalPanel options = buildDeleteAndReportButtons ();

		commentBox
		.setVerticalAlignment (HasVerticalAlignment.ALIGN_TOP);
		final FocusPanel icon = shadowUser.getIcon ();
		commentBox.add (icon);
		commentBox.setCellWidth (icon, "65px");
		commentBox.add (commentPane);
		commentBox.add (options);
		commentBox.setCellWidth (options, "15px");

		commentBox.setStyleName ("comment-margin comment-bg-color");

		add (commentBox);
	}

	/**
	 * <pre>
	 * twheys@gmail.com Dec 18, 2009
	 * </pre>
	 * 
	 * TO buildDeleteAndReportButtons WRITEME...
	 * 
	 * @return WRITEME
	 */
	private VerticalPanel buildDeleteAndReportButtons () {
		final Image deletePost = new Image (
		"/tootbook-resource/images/ui/delete.png");
		deletePost.setTitle (DisplayText.COMMENT_DELETE_TITLE);
		deletePost.setStyleName ("comment-icons");
		deletePost.addClickHandler (new ClickHandler () {
			@Override
			public void onClick (final ClickEvent event) {
				deletePost ();
			}
		});
		final Image reportPost = new Image (
		"/tootbook-resource/images/ui/report.png");
		reportPost.setTitle (DisplayText.REPORT_TOOT_TITLE);
		reportPost.setStyleName ("comment-icons");
		reportPost.addClickHandler (new ClickHandler () {
			@Override
			public void onClick (final ClickEvent event) {
				reportPost ();
			}
		});

		final VerticalPanel options = new VerticalPanel ();
		options.setSpacing (5);
		if (null != TootsBook.getUser ()) {
			if (post.getAuthor ().equals (TootsBook.getUser ())
					|| TootsBook.getUser ().getStaffLevel () > 0) {
				options.add (deletePost);
			}
			options.add (reportPost);
		}
		return options;
	}

	/**
	 * Perform an RPC call to the server to delete a post.
	 */
	protected void deletePost () {
		final BookServiceAsync service = TootsBook.initService ();

		if (Window
				.confirm ("Are you sure you want to delete this comment?")) {
			// Set up the callback object.
			final AsyncCallback <Post> callback = new AsyncCallback <Post> () {
				@Override
				public void onFailure (final Throwable caught) {
					TootsBook.showError (caught);
				}

				@Override
				public void onSuccess (final Post successfulComment) {
					puffDestroy (post.getContainingPanel ());
				}
			};

			// Make the call to the search service.
			service.deletePost (TootsBook.getUser ().getUserID (),
					post, callback);
			TootsBook.displayLoadingBar ();
		}
	}

	/**
	 * Destroy this comment with a puff animation.
	 * 
	 * @param panelToDestroy WRITEME
	 */
	protected void puffDestroy (final Panel panelToDestroy) {
		final Callback callBack = new Callback () {
			@Override

			public void execute () {
				panelToDestroy.removeFromParent ();
			}
		};
		Effect.puff (panelToDestroy,
				new EffectOption [] { new EffectOption ("afterFinish",
						callBack) });
	}

	/**
	 * Perform an RPC call to the server to report a user and delete the
	 * post that was reported
	 */
	protected void reportPost () {
		final BookServiceAsync service = TootsBook.initService ();

		if (Window
				.confirm ("Are you sure you want to report this Toot to a Moderator?")) {
			// Set up the callback object.
			final AsyncCallback <Post> callback = new AsyncCallback <Post> () {
				@Override
				public void onFailure (final Throwable caught) {
					TootsBook.showError (caught);
				}

				@Override
				public void onSuccess (final Post reportedComment) {
					TootsBook.showPopup (new ErrorMessage (Message
							.getReportMessage (post.getAuthor ()
									.getUserName ())));
					puffDestroy (post.getContainingPanel ());
				}
			};

			// Make the call to the search service.
			service.reportPost (post, callback);
		}
	}
}
