package com.tootsville.tootsbook.client.panel;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
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 ToolPanel extends VerticalPanel {
	
	/**
	 * WRITEME
	 */
	private final Image ad = new Image (
			"/tootbook-resource/images/header/banner.png");
	
	/**
	 * Anchor
	 */
	private final HorizontalPanel adPanel = new HorizontalPanel ();
	
	/**
	 * WRITEME
	 */
	private Hyperlink home;
	
	/**
	 * WRITEME
	 */
	private final HorizontalPanel linkPanel = new HorizontalPanel ();

	/**
	 * WRITEME
	 */
	private Hyperlink log;
	
	/**
	 * WRITEME
	 */
	private final Image logoTootsBook = new Image (
			"/tootbook-resource/images/header/text-tootbook.png");
	
	/**
	 * WRITEME
	 */
	private final Image logoTootsville = new Image (
			"/tootbook-resource/images/header/toots-logo.png");
	
	/**
	 * WRITEME
	 */
	private Hyperlink profile;

	/**
	 * WRITEME
	 */
	private Hyperlink settings;

	/**
	 * WRITEME
	 */
	public ToolPanel () {
		buildAdPanel ();
		buildLinkPanel ();
		add (adPanel);
		add (linkPanel);
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Jan 19, 2010
	 * </pre>
	 * 
	 * TO buildAdPanel WRITEME...
	 */
	private void buildAdPanel () {
		adPanel.setStyleName ("ad-bar-contents");
		adPanel.add (logoTootsville);
		final Anchor aWrap = new Anchor ("",
				"http://mayorscorneroftootsville.blogspot.com/",
				"_blank");
		aWrap.getElement ().appendChild (ad.getElement ());
		adPanel.add (aWrap);
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Jan 19, 2010
	 * </pre>
	 * 
	 * TO buildLinkPanel WRITEME...
	 */
	private void buildLinkPanel () {
		home = new Hyperlink ("Home", TootsBook.PAGE_URL_HOME);
		profile = new Hyperlink ("My Profile",
				TootsBook.PAGE_URL_PROFILE);
		settings = new Hyperlink ("Settings",
				TootsBook.PAGE_URL_SETTINGS);
		log = new Hyperlink ("Log In", TootsBook.PAGE_URL_LOGIN);

		home.setStyleName ("link");
		profile.setStyleName ("link");
		settings.setStyleName ("link");
		log.setStyleName ("link");
		reloadLinks ();

		HTMLPanel space = new HTMLPanel ("");
		space.setStyleName ("space");
		space.setWidth ("5px");
		linkPanel.add (space);

		linkPanel.setStyleName ("tool-bar-contents");
		linkPanel.add (logoTootsBook);
		linkPanel.setCellWidth (logoTootsBook, "125px");

		space = new HTMLPanel ("");
		space.setStyleName ("space");
		space.setWidth ("20px");
		linkPanel.add (space);

		linkPanel
				.setHorizontalAlignment (HasHorizontalAlignment.ALIGN_CENTER);
		linkPanel.add (home);
		linkPanel.add (profile);
		linkPanel.add (settings);

		space = new HTMLPanel ("");
		space.setStyleName ("space");
		space.setWidth ("200px");
		linkPanel.add (space);

		linkPanel.add (log);

		space = new HTMLPanel ("");
		space.setStyleName ("space");
		space.setWidth ("20px");
		linkPanel.add (space);

		linkPanel.setSpacing (0);
		linkPanel.setBorderWidth (0);
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Feb 10, 2010
	 * </pre>
	 * 
	 * TO highlightHome add the style link-highlighted to the home
	 * hyperlink div
	 */
	public void highlightHome () {
		home.addStyleName ("link-highlighted");
		profile.removeStyleName ("link-highlighted");
		settings.removeStyleName ("link-highlighted");
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Feb 10, 2010
	 * </pre>
	 * 
	 * TO highlightProfile add the style link-highlighted to the profile
	 * hyperlink div
	 */
	public void highlightProfile () {
		home.removeStyleName ("link-highlighted");
		profile.addStyleName ("link-highlighted");
		settings.removeStyleName ("link-highlighted");
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Feb 10, 2010
	 * </pre>
	 * 
	 * TO highlightSettings add the style link-highlighted to the
	 * settings hyperlink div
	 */
	public void highlightSettings () {
		home.removeStyleName ("link-highlighted");
		profile.removeStyleName ("link-highlighted");
		settings.addStyleName ("link-highlighted");
	}

	/**
	 * Reloads links that can change. Currently this only cares about
	 * the Log in/Log out links
	 */
	public void reloadLinks () {
		if (TootsBook.isLoggedIn ()) {
			log.setText ("Log Out "
					+ TootsBook.getUser ().getShortUserName ());
			log.setTargetHistoryToken (TootsBook.PAGE_URL_LOGOUT);
		} else {
			log.setText ("Log In");
			log.setTargetHistoryToken (TootsBook.PAGE_URL_LOGIN);
		}
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Feb 10, 2010
	 * </pre>
	 * 
	 * TO highlightProfile add the style link-highlighted to the profile
	 * hyperlink div
	 */
	public void removeHighlights () {
		home.removeStyleName ("link-highlighted");
		profile.removeStyleName ("link-highlighted");
		settings.removeStyleName ("link-highlighted");
	}
}
