package com.tootsville.joshua.client.details;

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.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.tootsville.gwt.joshua.client.Joshua;
import com.tootsville.gwt.joshua.client.service.JoshuaServiceAsync;
import com.tootsville.gwt.joshua.client.util.Parent;

/**
 * 
 * @author Tim
 * 
 */
public class ParentDetails extends HorizontalPanel {

	private static final int SPACING = 10;
	VerticalPanel accountDetailsPanel = new VerticalPanel ();
	VerticalPanel actionsPanel = new VerticalPanel ();
	Button associateChild = new Button ("Associate Child Account");
	VerticalPanel childPanel = new VerticalPanel ();
	Button editAccount = new Button ("Edit Account");
	ClickHandler goToUser = new ClickHandler () {
		@Override
		public void onClick (final ClickEvent event) {
			final String user = ((Hyperlink) event.getSource ())
					.getText ();
			main.search (user);
		}
	};
	Joshua main;
	final Parent parent;
	Button remindPassword = new Button ("Remind Password");
	Button resetPassword = new Button ("Reset Password");
	Label userID = new Label ("");
	Label userName = new Label ("");

	public ParentDetails (final Joshua main1, final Parent parent1) {
		main = main1;
		parent = parent1;

		final VerticalPanel leftPanel = new VerticalPanel ();

		buildAccountDetailsPanel ();
		buildActionsPanel ();
		buildChildPanel ();

		leftPanel.add (accountDetailsPanel);
		leftPanel.add (actionsPanel);
		leftPanel.add (childPanel);

		add (leftPanel);

		Window.setStatus ("Done");
	}

	protected void associateChild () {
		final String newUserName = Window.prompt (
				"Please enter a username.", "");

		if (null == newUserName)
			return;

		final JoshuaServiceAsync service = Joshua.initService ();

		// Set up the callback object.
		final AsyncCallback <Void> callback = new AsyncCallback <Void> () {
			@Override
			public void onFailure (final Throwable caught) {
				Window.alert (caught.getMessage ());
			}

			@Override
			public void onSuccess (final Void result) {
				Window.alert ("Parent has been set.");
				main.search (String.valueOf (parent.getMail ()));
			}
		};

		// Make the call to the search service.
		service.associateChild (parent.getMail (), newUserName,
				callback);
	}

	private void buildAccountDetailsPanel () {
		Window.setStatus ("Building account details");

		accountDetailsPanel.add (new Label ("eMail:    "
				+ parent.getMail ()));
		accountDetailsPanel.add (new Label ("User ID: "
				+ parent.getUserID ()));
		accountDetailsPanel.add (new Label ("Password:    "
				+ parent.getPassword ()));

		accountDetailsPanel.setStyleName ("detailsBox");

	}

	private void buildActionsPanel () {

		editAccount.addClickHandler (new ClickHandler () {
			@Override
			public void onClick (final ClickEvent event) {
				editAccount ();
			}
		});

		resetPassword.addClickHandler (new ClickHandler () {
			@Override
			public void onClick (final ClickEvent event) {
				resetPassword ();
			}
		});

		remindPassword.addClickHandler (new ClickHandler () {
			@Override
			public void onClick (final ClickEvent event) {
				remindPassword ();
			}
		});

		associateChild.addClickHandler (new ClickHandler () {
			@Override
			public void onClick (final ClickEvent event) {
				associateChild ();
			}
		});

		actionsPanel.add (editAccount);
		actionsPanel.add (resetPassword);
		actionsPanel.add (remindPassword);
		actionsPanel.add (associateChild);
		actionsPanel.setSpacing (ParentDetails.SPACING);
		actionsPanel.setStyleName ("detailsBox");
	}

	private void buildChildPanel () {
		Window.setStatus ("Building child list");

		final FlexTable childrenTable = new FlexTable ();
		int i = 0;
		childrenTable.setText (i++ , 0, "Child List:");
		for (final String child : parent.getChildren ()) {
			final Hyperlink childLink = new Hyperlink (child, "");
			childLink.addClickHandler (goToUser);
			childrenTable.setWidget (i++ , 0, childLink);
		}
		childrenTable.setStyleName ("buddyListTable");
		childrenTable.setCellPadding (0);
		childrenTable.setCellSpacing (0);
		childrenTable.setHeight ("auto");

		childPanel.add (childrenTable);
		childPanel.setStyleName ("detailsBox buddyListTable");
	}

	protected void editAccount () {
		Window.alert ("This feature is not supported yet.");
	}

	protected void remindPassword () {
		final JoshuaServiceAsync service = Joshua.initService ();

		// Set up the callback object.
		final AsyncCallback <Void> callback = new AsyncCallback <Void> () {
			@Override
			public void onFailure (final Throwable caught) {
				Window
						.alert ("Unable to send user's password, please retry your last action.");
			}

			@Override
			public void onSuccess (final Void result) {
				Window
						.alert ("The user's password has been e-mailed to them.");
			}
		};

		// Make the call to the search service.
		service.remindPassword (parent.getUserID (), false, callback);
	}

	protected void resetPassword () {
		Window.alert ("This feature is not supported yet.");
	}

}
