package com.hero.objects.modifiers;

import org.jdom.Element;

import com.hero.objects.Adder;
import com.hero.objects.GenericObject;
import com.hero.objects.powers.ChangeEnvironment;
import com.hero.ui.dialog.ModifierDialog;
import com.hero.util.Rounder;

/**
 * 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 Explosion extends Modifier {

	private static String xmlID = "EXPLOSION";

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

	@Override
	public String getColumn2Output() {
		String adderString = "";
		String ret = "";
		ret += getAlias();
		double val = getTotalValue();
		for (Adder ad : getAssignedAdders()) {
			if (adderString.length() > 0) {
				adderString += ", ";
			}
			adderString += ad.getAlias() + " (" + getFraction(ad.getBaseCost())
					+ ")";
			val -= ad.getBaseCost();
		}
		if ((getInput() != null) && (getInput().trim().length() > 0)) {
			if (ret.trim().length() > 0) {
				ret += ":  ";
			}
			ret += getInput();
		}
		for (Modifier mod : getAssignedModifiers()) {
			ret += ", " + mod.getAlias();
		}
		ret += " (";
		if ((getSelectedOption() != null)
				&& (getSelectedOption().getAlias().trim().length() > 0)
				&& !getSelectedOption().getAlias().equals("Normal (Radius)")) {
			ret += getSelectedOption().getAlias();
			ret += "; ";
		}
		if (getLevels() > 0) {
			int fade = getLevels();
			if ((getSelectedOption() != null)
					&& (getSelectedOption().getLevelMultiplier() != 0)) {
				fade = fade * getSelectedOption().getLevelMultiplier();
			}
			if (fade != 1) {
				ret += "-1 DC/" + fade + "\"; ";
			}
		}
		if (getComments().trim().length() > 0) {
			ret += getComments() + "; ";
		}
		ret += getFraction(val) + ")";
		if (adderString.trim().length() > 0) {
			if (ret.trim().length() > 0) {
				ret += ", ";
			}
			ret += adderString;
		}
		return ret;
	}

	@Override
	public ModifierDialog getDialog(GenericObject parent, boolean isNew) {
		ModifierDialog d = new ModifierDialog(this, isNew, parent);
		d.levelsLbl.setText("Fade Rate (1 DC/ X\")");
		return d;
	}

	@Override
	public double getTotalValue() {
		double val = getBaseCost();
		for (Adder ad : getAssignedAdders()) {
			val += ad.getDoubleTotal();
		}
		if (getLevelValue() > 0) {
			val += (getLevels() - getMinimumLevel()) * getLevelCost();
		}
		// some funkiness here, as the increase in fade rate for each +1/4 is
		// 1", regardless of the shape of the explosion...only the
		// starting point changes (1" for radius, 2" for cone, 3" for line)
		double advantageTotal = 0d;
		for (Modifier mod : getAssignedModifiers()) {
			if (mod.getTotalValue() > 0) {
				advantageTotal += mod.getTotalValue();
			}
		}
		double active = val * (1 + advantageTotal);
		double limitationTotal = 0d;
		for (Modifier mod : getAssignedModifiers()) {
			if (mod.getTotalValue() < 0) {
				limitationTotal += Math.abs(mod.getTotalValue());
			}
		}
		val = active / (1 + limitationTotal);

		// the following effectively "rounds" the value to the closest 1/4
		val = val * 4d;
		int mult = 1;
		if (val < 0) {
			mult = -1;
		}
		val = val * mult;
		val = Rounder.roundHalfUp(val);
		val = val * mult;
		val = val / 4d;

		if ((val < getMinimumCost()) && isMinSet()) {
			return getMinimumCost();
		} else if ((val > getMaxCost()) && isMaxSet()) {
			return getMaxCost();
		} else {
			return val;
		}
	}

	@Override
	public String included(GenericObject o) {
		String ret = super.included(o);
		if (ret.trim().length() > 0) {
			return ret;
		}

        if (forceAllow()) return ret;

		if (o instanceof ChangeEnvironment) {
			return "";
		}
		if (o.getTarget().equals("HEX")) {
			return getDisplay()
					+ " cannot be applied to Powers which already affect an area.";
		} else if (!(o.getTarget().equals("DCV") || o.getTarget().equals("ECV"))) {
			return getDisplay()
					+ " can only be applied to Powers which are targeted on others.";
		} else if (GenericObject
				.findObjectByID(o.getAssignedModifiers(), "AOE") != null) {
			return getDisplay()
					+ " cannot be applied to abilities which already affect an area.";
		} else {
			return "";
		}
	}

}