/**
 * Copyright © 2009,2010 Res Interactive, LLC
 */
package com.tootsville.tootsbook.client.panel;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
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.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.tootsville.tootsbook.client.pages.ProfilePage;
import com.tootsville.tootsbook.client.util.DisplayText;
import com.tootsville.tootsbook.client.util.ShadowUser;
import com.tootsville.tootsbook.client.util.UserProfile;

/**
 * A pop-up panel for buddy lists? (WRITEME)…
 * 
 * @author <a href="mailto:twheys@gmail.com">Tim Heys</a>
 */
public class BuddyPanelPopup extends PopupPanel {

	/**
	 * WRITEME: Document this type.
	 *
	 * @author brpocock@star-hope.org
	 */
	private static final class ProfileIconClickHandler implements
	ClickHandler {
		/**
		 * WRITEME: Document this brpocock@star-hope.org
		 */
		private final ShadowUser buddy;

		/**
		 * WRITEME: Document this constructor brpocock@star-hope.org
		 *
		 * @param someBuddy WRITEME
		 */
		ProfileIconClickHandler (final ShadowUser someBuddy) {
			buddy = someBuddy;
		}

		/**
		 * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
		 */
		@Override
		public void onClick (final ClickEvent event) {
			History.newItem (buddy.getProfileURL ());
		}
	}

	/**
	 * WRITEME: Document this type.
	 *
	 * @author brpocock@star-hope.org
	 *
	 */
	private final static class SearchFieldBlurHandler implements
	BlurHandler {
		/**
		 * WRITEME: Document this brpocock@star-hope.org
		 */
		private final TextArea searchField;

		/**
		 * WRITEME: Document this constructor brpocock@star-hope.org
		 *
		 * @param blurryField WRITEME
		 */
		SearchFieldBlurHandler (final TextArea blurryField) {
			searchField = blurryField;
		}

		@Override
		public void onBlur (final BlurEvent event) {
			if ("".equals (searchField.getText ())) {
				searchField
				.setText (DisplayText.DEFAULT_SEARCH_TEXT);
				searchField.addStyleName ("greyed-text");
			}
		}
	}

	/**
	 * WRITEME: Document this type.
	 *
	 * @author brpocock@star-hope.org
	 *
	 */
	private final static class SearchFieldFocusHandler implements
	FocusHandler {
		/**
		 * WRITEME: Document this brpocock@star-hope.org
		 */
		private final TextArea searchField;

		/**
		 * WRITEME: Document this constructor brpocock@star-hope.org
		 *
		 * @param yourNewField WRITEME
		 */
		SearchFieldFocusHandler (final TextArea yourNewField) {
			searchField = yourNewField;
		}

		/**
		 *
		 * @see com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event.dom.client.FocusEvent)
		 */
		@Override
		public void onFocus (final FocusEvent event) {
			if (DisplayText.DEFAULT_SEARCH_TEXT.equals (searchField
					.getText ())) {
				searchField.setText ("");
				searchField.removeStyleName ("greyed-text");
			}
		}
	}

	/**
	 * WRITEME: Document this type.
	 *
	 * @author brpocock@star-hope.org
	 *
	 */
	private final class SearchFieldKeyPressHandler implements
	KeyPressHandler {
		/**
		 * WRITEME: Document this brpocock@star-hope.org
		 */
		private final TextArea searchField;

		/**
		 * WRITEME: Document this constructor brpocock@star-hope.org
		 * 
		 * @param typoField WRITEME
		 */
		SearchFieldKeyPressHandler (final TextArea typoField) {
			searchField = typoField;
		}

		@Override
		public void onKeyPress (final KeyPressEvent event) {
			if (event.getCharCode () == KeyCodes.KEY_ENTER) {
				searchBuddies (searchField.getText ());
				searchField.cancelKey ();
				searchField
				.setText (DisplayText.DEFAULT_SEARCH_TEXT);
				searchField.addStyleName ("greyed-text");
			}
		}
	}

	/**
	 * The primary ui component.
	 */
	Box buddyBox;

	/**
	 * WRITEME
	 */
	private VerticalPanel buddyList;

	/**
	 * WRITEME
	 */
	// ClickHandler closeOnClick = new ClickHandler () {
	// @Override
	// public void onClick (final ClickEvent event) {
	// BuddyPanelPopup.this.hide ();
	// }
	// };

	/**
	 * WRITEME
	 */
	private final UserProfile user;

	/**
	 * @param newUser WRITEME
	 */
	public BuddyPanelPopup (final UserProfile newUser) {
		super (true);
		setAnimationEnabled (true);
		setModal (true);
		user = newUser;

		buildList ();
		setWidget (buddyBox);
	}

	/**
	 * <pre>
	 * twheys@gmail.com Jan 18, 2010
	 * </pre>
	 * 
	 * TO buildList WRITEME...
	 */
	private void buildList () {
		buddyList = new VerticalPanel ();
		buddyList.setWidth ("490px");

		final VerticalPanel buddyListAndSearch = new VerticalPanel ();
		final SimplePanel buddyListContainer = new SimplePanel ();
		buddyListAndSearch.setWidth ("520px");
		buddyListContainer.setWidget (buddyListAndSearch);
		buddyListContainer.setStyleName ("buddy-view-container");
		buddyListAndSearch
		.setHorizontalAlignment (HasHorizontalAlignment.ALIGN_CENTER);
		// buddyList.getElement ().setAttribute ("width", "auto");
		// buddyList.setBorderWidth (1);

		buddyBox = new Box (buddyListContainer, user.getUserName ()
				+ "'s Buddy List", "Close", new ClickHandler () {
			@Override
			public void onClick (final ClickEvent event) {
				BuddyPanelPopup.this.hide ();
			}
		});
		buddyBox.pack ();

		buddyListAndSearch.add (getSearchBar ());
		buddyListAndSearch.add (buddyList);
		populateList ("");
	}

