/**
 *
 */
package com.tootsville.tootsbook.client.util;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.IsSerializable;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * WRITEME: Document this type. twheys@gmail.com Jan 19, 2010
 * 
 * @author <a href="mailto:twheys@gmail.com@resinteractive.com">Tim
 *         Heys</a>
 */
public class ShadowUser implements IsSerializable {
	/**
	 * relative path to where image icons are stored
	 */
	private static final String imageURL = "/tootbook-resource/themes/icons/";

	/**
	 * relative path to profile pages
	 */
	private static final String profileURL = "profile?id=";
	
	/**
	 * <pre>
	 * twheys@gmail.com Dec 18, 2009
	 * </pre>
	 * 
	 * TO getAvatarBgURLForItemID WRITEME...
	 * 
	 * @param itemID WRITEME twheys@gmail.com
	 * @return WRITEME twheys@gmail.com
	 */
	public static String getAvatarBgURLForItemID (final String itemID) {
		return "/tootbook-resource/themes/avatar-bg/" + itemID + ".png";
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Dec 18, 2009
	 * </pre>
	 * 
	 * TO getBoxStyleURLForItemID WRITEME...
	 * 
	 * @param itemID WRITEME twheys@gmail.com
	 * @return WRITEME twheys@gmail.com
	 */
	public static String getBoxStyleURLForItemID (final String itemID) {
		// default getter (twheys@gmail.com, Oct 22, 2009)
		return "/tootbook-resource/themes/box-styles/" + itemID
				+ "/style.css";
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Dec 18, 2009
	 * </pre>
	 * 
	 * TO getIconWithImage WRITEME...
	 * 
	 * @param user WRITEME twheys@gmail.com
	 * @param itemID WRITEME twheys@gmail.com
	 * @return WRITEME twheys@gmail.com
	 */
	public static FocusPanel getIconForUserWithItemID (
			final ShadowUser user,
			final String itemID) {
		final Image buddyImg = new Image (
ShadowUser
				.getImageURLForItemID (itemID));
		buddyImg.setSize ("50px", "50px");
		buddyImg.setTitle (user.getUserName ());
		buddyImg.setStyleName ("buddy-image");

		final VerticalPanel buddyBox = new VerticalPanel ();

		final Anchor buddyNameLink = new Anchor (
				user.getShortUserName (), true, "#"
						+ user.getProfileURL ());
		buddyNameLink.setStyleName ("secondary-text");

		buddyBox.setHorizontalAlignment (HasHorizontalAlignment.ALIGN_CENTER);
		buddyBox.add (buddyImg);
		buddyBox.add (buddyNameLink);
		buddyBox.setSize ("65px", "auto");
		// buddyBox.setBorderWidth (1);

		final FocusPanel buddyNode = new FocusPanel ();
		buddyNode.addClickHandler (new ClickHandler () {
			@Override
			public void onClick (final ClickEvent event) {
				History.newItem (user.getProfileURL ());
			}
		});
		buddyNode.add (buddyBox);
		buddyNode.setStyleName ("buddyNode");
		return buddyNode;
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Dec 17, 2009
	 * </pre>
	 * 
	 * TO getImageURLForItemID WRITEME...
	 * 
	 * @param itemID WRITEME twheys@gmail.com
	 * @return WRITEME twheys@gmail.com
	 */
	private static String getImageURLForItemID (final String itemID) {
		return URL.encode (ShadowUser.imageURL + itemID + ".png");
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Dec 18, 2009
	 * </pre>
	 * 
	 * TO getPageStyleURLForItemID WRITEME...
	 * 
	 * @param itemID WRITEME twheys@gmail.com
	 * @return WRITEME twheys@gmail.com
	 */
	public static String getPageStyleURLForItemID (final String itemID) {
		return "/tootbook-resource/themes/page-bg/" + itemID
				+ "/style.css";
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Dec 18, 2009
	 * </pre>
	 * 
	 * TO getTitleStyleURLForItemID WRITEME...
	 * 
	 * @param itemID WRITEME twheys@gmail.com
	 * @return WRITEME twheys@gmail.com
	 */
	public static String getTitleStyleURLForItemID (final String itemID) {
		return "/tootbook-resource/themes/title-bg/" + itemID
				+ "/style.css";
	}

	/**
	 * WRITEME twheys@gmail.com
	 */
	private String icon = "icon1";

	/**
	 * WRITEME twheys@gmail.com
	 */
	int userID = 0;

	/**
	 * WRITEME twheys@gmail.com
	 */
	private String userName = "";

	/**
	 * default constructor, creates an empty object
	 */
	public ShadowUser () {
		// do nothing
	}
	
	/**
	 * WRITEME: Document this field. twheys@gmail.com Dec 11, 2009
	 * 
	 * @param newID WRITEME twheys@gmail.com
	 * @param newUserName WRITEME twheys@gmail.com
	 */
	public ShadowUser (final int newID, final String newUserName) {
		userID = newID;
		userName = newUserName;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals (final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass () != obj.getClass ()) {
			return false;
		}
		final ShadowUser other = (ShadowUser) obj;
		if (icon == null) {
			if (other.icon != null) {
				return false;
			}
		} else if ( !icon.equals (other.icon)) {
			return false;
		}
		if (userID != other.userID) {
			return false;
		}
		if (userName == null) {
			if (other.userName != null) {
				return false;
			}
		} else if ( !userName.equals (other.userName)) {
			return false;
		}
		return true;
	}

	/**
	 * @return a VerticalPanel containing an image of the buddy with the
	 *         buddy's name below it.
	 */
	public FocusPanel getIcon () {
		return ShadowUser.getIconForUserWithItemID (this, icon);
	}

	/**
	 * @return RELATIVE URL TO BUDDY PROFILE
	 */
	public String getProfileURL () {
		return ShadowUser.profileURL + userID;
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Feb 3, 2010
	 * </pre>
	 * 
	 * TO getProfileLink WRITEME...
	 * 
	 * @return WRITEME twheys@gmail.com
	 */
	public String getPublicProfileLink () {
		return "www.TootsBook.com/" + userName;

	}

	/**
	 * @return buddyName or the first 6 characters of the buddies name
	 *         with an elipsis if the buddy name is longer than 7
	 *         characters
	 */
	public String getShortUserName () {
		if (6 < userName.length ()) {
			return userName.substring (0, 5) + "..";
		}
		return userName;
	}
	
	/**
	 * <pre>
	 * twheys@gmail.com Jan 19, 2010
	 * </pre>
	 * 
	 * TO getUserID WRITEME...
	 * 
	 * @return WRITEME twheys@gmail.com
	 */
	public int getUserID () {
		// default getter (twheys@gmail.com, Oct 19, 2009)
		return userID;
	}

	/**
	 * @return the userName
	 */
	public String getUserName () {
		return userName;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode () {
		final int prime = 31;
		int result = 1;
		result = prime * result + (icon == null ? 0 : icon.hashCode ());
		result = prime * result + userID;
		result = prime * result
				+ (userName == null ? 0 : userName.hashCode ());
		return result;
	}

	/**
	 * @param newIcon WRITEME twheys@gmail.com
	 */
	public void setIcon (final String newIcon) {
		icon = newIcon;
	}

	/**
	 * @param newUserID the userID to set
	 */
	public void setUserID (final int newUserID) {
		userID = newUserID;
	}

	/**
	 * @param newUserName the userName to set
	 */
	public void setUserName (final String newUserName) {
		userName = newUserName;
	}

}
