package com.hero.objects.modifiers;

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.powers.Power;

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

	private static String xmlID = "CONCENTRATION";

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

	@Override
	public ArrayList<Modifier> getAssignedModifiers() {
		ArrayList<Modifier> ret = super.getAssignedModifiers();
		if (!HeroDesigner.getInstance().getPrefs().isModifierIntelligenceOn()) {
			return ret;
		}
		if ((getProgenitor() == null) || !(getProgenitor() instanceof Power)) {
			return ret;
		}
		GenericObject p = getProgenitor();
		if ((HeroDesigner.getActiveHero() == null)
				|| HeroDesigner.getActiveHero().isLoading()
				|| !p.getDuration().equals("INSTANT")
				|| (GenericObject.findObjectByID(p.getAssignedModifiers(),
						"EXTRATIME") != null)
				|| (GenericObject.findObjectByID(p.getAssignedModifiers(),
						"REGENEXTRATIME") != null)
				|| getProgenitor().getXMLID().equals("MINDCONTROL")
				|| getProgenitor().getXMLID().equals("MINDSCAN")
				|| getProgenitor().getXMLID().equals("MINDLINK")
				|| getProgenitor().getXMLID().equals("TELEPATHY")
				|| getProgenitor().getXMLID().equals("FORCEWALL")) {
			return ret;
		}
		for (Modifier mod : ret) {
			if (mod.getXMLID().equals("CONTINUOUSCONCENTRATION")) {
				ret.remove(mod);
				return ret;
			}
		}
		return ret;
	}

	@Override
	public ArrayList<Modifier> getAvailableModifiers() {
		ArrayList<Modifier> ret = (ArrayList<Modifier>) super
				.getAvailableModifiers().clone();
		if ((getProgenitor() == null) || !(getProgenitor() instanceof Power)) {
			return ret;
		}
		if ((HeroDesigner.getActiveHero() == null)
				|| !HeroDesigner.getInstance().getPrefs()
						.isModifierIntelligenceOn()
				|| HeroDesigner.getActiveHero().isLoading()) {
			return ret;
		}
		GenericObject p = getProgenitor();
		if (!p.getDuration().equals("INSTANT")
				|| (GenericObject.findObjectByID(p.getAssignedModifiers(),
						"EXTRATIME") != null)
				|| (GenericObject.findObjectByID(p.getAssignedModifiers(),
						"REGENEXTRATIME") != null)
				|| getProgenitor().getXMLID().equals("MINDCONTROL")
				|| getProgenitor().getXMLID().equals("MINDSCAN")
				|| getProgenitor().getXMLID().equals("MINDLINK")
				|| getProgenitor().getXMLID().equals("TELEPATHY")
				|| getProgenitor().getXMLID().equals("FORCEWALL")) {
			return ret;
		}
		for (Modifier mod : ret) {
			if (mod.getXMLID().equals("CONTINUOUSCONCENTRATION")) {
				ret.remove(mod);
				return ret;
			}
		}
		return ret;
	}

	@Override
	public String getColumn2Output() {
		String adderString = "";
		String ret = "";
		if (!showOptionOnly) {
			ret += getAlias();
		}
		double val = getTotalValue();
		if ((getInput() != null) && (getInput().trim().length() > 0)) {
			if (ret.trim().length() > 0) {
				ret += " ";
			}
			ret += getInput();
		}
		ret = ret.trim();
		for (Modifier mod : getAssignedModifiers()) {
			ret += ", " + mod.getAlias();
		}
		int index = 0;
		int parenCount = 0;
		while (ret.indexOf("(", index) >= 0) {
			parenCount++;
			index = ret.indexOf("(", index) + 1;
		}
		index = 0;
		while (ret.indexOf(")", index) >= 0) {
			parenCount--;
			index = ret.indexOf(")", index) + 1;
		}
		if (parenCount <= 0) {
			ret += " (";
		} else {
			ret += "; ";
		}
		if ((getSelectedOption() != null)
				&& getSelectedOption().displayInString()
				&& (getSelectedOption().getAlias().trim().length() > 0)) {
			ret += getSelectedOption().getAlias() + "; ";
		}
		for (Adder ad : getAssignedAdders()) {
			if (!ad.isSelected()) {
				continue;
			}
			if (ad.getColumn2Output().trim().length() > 0) {
				ret += ad.getColumn2Output().trim() + "; ";
			}
		}
		if (getComments().trim().length() > 0) {
			ret += getComments() + "; ";
		}
		if ((val > maxCost) && maxSet) {
			val = maxCost;
		}
		if ((val < minimumCost) && minSet) {
			val = minimumCost;
		}
		ret += getFraction(val) + ")";
		parenCount--;
		while (parenCount > 0) {
			ret += ")";
			parenCount--;
		}
		if (adderString.trim().length() > 0) {
			if (ret.trim().length() > 0) {
				ret += ", ";
			}
			ret += adderString;
		}
		return ret;
	}

}