package com.hero.objects.powers;

import java.text.NumberFormat;
import java.util.ArrayList;

import org.jdom.Element;

import com.hero.HeroDesigner;
import com.hero.objects.Adder;
import com.hero.objects.GenericObject;
import com.hero.objects.modifiers.Modifier;

/**
 * Copyright (c) 2000 - 2005, CompNet Design, Inc. All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, is prohibited unless the following conditions are met: 1.
 * Express written consent of CompNet Design, Inc. is obtained by the developer.
 * 2. Redistributions must retain this copyright notice. THIS SOFTWARE IS
 * PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @author CompNet Design, Inc.
 * @version $Revision$
 */

public class Darkness extends SenseAffectingPower {

	private static String xmlID = "DARKNESS";

	public Darkness(Element root) {
		super(root, Darkness.xmlID);
	}

	@Override
	public String getColumn2Output() {
		String ret = getAlias();
		ArrayList<String> groups = new ArrayList<String>();
		ArrayList<String> senses = new ArrayList<String>();
		ArrayList<Sense> allSenses = Sense.getAllSenses();
		String group = "[Unknown]";
		if (getSelectedOption() != null) {
			group = getSelectedOption().getAlias();
		}
		if (group.toUpperCase().indexOf("GROUP") > 0) {
			group = group.substring(0, group.toUpperCase().indexOf("GROUP"))
					.trim();
		}
		groups.add(group);
		for (Adder ad : getAssignedAdders()) {
			if (ad.getXMLID().equals("ADDITIONAL_GROUP")
					&& (ad.getSelectedOption() != null)) {
				ad.setDisplayInString(false);
				group = ad.getSelectedOption().getAlias();
				if (group.toUpperCase().indexOf("GROUP") > 0) {
					group = group.substring(0,
							group.toUpperCase().indexOf("GROUP")).trim();
				}
				groups.add(group);
			} else if (ad.getXMLID().endsWith("GROUP")) {
				ad.setDisplayInString(false);
				group = ad.getAlias();
				if (group.toUpperCase().indexOf("GROUP") > 0) {
					group = group.substring(0,
							group.toUpperCase().indexOf("GROUP")).trim();
				}
				groups.add(group);
			} else if (ad.getXMLID().equals("ADDITIONAL_SENSE")
					&& (ad.getSelectedOption() != null)) {
				ad.setDisplayInString(false);
				String sense = ad.getSelectedOption().getAlias();
				senses.add(sense);
			} else if (GenericObject.findObjectByID(allSenses, ad.getXMLID()) != null) {
				ad.setDisplayInString(false);
				String sense = ad.getAlias();
				senses.add(sense);
			}
		}
		if (getName().trim().length() > 0) {
			ret = "<i>" + getName() + ":</i>  " + ret;
		}
		ret += " to ";
		for (int i = 0; i < groups.size(); i++) {
			if ((i > 0) && (i < groups.size() - 1)) {
				ret += ", ";
			} else if ((i == groups.size() - 1) && (i > 0)) {
				ret += " and ";
			}
			ret += groups.get(i).toString();
		}
		if (groups.size() > 1) {
			ret += " Groups";
		} else {
			ret += " Group";
		}
		for (int i = 0; i < senses.size(); i++) {
			if (i < senses.size() - 1) {
				ret += ", ";
			} else {
				ret += " and ";
			}
			ret += senses.get(i).toString();
		}
		ret += " " + getDamageDisplay();
		if ((getInput() != null) && (getInput().trim().length() > 0)) {
			ret += ":  " + getInput();
		}
		String adderString = getAdderString();
		if (adderString.trim().length() > 0) {
			ret += ", " + adderString;
		}
		Modifier alteredShape = (Modifier) GenericObject.findObjectByID(
				getAssignedModifiers(), "ALTEREDSHAPE");
		if (alteredShape != null) {
			getAssignedModifiers().remove(alteredShape);
		}
		ret += getModifierString();
		if (alteredShape != null) {
			getAssignedModifiers().add(alteredShape);
		}
		if ((getEndUsage() > 0)
				&& (GenericObject.findObjectByID(HeroDesigner.getActiveHero()
						.getPowers(), "ENDURANCERESERVE") != null)
				&& (GenericObject.findObjectByID(getAllAssignedModifiers(),
						"ENDRESERVEOREND") == null)
				&& !HeroDesigner.getInstance().getPrefs().useWG()) {
			if (getUseENDReserve()) {
				ret += " (uses END Reserve)";
			} else {
				ret += " (uses Personal END)";
			}
		}
		return ret;
	}

	@Override
	public String getDamageDisplay() {
		String ret = "";
		if (HeroDesigner.getActiveTemplate().is6E()) {
			ret = NumberFormat.getIntegerInstance().format(getLevels())
					+ "m radius";
		} else {
			ret = NumberFormat.getIntegerInstance().format(getLevels())
					+ "\" radius";
		}
		Modifier alteredShape = null;
		if (GenericObject
				.findObjectByID(getAssignedModifiers(), "ALTEREDSHAPE") != null) {
			alteredShape = (Modifier) GenericObject.findObjectByID(
					getAssignedModifiers(), "ALTEREDSHAPE");
		} else {
			return ret;
		}

		if ((alteredShape != null)
				&& (alteredShape.getSelectedOption() != null)
				&& alteredShape.getSelectedOption().getXMLID().equals("CONE")) {
			ret = NumberFormat.getIntegerInstance().format(getLevels() * 2)
					+ "\" Cone";
			if (HeroDesigner.getActiveTemplate().is6E()) {
				ret = NumberFormat.getIntegerInstance().format(getLevels() * 2)
				+ "m Cone";
			}
		} else if ((alteredShape != null)
				&& (alteredShape.getSelectedOption() != null)
				&& alteredShape.getSelectedOption().getXMLID().equals("LINE")) {
			double width = 1;
			if (GenericObject.findObjectByID(alteredShape.getAssignedAdders(),
					"DOUBLEWIDTH") != null) {
				Adder ad = (Adder) GenericObject.findObjectByID(alteredShape
						.getAssignedAdders(), "DOUBLEWIDTH");
				width = Math.pow(ad.getLevelPower(), ad.getLevels());
			}
			if (width == 1) {
				ret = NumberFormat.getIntegerInstance().format(
								getLevels() * 2) + "\" Line";
				if (HeroDesigner.getActiveTemplate().is6E()) {
				ret = NumberFormat.getIntegerInstance().format(
								getLevels() * 2) + "m Line";
					
				}
			} else {
				ret = NumberFormat.getIntegerInstance().format(
								getLevels() * 2)
						+ "\" long and "
						+ NumberFormat.getIntegerInstance().format(width)
						+ "\" wide Line";
				if (HeroDesigner.getActiveTemplate().is6E()) {
					ret = NumberFormat.getIntegerInstance().format(
							getLevels() * 2)
					+ "m long and "
					+ NumberFormat.getIntegerInstance().format(width)
					+ "m wide Line";
						
					}
			}
		} else if ((alteredShape != null)
				&& (alteredShape.getSelectedOption() != null)
				&& alteredShape.getSelectedOption().getXMLID().equals("ANY")) {
			ret = NumberFormat.getIntegerInstance().format(getLevels())
					+ "\" Any Area";
			if (HeroDesigner.getActiveTemplate().is6E()) {
				ret = NumberFormat.getIntegerInstance().format(getLevels())
				+ "m Any Area";
					
				}
		}
		return ret;
	}
}