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

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.Nomenclator;
import org.starhope.appius.user.events.ActionHandler;
import org.starhope.appius.user.events.ActionMethod;
import org.starhope.appius.user.events.Quaestor;

/**
 * Services for Shade
 * 
 * @author brpocock@star-hope.org
 */
public class ShadeServices implements RunCommands {

// /**
	// * WRITEME: Document this type.
	// *
	// * @author brpocock@star-hope.org
	// */
	// final static class ShadeClickHandler implements ActionMethod {
	// @Override
	// public boolean acceptAction (final Room where,
	// final AbstractUser subject, final String verb,
	// final AbstractUser object, final String indirectObject,
	// final Object... trailer) {
	// if (null == trailer || trailer.length < 2) {
	// AppiusClaudiusCaecus.reportBug ("trailer incomplete: "
	// + LibMisc.stringify (trailer));
	// return false;
	// }
	// final String mods = (String) trailer [0];
	//
	// final Coord2D target = (Coord2D) trailer [1];
	// if (mods.contains ("S") && mods.contains ("1")) {
	// return shoot (where, subject, target);
	// }
	// return false;
	// }
	//
	// /**
	// * WRITEME: Document this method brpocock@star-hope.org
	// *
	// * @param where WRITEME
	// * @param subject WRITEME
	// * @param target WRITEME
	// * @return WRITEME
	// */
	// private boolean shoot (final Room where,
	// final AbstractUser subject, final Coord2D target) {
	// Geometry.updateUserPositioning (subject);
	// Coord3D shotLocation = subject.getLocation ();
	// shotLocation = shotLocation.add (0,
	// -ShadeServices.SHADE_SHOT_OFFSET_Y, 0);
	// Projectile p;
	// try {
	// p = new Projectile (Nomenclator.getDataRecord (
	// AvatarClass.class, ShadeServices.SHADE_SHOT),
	// where, shotLocation, System
	// .currentTimeMillis ()
	// + where.getLag (), target.toCoord3D (),
	// ShadeServices.SHADE_SHOT_SPEED, subject,
	// (AvatarClass) null, (AvatarClass) null);
	// } catch (NotFoundException e) {
	// AppiusClaudiusCaecus
	// .reportBug (
	// "Caught a NotFoundException in (ShadeServices)…acceptAction ",
	// e);
	// return false;
	// }
	// Quaestor.listen (new ActionHandler (where, p,
	// "projectile.hit", null,
	// new ShadeServices.ShadeHitHandler ()));
	// return true;
	// }
	// }

// /**
	// * WRITEME: Document this type.
	// *
	// * @author brpocock@star-hope.org
	// */
	// static final class ShadeHitHandler implements ActionMethod {
	// /**
	// * WRITEME: Document this brpocock@star-hope.org
	// */
	// private static final int SHIELD_DEATH_PARTICLE_LIFE = 750;
	// /**
	// * The array of shield items and their values
	// */
	// final private static Map <Integer, BigDecimal> lightshields = new
	// HashMap <Integer, BigDecimal> ();
	// {
	// ShadeHitHandler.lightshields.put (Integer.valueOf (2273),
	// BigDecimal.valueOf (87.5)); // white
	// ShadeHitHandler.lightshields.put (Integer.valueOf (2270),
	// BigDecimal.valueOf (75)); // pink
	// ShadeHitHandler.lightshields.put (Integer.valueOf (2271),
	// BigDecimal.valueOf (62.5)); // purple
	// ShadeHitHandler.lightshields.put (Integer.valueOf (2268),
	// BigDecimal.valueOf (50)); // blue
	// ShadeHitHandler.lightshields.put (Integer.valueOf (2269),
	// BigDecimal.valueOf (37.5)); // green
	// ShadeHitHandler.lightshields.put (Integer.valueOf (2274),
	// BigDecimal.valueOf (25)); // yellow
	// ShadeHitHandler.lightshields.put (Integer.valueOf (2140),
	// BigDecimal.valueOf (12.5)); // orange
	// ShadeHitHandler.lightshields.put (Integer.valueOf (2272),
	// BigDecimal.valueOf (0)); // red
	// }
	//
	// @Override
	// public boolean acceptAction (final Room where,
	// final AbstractUser projectile, final String verb,
	// final AbstractUser victim, final String indirectObject,
	// final Object... trailer) {
	// final Inventory victimInventory = victim.getInventory ();
	// try {
	// for (InventoryItem i : victimInventory
	// .getActiveItemsByType (Nomenclator
	// .getDataRecord (
	// InventoryItemType.class, 17))) {
	// if (ShadeHitHandler.lightshields
	// .containsKey (Integer.valueOf (i.getGenericItem ()
	// .getItemID ()))) {
	// return hitWithShield (projectile, victim, i);
	// }
	// }
	// } catch (NotFoundException e) {
	// AppiusClaudiusCaecus
	// .reportBug (
	// "Caught a NotFoundException in ShadeHitHandler.acceptAction ",
	// e);
	// }
	//
	// return hitWithoutShield (projectile, victim);
	// }
	//
	// /**
	// * WRITEME: Document this method brpocock@star-hope.org
	// *
	// * @param shot WRITEME
	// * @param victim WRITEME
	// * @return WRITEME
	// */
	// private boolean hitWithoutShield (final AbstractUser shot,
	// final AbstractUser victim) {
	// if (victim instanceof Toot) {
	// try {
	// final BigDecimal shadeHurtValue = BigDecimal
	// .valueOf ( -AppiusConfig.getIntOrDefault (
	// "com.tootsville.shade.hurts", 100));
	// ((Toot) victim)
	// .transferPeanuts (shadeHurtValue,
	// (Toot) Nomenclator
	// .getUserByLogin ("Shade"),
	// "shaddowFalls");
	// } catch (AlreadyExistsException e) {
	// AppiusClaudiusCaecus
	// .reportBug (
	// "Caught a AlreadyExistsException in ShadeHitHandler.hitWithoutShield ",
	// e);
	// } catch (JSONException e) {
	// AppiusClaudiusCaecus
	// .reportBug (
	// "Caught a JSONException in ShadeHitHandler.hitWithoutShield ",
	// e);
	// }
	// }
	// ((Projectile) shot).destroy ();
	// return true;
	//
	// }
	//
	// /**
	// * WRITEME: Document this method brpocock@star-hope.org
	// *
	// * @param shot WRITEME
	// * @param victim WRITEME
	// * @param shield WRITEME
	// * @return WRITEME
	// */
	// private boolean hitWithShield (final AbstractUser shot,
	// final AbstractUser victim, final InventoryItem shield) {
	// BigDecimal shieldHealth = shield.getHealth ();
	// final BigDecimal shadeHurtValue = BigDecimal
	// .valueOf (AppiusConfig.getIntOrDefault (
	// "com.tootsville.shade.hurts", 100));
	// final BigDecimal shieldReduction = new BigDecimal (
	// AppiusConfig.getConfigOrDefault (
	// "com.tootsville.lightshield.decrement",
	// "0.5")).multiply (shadeHurtValue);
	// shieldHealth = shieldHealth.subtract (shieldReduction);
	//
	// /*
	// * Find the best shield that is rated for equal or less than
	// * the current shield value
	// */
	// Entry <Integer, BigDecimal> bestShield = null;
	// for (Entry <Integer, BigDecimal> aShield :
	// ShadeHitHandler.lightshields
	// .entrySet ()) {
	// if (aShield.getValue ().compareTo (shieldHealth) <= 0) {
	// if (null != bestShield) {
	// if (bestShield.getValue ().compareTo (
	// aShield.getValue ()) > 0) {
	// continue;
	// }
	// }
	// bestShield = aShield;
	// }
	// }
	//
	// if (shieldHealth.compareTo (BigDecimal.ZERO) < 0
	// || null == bestShield) {
	// shield.destroy ();
	// victim
	// .acceptMessage ("Shade", "Shade",
	// "Haa haa haa! Shade's Shadow Bolt has destroyed your Lightshield!");
	// final double victimHeight = victim.getHeight () / 2;
	// try {
	// new Particle (
	// Nomenclator.getDataRecord (
	// AvatarClass.class,
	// ShadeServices.SHADE_SHOT),
	// victim.getRoom (),
	// victim.getLocation (),
	// System.currentTimeMillis () + 100,
	// victim.getLocation ().subtract (
	// new Coord3D (0, -victimHeight, 0)),
	// (victim.getHeight () / 2 /
	// ShadeHitHandler.SHIELD_DEATH_PARTICLE_LIFE),
	// ShadeHitHandler.SHIELD_DEATH_PARTICLE_LIFE);
	// } catch (NotFoundException e) {
	// AppiusClaudiusCaecus
	// .reportBug (
	// "Caught a NotFoundException in ShadeHitHandler.hitWithShield (boom particle)",
	// e);
	// }
	// return true;
	// }
	//
	// shield.setHealth (shieldHealth);
	// shield.setItemID (bestShield.getKey ().intValue ());
	//
	// return true;
	// }
	//
	// }

