package com.tootsville.tootsbook.client.panel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.tootsville.tootsbook.client.TootsBook;

/**
 * WRITEME: Document this type. twheys@gmail.com Jan 19, 2010
 * 
 * @author <a href="mailto:twheys@gmail.com@resinteractive.com">Tim
 *         Heys</a>
 * 
 */
public class ErrorMessage extends PopupPanel {

	/**
	 * 
	 */
	private Box errorBox;

	/**
	 * 
	 */
	private VerticalPanel errorPanel;

	/**
	 * 
	 */
	private final String message;

	/**
	 * 
	 */
	private final String title;

	/**
	 * <pre>
	 * Creates an Error message box. This popup will not be centered or visible
	 * by default. Use
	 * {@link PopupPanel#setPopupPositionAndShow(PositionCallback)} or
	 * {@link PopupPanel#center()} to display it. Default handling of popups is
	 * to use {@link TootsBook#showPopup(PopupPanel)} like below:
	 * </pre>
	 * 
	 * <code>
	 * TootsBook.showPopup (new ErrorMessage ("Error Message to be displayed"));
	 * </code>
	 * 
	 * @param popupMessage The message to be displayed.
	 */
	public ErrorMessage (final String popupMessage) {
		this ("Error!", popupMessage);
	}

	/**
	 * <pre>
	 * Creates an Error message box. This popup will not be centered or visible
	 * by default. Use
	 * {@link PopupPanel#setPopupPositionAndShow(PositionCallback)} or
	 * {@link PopupPanel#center()} to display it. Default handling of popups is
	 * to use {@link TootsBook#showPopup(PopupPanel)} like below:
	 * </pre>
	 * 
	 * <code>
	 * TootsBook.showPopup (new ErrorMessage ("Error", "Error Message to be displayed"));
	 * </code>
	 * 
	 * @param popupTitle The title to be displayed in the header
	 * @param popupMessage The message to be displayed.
	 */
	public ErrorMessage (final String popupTitle,
			final String popupMessage) {
		super (true);
		setAnimationEnabled (true);
		setModal (true);
		title = popupTitle;
		message = popupMessage;
		buildErrorBox ();
		setWidget (errorBox);
		setStyleName ("popup-overlay");
	}

	/**
	 * <pre>
	 * twheys@gmail.com Jan 19, 2010
	 * </pre>
	 * 
	 * TO buildErrorBox WRITEME...
	 * 
	 */
	private void buildErrorBox () {
		errorPanel = new VerticalPanel ();

		errorBox = new Box (errorPanel, title);
		errorBox.pack ();

		final Button submit = getButton ();

		// TODO: unused?
		// final Image oops = new Image (
		// "/tootbook-resource/images/ui/oops.png");

		final Label errorMessage = new Label (message);
		errorMessage.setStyleName ("primary-text");

		errorPanel
				.setHorizontalAlignment (HasHorizontalAlignment.ALIGN_CENTER);
		errorPanel
				.setVerticalAlignment (HasVerticalAlignment.ALIGN_MIDDLE);
		errorPanel.setSpacing (5);
		// errorPanel.add (oops);
		errorPanel.add (errorMessage);
		errorPanel.add (submit);
	}

	/**
	 * Creates a decorated button to confirm the error message here.
	 * 
	 * @return a submit button which hides this window.
	 */
	private Button getButton () {
		final Button submit = new Button ("OK");
		submit.addClickHandler (new ClickHandler () {
			@Override
			public void onClick (final ClickEvent event) {
				ErrorMessage.this.hide ();
			}
		});
		return submit;
	}
}
