/**
 * Copyright © 2010, Res Interactive, LLC. All Rights Reserved.
 */
package com.tootsville.actions;

import java.math.BigInteger;

import org.json.JSONException;
import org.json.JSONObject;
import org.starhope.appius.game.AppiusClaudiusCaecus;
import org.starhope.appius.game.RunCommands;
import org.starhope.appius.game.Zone;
import org.starhope.appius.room.Room;
import org.starhope.appius.user.AbstractUser;
import org.starhope.appius.user.events.ActionHandler;
import org.starhope.appius.user.events.ActionMethod;
import org.starhope.appius.user.events.EventRecord;
import org.starhope.appius.user.events.Quaestor;

/**
 * WRITEME: Document this type.
 *
 * @author brpocock@star-hope.org
 *
 */
public class Fountain implements RunCommands {
	
	/**
	 * WRITEME: Document this type.
	 * 
	 * @author brpocock@star-hope.org
	 */
	static final class FountainStartHandler implements ActionMethod {

		/**
		 * WRITEME: Document this type.
		 *
		 * @author brpocock@star-hope.org
		 *
		 */
		final static class FountainPromptReplyHandler implements
				ActionMethod {
			/**
			 * fountain event in question
			 */
			final private EventRecord event;
			
			/**
			 * WRITEME: Document this constructor brpocock@star-hope.org
			 * 
			 * @param ev event
			 */
			public FountainPromptReplyHandler (final EventRecord ev) {
				event = ev;
			}

			/**
			 * @see org.starhope.appius.user.events.ActionMethod#acceptAction(org.starhope.appius.room.Room,
			 *      org.starhope.appius.user.AbstractUser,
			 *      java.lang.String,
			 *      org.starhope.appius.user.AbstractUser,
			 *      java.lang.String, java.lang.Object[])
			 */
			@Override
			public boolean acceptAction (
					final Room where,
					final AbstractUser subject,
					final String verb,
					final AbstractUser object,
					final String indirectObject,
					final Object... trailer) {
				if ("yes".equals (indirectObject)) {
					event.end (BigInteger.ZERO);
					return true;
				} else if ("no".equals (indirectObject)
						|| "close".equals (indirectObject)) {
					event.cancel ();
					return true;
				}
					subject.acceptErrorReply (
							"promptReply",
							"reply.notFound", null, where);
				return false;
			}
		}

		/**
		 * @see org.starhope.appius.user.events.ActionMethod#acceptAction(org.starhope.appius.room.Room,
		 *      org.starhope.appius.user.AbstractUser, java.lang.String,
		 *      org.starhope.appius.user.AbstractUser, java.lang.String,
		 *      java.lang.Object[])
		 */
		@Override
		public boolean acceptAction (final Room where,
				final AbstractUser subject, final String verb,
				final AbstractUser object, final String indirectObject,
				final Object... trailer) {
			if (trailer.length != 1
					|| ! (trailer [0] instanceof EventRecord)
					|| !indirectObject.startsWith ("fountain/")) {
				return false; // not interested
			}

			EventRecord ev = (EventRecord) trailer [0];
			final String id = toString () + "/"
					+ ev.getCacheableIdent ();

			Quaestor.listen (new ActionHandler (null, subject,
					"promptReply[" + id + "]", null,
					new FountainPromptReplyHandler (ev)));

			// FIXME: i18n
			try {
				JSONObject prompt = new JSONObject ();
				prompt.put ("label", "Fountain");
				prompt.put ("title", "Make a Wish?");
				JSONObject yesReply = new JSONObject ();
				yesReply.put ("label", "Make a wish!");
				yesReply.put ("label_en_US", "Make a wish!");
				yesReply.put ("type", "aff");
				JSONObject noReply = new JSONObject ();
				yesReply.put ("label", "No, thanks");
				yesReply.put ("label_en_US", "No, thanks");
				yesReply.put ("type", "neg");
				JSONObject replies = new JSONObject ();
				replies.put ("yes", yesReply);
				replies.put ("no", noReply);
				prompt.put ("replies", replies);
				prompt.put ("id", id);
				subject.acceptSuccessReply ("prompt", prompt, where);
			} catch (JSONException e) {
				AppiusClaudiusCaecus.reportBug (e);
			}
			return true;
		}

	}

	/**
	 * @see org.starhope.appius.game.RunCommands#newZone(org.starhope.appius.game.Zone)
	 */
	@Override
	public void newZone (final Zone z) {
		// no op
	}

	/**
	 * @see org.starhope.appius.game.RunCommands#run()
	 */
	@Override
	public void run () {
		try {
			Quaestor.listen (new ActionHandler (null, null,
					"event.start", null, new FountainStartHandler ()));
		} catch (final Exception e) {
			AppiusClaudiusCaecus.reportBug (e);
		}

	}

}