	/**
	 * <pre>
	 * twheys@gmail.com Jan 18, 2010
	 * </pre>
	 * 
	 * TO getBuddyView WRITEME...
	 * 
	 * @param buddy WRITEME
	 * @return WRITEME
	 */
	private SimplePanel getBuddyView (final ShadowUser buddy) {
		final HorizontalPanel buddyView = new HorizontalPanel ();
		buddyView.setSize ("100%", "75px");
		buddyView
		.setVerticalAlignment (HasVerticalAlignment.ALIGN_MIDDLE);

		final Anchor buddyName = new Anchor (buddy.getUserName (), "#"
				+ buddy.getProfileURL ());
		buddyName.setStyleName ("primary-text big-text");
		buddyName
		.setHorizontalAlignment (HasHorizontalAlignment.ALIGN_LEFT);

		final Image profileIcon = new Image (
				"/tootbook-resource/images/ui/profile.png");
		profileIcon.setTitle (buddy.getUserName () + "'s Profile");
		profileIcon.setStyleName ("buddy-view-icon");
		profileIcon
		.addClickHandler (new ProfileIconClickHandler (buddy));

		final FocusPanel icon = buddy.getIcon ();

		buddyView.add (icon);
		buddyView.add (buddyName);
		buddyView.add (profileIcon);

		buddyView.setCellWidth (icon, "65px");
		buddyView.setCellWidth (buddyName, "400px");
		buddyView.setCellWidth (profileIcon, "50px");

		final SimplePanel buddyItemContainer = new SimplePanel ();
		buddyItemContainer.add (buddyView);
		buddyItemContainer
		.setStyleName ("buddy-view-item box-border box-color");

		return buddyItemContainer;
	}

	/**
	 * <pre>
	 * twheys@gmail.com Jan 18, 2010
	 * </pre>
	 * 
	 * TO getSearchBar WRITEME...
	 * 
	 * @return WRITEME
	 */
	private HorizontalPanel getSearchBar () {
		final TextArea searchField = new TextArea ();
		searchField.setStyleName ("buddy-search-bar");
		searchField.setVisibleLines (1);
		searchField.setText (DisplayText.DEFAULT_SEARCH_TEXT);
		searchField.addStyleName ("greyed-text");

		searchField.addFocusHandler (new SearchFieldFocusHandler (
				searchField));
		searchField.addBlurHandler (new SearchFieldBlurHandler (
				searchField));
		searchField.addKeyPressHandler (new SearchFieldKeyPressHandler (searchField));
		searchField.setFocus (false);

		final Button searchButton = new Button ("");
		searchButton.setStyleName ("buddy-list-search-button");

		searchButton.addClickHandler (new ClickHandler () {

			@Override
			public void onClick (final ClickEvent event) {
				searchBuddies (searchField.getText ());
				searchField.setText (DisplayText.DEFAULT_SEARCH_TEXT);
				searchField.addStyleName ("greyed-text");
			}
		});

		final HTML space = new HTML ("");

		final HorizontalPanel searchPanel = new HorizontalPanel ();
		searchPanel.setSize ("490px", "24px");
		searchPanel
		.setVerticalAlignment (HasVerticalAlignment.ALIGN_MIDDLE);
		searchPanel.add (space);
		searchPanel.add (searchField);
		searchPanel.add (searchButton);

		searchPanel.setCellWidth (space, "276px");
		searchPanel.setCellWidth (searchField, "200px");
		searchPanel.setCellWidth (searchButton, "24px");

		return searchPanel;

	}

	/**
	 * <pre>
	 * twheys@gmail.com Jan 19, 2010
	 * </pre>
	 * 
	 * TO populateList WRITEME...
	 * 
	 * @param searchContext WRITEME
	 */
	private void populateList (final String searchContext) {
		buddyList.clear ();
		for (final ShadowUser buddy : user.getBuddies ()) {
			if (null == buddy || ProfilePage.getUser ().equals (buddy)) {
				// do nothing
			} else {
				if ("".equals (searchContext)
						|| buddy.getUserName ()
						.contains (searchContext)) {
					buddyList.add (getBuddyView (buddy));
				}
			}
		}
	}

	/**
	 * <pre>
	 * twheys@gmail.com Jan 18, 2010
	 * </pre>
	 * 
	 * TO searchBuddies WRITEME...
	 * 
	 * @param searchContext WRITEME
	 */
	protected void searchBuddies (final String searchContext) {
		if ( !DisplayText.DEFAULT_SEARCH_TEXT.equals (searchContext)
				&& !"".equals (DisplayText.DEFAULT_SEARCH_TEXT)) {
			populateList (searchContext);
		} else {
			populateList ("");
		}
	}
}
