/*
* Copyright 2008 (C) Thomas Parker <thpr@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package plugin.lsttokens;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import pcgen.base.formula.Formula;
import pcgen.cdom.base.CDOMObject;
import pcgen.cdom.base.CDOMReference;
import pcgen.cdom.base.ChoiceActor;
import pcgen.cdom.base.ChoiceSet;
import pcgen.cdom.base.ConcreteTransitionChoice;
import pcgen.cdom.base.Constants;
import pcgen.cdom.base.FormulaFactory;
import pcgen.cdom.base.NonInteractive;
import pcgen.cdom.base.TransitionChoice;
import pcgen.cdom.base.Ungranted;
import pcgen.cdom.choiceset.QualifiedDecorator;
import pcgen.cdom.choiceset.ReferenceChoiceSet;
import pcgen.cdom.enumeration.ListKey;
import pcgen.core.Kit;
import pcgen.core.PlayerCharacter;
import pcgen.rules.context.Changes;
import pcgen.rules.context.LoadContext;
import pcgen.rules.persistence.token.AbstractTokenWithSeparator;
import pcgen.rules.persistence.token.CDOMPrimaryToken;
import pcgen.rules.persistence.token.ParseResult;
/**
* @author djones4
*
*/
public class KitLst extends AbstractTokenWithSeparator<CDOMObject> implements
CDOMPrimaryToken<CDOMObject>, ChoiceActor<Kit>
{
private static final Class<Kit> KIT_CLASS = Kit.class;
@Override
public String getTokenName()
{
return "KIT";
}
@Override
protected char separator()
{
return '|';
}
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context,
CDOMObject obj, String value)
{
if (obj instanceof Ungranted)
{
return new ParseResult.Fail("Cannot use " + getTokenName()
+ " on an Ungranted object type: "
+ obj.getClass().getSimpleName(), context);
}
if (obj instanceof NonInteractive)
{
return new ParseResult.Fail("Cannot use " + getTokenName()
+ " on an Non-Interactive object type: "
+ obj.getClass().getSimpleName(), context);
}
StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
Formula count = FormulaFactory.getFormulaFor(tok.nextToken());
if (!count.isValid())
{
return new ParseResult.Fail("Count in " + getTokenName()
+ " was not valid: " + count.toString(), context);
}
if (!count.isStatic())
{
return new ParseResult.Fail("Count in "
+ getTokenName() + " must be a number", context);
}
if (count.resolveStatic().intValue() <= 0)
{
return new ParseResult.Fail("Count in "
+ getTokenName() + " must be > 0", context);
}
if (!tok.hasMoreTokens())
{
return new ParseResult.Fail(getTokenName()
+ " must have a | separating "
+ "count from the list of possible values: " + value, context);
}
List<CDOMReference<Kit>> refs = new ArrayList<>();
while (tok.hasMoreTokens())
{
String token = tok.nextToken();
CDOMReference<Kit> ref;
if (Constants.LST_ALL.equals(token))
{
ref = context.getReferenceContext().getCDOMAllReference(KIT_CLASS);
}
else
{
ref = context.getReferenceContext().getCDOMReference(KIT_CLASS, token);
}
refs.add(ref);
}
ReferenceChoiceSet<Kit> rcs = new ReferenceChoiceSet<>(refs);
if (!rcs.getGroupingState().isValid())
{
return new ParseResult.Fail("Non-sensical "
+ getTokenName()
+ ": Contains ANY and a specific reference: " + value, context);
}
ChoiceSet<Kit> cs = new ChoiceSet<>(getTokenName(),
new QualifiedDecorator<>(rcs));
cs.setTitle("Kit Selection");
TransitionChoice<Kit> tc = new ConcreteTransitionChoice<>(cs, count);
context.getObjectContext().addToList(obj, ListKey.KIT_CHOICE, tc);
tc.setRequired(false);
tc.setChoiceActor(this);
return ParseResult.SUCCESS;
}
@Override
public String[] unparse(LoadContext context, CDOMObject pcc)
{
Changes<TransitionChoice<Kit>> changes = context.getObjectContext()
.getListChanges(pcc, ListKey.KIT_CHOICE);
if (changes == null || changes.isEmpty())
{
// Zero indicates no Token
return null;
}
Collection<TransitionChoice<Kit>> added = changes.getAdded();
Set<String> set = new TreeSet<>();
for (TransitionChoice<Kit> tc : added)
{
StringBuilder sb = new StringBuilder();
sb.append(tc.getCount());
sb.append(Constants.PIPE);
sb.append(tc.getChoices().getLSTformat().replaceAll(
Constants.COMMA, Constants.PIPE));
set.add(sb.toString());
}
return set.toArray(new String[set.size()]);
}
@Override
public Class<CDOMObject> getTokenClass()
{
return CDOMObject.class;
}
@Override
public void applyChoice(CDOMObject owner, Kit choice, PlayerCharacter pc)
{
Kit.applyKit(choice, pc);
}
@Override
public boolean allow(Kit choice, PlayerCharacter pc, boolean allowStack)
{
for (Kit k : pc.getKitInfo())
{
if (k.getKeyName().equalsIgnoreCase(choice.getKeyName()))
{
return false;
}
}
return true;
}
}