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

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

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.SimpleArmour;
import org.starhope.appius.user.Nomenclator;

/**
 * A Light Field provides (temporary) protection against Shadow damage
 * 
 * @author brpocock@star-hope.org
 */
public class LightField extends SimpleArmour {

	/**
	 * The “array” of shield items and their values
	 */
	final private static Map <Integer, BigDecimal> lightshields = new HashMap <Integer, BigDecimal> ();

	{
		LightField.lightshields.put (Integer.valueOf (2273), BigDecimal
				.valueOf (87.5)); // white
		LightField.lightshields.put (Integer.valueOf (2270), BigDecimal
				.valueOf (75)); // pink
		LightField.lightshields.put (Integer.valueOf (2271), BigDecimal
				.valueOf (62.5)); // purple
		LightField.lightshields.put (Integer.valueOf (2268), BigDecimal
				.valueOf (50)); // blue
		LightField.lightshields.put (Integer.valueOf (2269), BigDecimal
				.valueOf (37.5)); // green
		LightField.lightshields.put (Integer.valueOf (2274), BigDecimal
				.valueOf (25)); // yellow
		LightField.lightshields.put (Integer.valueOf (2140), BigDecimal
				.valueOf (12.5)); // orange
		LightField.lightshields.put (Integer.valueOf (2272), BigDecimal
				.valueOf (0)); // red
	}

	/**
	 * @param theItem the item being used as a Light Field
	 */
	public LightField (final InventoryItem theItem) {
		super (theItem);
		attritionLinear = true;
		attritionRate = .01;
		adjustColour ();
	}

	/**
	 * WRITEME: Document this method brpocock@star-hope.org
	 */
	private void adjustColour () {
		/*
		 * Find the best shield that is rated for equal or less than the
		 * current shield value
		 */
		Map.Entry <Integer, BigDecimal> bestShield = null;
		final BigDecimal health = item.getHealth ();
		for (Entry <Integer, BigDecimal> aShield : LightField.lightshields
				.entrySet ()) {
			if (aShield.getValue ().compareTo (health) <= 0) {
				if (null != bestShield) {
					if (bestShield.getValue ().compareTo (
							aShield.getValue ()) > 0) {
						continue;
					}
				}
				bestShield = aShield;
			}
		}
		if (health.compareTo (BigDecimal.ZERO) < 0
				|| null == bestShield) {
			item.destroy ();
			item
					.getOwner ()
					.acceptMessage ("Shade", "Shade",
							"Haa haa haa! Shade's Shadow Bolt has destroyed your Lightshield!");
		}
	}

	/**
	 * @see org.starhope.appius.game.inventory.ItemEffects#takeDamage(org.starhope.appius.game.DamageTypeRanks)
	 */
	@Override
	public void takeDamage (final DamageTypeRanks attacks) {
		try {
			final int delta = -(int) attacks.get (Nomenclator
					.getDataRecord (DamageArea.class, "Shadow"));
			item.changeHealth ( delta);
		} catch (NotFoundException e) {
			AppiusClaudiusCaecus
					.reportBug (
							"Caught a NotFoundException in LightField.takeDamage ",
							e);
		}
		adjustColour ();
		super.takeDamage (attacks);
	}

}
