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.Run;
import ru.alphatech.icfpc.game.Value;

public class Dec extends AbstractCard
{
	@Override
	public String getName() {
		return "dec";
	}
	
	@Override
	public IValue returnValue(IValue iobj, Player proponent, Player opponent)
	{
		incAndTest();
		
		if(iobj instanceof Value)
		{
			int i = ((Value)iobj).getValue();

			if (i >= 0 && i <= 255)
			{
				int v = proponent.getSlot(255 - i).getVitality();
				
				if(Run.getAutoMode())
				{
					if(v > 0 && v < 65535)
					{
						proponent.getSlot(255 - i).setVitality(v + 1);
					}
				}
				else
				{
					if (v > 0)
					{
						proponent.getSlot(255 - i).setVitality(v - 1);
					}
				}

				return new I();
			}
			else
			{
				throw new ICFPCException();
			}
		}
		else
		{
			throw new ICFPCException();
		}
	}
}