	/**
	 * WRITEME: Document this type.
	 *
	 * @author brpocock@star-hope.org
	 */
	final static class ShadeLoginHandler implements ActionMethod {
		@Override
		public boolean acceptAction (final Room where,
				final AbstractUser subject, final String verb,
				final AbstractUser object, final String indirectObject,
				final Object... trailer) {
			Nomenclator.getUserByLogin ("shade").setVariable (
					"noClick", "true");
			return false;
		}
	}

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

		private String savedWeather = "";
		private Room savedWeatherRoom = null;

		/**
		 * @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 (null != where){
				if ("part".equals(verb)) {
					if (null != savedWeatherRoom
							&& where.equals (savedWeatherRoom)) {
						where.setOverlay (savedWeather);
					} else {
						where.setOverlay ("");
					}
				} else if ("join".equals(verb)) {
					savedWeather = where.getOverlay();
					savedWeatherRoom  = where;
					where.setOverlay ("tendrils.swf");
				}
			}
			return false;
		}

	}

// /**
	// * The avatar for Shade's shots
	// */
	// static final String SHADE_SHOT = "Shade.Shot";
	// /**
	// * Speed of Shade's shots' travel
	// */
	// static final double SHADE_SHOT_SPEED = 200;
	// /**
	// * Offset Shade's projectile emission point by this height from
	// his
	// * shadow/pivot point
	// */
	// final static int SHADE_SHOT_OFFSET_Y = 320;

	/**
	 * WRITEME: Document this constructor brpocock@star-hope.org
	 */
	public ShadeServices () {
		// no op
	}

	/**
	 * @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 () {
		AppiusClaudiusCaecus.blather ("Enabling services for Shade…");
		final AbstractUser shade = Nomenclator.getUserByLogin ("Shade");
		Quaestor.listen (new ActionHandler (null, shade, "login", null,
				new ShadeLoginHandler ()));
		Quaestor.listen (new ActionHandler (null, shade, "join", null,
				new ShadeTendrilsHandler ()));
		Quaestor.listen (new ActionHandler (null, shade, "part", null,
				new ShadeTendrilsHandler ()));
		// Quaestor.listen (new ActionHandler (null, shade, "click",
		// null,
		// new ShadeClickHandler ()));
	}

}
