/**
 * <p>
 * Copyright © 2010, Bruce-Robert Pocock &amp; Edward Winkelman &amp;
 * Res Interactive, LLC
 * </p>
 * <p>
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * </p>
 * <p>
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * </p>
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <a href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses/</a>.
 * </p>
 *
 * @author brpocock@star-hope.org
 * @author edward.winkelman@gmail.com
 */
package com.tootsville.game;

import org.json.JSONException;
import org.json.JSONObject;
import org.starhope.appius.except.GameLogicException;
import org.starhope.appius.except.NotFoundException;
import org.starhope.appius.game.AppiusClaudiusCaecus;
import org.starhope.appius.game.GameEvent;
import org.starhope.appius.game.GameStateFlag;
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.Quaestor;

/**
 * @author edward.winkelman@gmail.com
 */
public class HauntedOrgan extends GameEvent {

	/**
	 * WRITEME edward.winkelman@gmail.com Oct 11, 2010
	 * 
	 * @author edward.winkelman@gmail.com
	 */
	private final class HauntedOrganClickButton implements
	ActionMethod {
		/**
		 * WRITEME edward.winkelman@gmail.com Oct 11, 2010
		 */
		public HauntedOrganClickButton () {
			// No op
		}

		/**
		 * @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 (getLastClicked() + 2000 < System
					.currentTimeMillis ()) {
				setLastClicked (System.currentTimeMillis ());
				if (isDark()) {
					getRoom ().setVariable (
							"anim~organRoom",
					"default");
					setDark (false);
				} else {
					getRoom ().setVariable (
							"anim~organRoom",
					"dark");
					setDark (true);
					final JSONObject soundKey = new JSONObject ();
					try {
						soundKey.put ("url",
						"DarkEvilLaugh.mp3");
					} catch (final JSONException e) {
						AppiusClaudiusCaecus
						.reportBug (
								"Caught a JSONException in DoorHandler.acceptAction/playSound",
								e);
					}
					getRoom ().broadcast ("playSound",
							soundKey);
				}
			}
			return false;
		}
	}

	/**
	 * If the room is dark or not
	 */
	private transient boolean dark = false;

	/**
	 * Click cooldown so that people can't spam
	 */
	private transient long lastClicked = System.currentTimeMillis ();

	/**
	 * Haunted organ room object
	 */
	private transient final Room room;

	/**
	 * WRITEME edward.winkelman@gmail.com Sep 28, 2010
	 * 
	 * @param z Zone in which haunted organ is placed
	 * @throws NotFoundException WRITEME
	 * @throws GameLogicException WRITEME
	 */
	public HauntedOrgan (final Zone z) throws NotFoundException,
	GameLogicException {
		super (z, 'S');

		gameState = GameStateFlag.GAME_SOLO;
		room = z.getRoom ("hauntedOrgan");
		if (null == room) {
			AppiusClaudiusCaecus
			.reportBug ("can't find the haunted organ");
			throw new GameLogicException (
					"Can't find the haunted organ",
					this, z);
		}
		rooms.add (room);
		room.add (this);
		Quaestor.listen (new ActionHandler (room, null, "click", null,
				"item0", new HauntedOrganClickButton ()));
	}

	/**
	 * @see org.starhope.appius.room.RoomListener#acceptGameAction(org.starhope.appius.user.AbstractUser,
	 *      org.json.JSONObject)
	 */
	@Override
	public void acceptGameAction (final AbstractUser u, final JSONObject action) {
		// No op

	}

	/**
	 * @see org.starhope.appius.room.RoomListener#acceptOutOfBandMessage(org.starhope.appius.user.AbstractUser,
	 *      org.starhope.appius.room.Room, org.json.JSONObject)
	 */
	@Override
	public void acceptOutOfBandMessage (final AbstractUser sender,
			final Room inRoom,
			final JSONObject body) {
		// No op

	}

	/**
	 * @see org.starhope.appius.room.RoomListener#acceptPublicMessage(org.starhope.appius.user.AbstractUser,
	 *      java.lang.String)
	 */
	@Override
	public void acceptPublicMessage (final AbstractUser from, final String message) {
		// No op

	}

	/**
	 * @see org.starhope.appius.room.RoomListener#acceptUserAction(org.starhope.appius.room.Room,
	 *      org.starhope.appius.user.AbstractUser)
	 */
	@Override
	public void acceptUserAction (final Room r, final AbstractUser u) {
		// No op
	}

	/**
	 * @see org.starhope.appius.game.GameEvent#destroySelf()
	 */
	@Override
	public void destroySelf () {
		super.destroySelf ();
		AppiusClaudiusCaecus.remove (this);
	}

	/**
	 * @see org.starhope.appius.game.GameEvent#getGameEventPrefix()
	 */
	@Override
	public String getGameEventPrefix () {
		return "hauntedOrgan";
	}

	/**
	 * @return the lastClicked
	 */
	public long getLastClicked () {
		return lastClicked;
	}

	/**
	 * @see org.starhope.appius.util.HasName#getName()
	 */
	@Override
	public String getName () {
		return "Haunted Organ in "
		+ rooms.first ().getZone ().getName ();
	}

	/**
	 * @return the dark
	 */
	public boolean isDark () {
		return dark;
	}
	
	/**
	 * @param darkNow the dark to set
	 */
	public void setDark (final boolean darkNow) {
		dark = darkNow;
	}
	
	/**
	 * @param thisClicked the lastClicked to set
	 */
	public void setLastClicked (final long thisClicked) {
		lastClicked = thisClicked;
	}


}
