package ru.alphatech.icfpc.cards;

import ru.alphatech.icfpc.game.ICFPCException;
import ru.alphatech.icfpc.game.IValue;
import ru.alphatech.icfpc.game.Player;
import ru.alphatech.icfpc.game.Value;

public class Attack implements ICard
{
	@Override
	public IValue returnValue(final IValue i, Player proponent, Player opponent)
	{
		return new ICard()
		{
			@Override
			public IValue returnValue(final IValue j, Player proponent, Player opponent)
			{
				return new ICard()
				{
					@Override
					public IValue returnValue(final IValue n, Player proponent, Player opponent)
					{
						if (i.getClass().equals(Value.class) && j.getClass().equals(Value.class) && n.getClass().equals(Value.class))
						{
							int ival = ((Value)i).getValue();
							int jval = ((Value)j).getValue();
							int nval = ((Value)n).getValue();

							if (ival >= 0 && ival < 256 && jval >= 0 && jval < 256)
							{
								int v = proponent.getSlot(ival).getVitality();
								int w = opponent.getSlot(255 - jval).getVitality();
								if (v - nval < 1)
								{
									proponent.getSlot(ival).setVitality(v - nval);
									if (w > 0)
									{
										opponent.getSlot(255 - jval).setVitality(w - ((9 * nval)/10));
									}
								}
								else
								{
									throw new ICFPCException();
								}
							}
							else
							{
								throw new ICFPCException();
							}
						}
						else
						{
							throw new ICFPCException();
						}
						return new I();
					}
				};
			}
		};
	}

}
