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

import java.math.BigDecimal;

import org.starhope.appius.except.NotFoundException;
import org.starhope.appius.game.AppiusClaudiusCaecus;
import org.starhope.appius.game.DamageArea;
import org.starhope.appius.game.DamageTypeRanks;
import org.starhope.appius.game.inventory.InventoryItem;
import org.starhope.appius.game.inventory.effects.SimpleRangedWeapon;
import org.starhope.appius.game.npc.DamageHitHandler;
import org.starhope.appius.game.npc.DamageMissHandler;
import org.starhope.appius.game.npc.Projectile;
import org.starhope.appius.geometry.Coord3D;
import org.starhope.appius.room.Room;
import org.starhope.appius.user.AbstractUser;
import org.starhope.appius.user.AvatarClass;
import org.starhope.appius.user.Nomenclator;

/**
 * Props’ Air Blaster “not-gun”
 * 
 * @author brpocock@star-hope.org
 */
public class PropsAirBlaster extends SimpleRangedWeapon implements
DamageHitHandler, DamageMissHandler {

	/**
	 * Java serialisation unique ID
	 */
	private static final long serialVersionUID = -4119524558191651469L;

	/**
	 * @param theItem the specific item associated with this class
	 */
	public PropsAirBlaster (final InventoryItem theItem) {
		super (theItem);
		setLimitedAmmunition (true);
		setMaxAmmunition (99);
		setAmmunition (theItem.getHealth ().intValueExact ());
		setAvatarUseAction (null);
		try {
			setProjectileAvatar (Nomenclator.getDataRecord (
					AvatarClass.class, "AirBlaster Bubble"));
		} catch (final NotFoundException e) {
			throw AppiusClaudiusCaecus.fatalBug (
					"Can't get the bullet avatar ", e);
		}
		setHitHandler (this);
		setHitAvatar (null);
		setMissAvatar (null);
		setMissHandler (this);
		setRepeatRateMillis (1000);
		setShotDelayMillis (250);
		setTravelRate (175);
		attackDamage = new DamageTypeRanks ();
		try {
			attackDamage
			.add (Nomenclator.getDataRecord (DamageArea.class,
					"Air"), 1);
		} catch (final NotFoundException e) {
			try {
				attackDamage.add (Nomenclator.getDataRecord (
						DamageArea.class, "Wish"), 1);
			} catch (final NotFoundException e1) {
				throw AppiusClaudiusCaecus.fatalBug (e);
			}
		}
		travelsToEdgeOfScreen = true;
	}

	/**
	 * @see org.starhope.appius.game.npc.DamageMissHandler#damageMiss(org.starhope.appius.game.npc.Projectile)
	 */
	@Override
	public void damageMiss (final Projectile projectile) {
		return;
	}

	/**
	 * @see org.starhope.appius.game.npc.DamageHitHandler#hitForDamage(org.starhope.appius.game.npc.Projectile,
	 *      org.starhope.appius.user.AbstractUser)
	 */
	@Override
	public void hitForDamage (final Projectile projectile,
			final AbstractUser victim) {
		if ( !victim.takeAttack (attackDamage)) return;

		final Room room = victim.getRoom ();
		final Coord3D victimAt = victim.getLocation ();
		final Coord3D moved = victimAt.subtract (projectile
				.getStartLocation ());
		final double flew = Coord3D.ORIGIN.distance (moved);
		final String facing = Room.octantFacing [(int) ( (Math.atan2 (
				-moved.getX (), moved.getY ()) + Math.PI + Room.octant / 2) / Room.octant) % 8];
		final double maxDist = new Coord3D (room.getMaxX (),
				room.getMaxY (), room.getMaxZ ())
		.distance (new Coord3D (room.getMinX (), room
				.getMinY (), room.getMinZ ()));
		Coord3D bumpTarget = victimAt.add (moved.multiply (
				(maxDist - flew) / maxDist * 30).divide (flew + .5));
		if ( !room.canWalk (bumpTarget)) {
			bumpTarget = victimAt;
		}
		room.goTo (victim, bumpTarget, facing, "Sit");
		victim.speak (room, "/omg");
	}

	/**
	 * @see org.starhope.appius.game.inventory.effects.SimpleAbstractWeapon#onAmmoCountChanged()
	 */
	@Override
	protected void onAmmoCountChanged () {
		item.setHealth (new BigDecimal (getAmmunition ()));
		super.onAmmoCountChanged ();
	}

	/**
	 * @see org.starhope.appius.game.inventory.ItemEffects#takeDamage(org.starhope.appius.game.DamageTypeRanks)
	 */
	@Override
	public void takeDamage (final DamageTypeRanks attacks) {
		double wishDamage = 0;
		try {
			wishDamage = attacks.get (Nomenclator.getDataRecord (
					DamageArea.class, "Wish"));
		} catch (final NotFoundException e) {
			return;
		}
		--wishDamage;
		if (wishDamage > 0) {
			AppiusClaudiusCaecus.blather (item.getOwner ()
					.getDebugName (), item.getOwner ().getRoom ()
					.getDebugName (), item.getOwner ().getIPAddress (),
					"Reloading Air Blaster from Wish magic × "
					+ wishDamage, false);
			setAmmunition (getAmmunition () + (int) wishDamage);
		}
	}

}
