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 Help implements ICard
{
	@Override
	public IValue returnValue(final IValue iobj, Player proponent, Player opponent)
	{
		return new ICard()
		{
			@Override
			public IValue returnValue(final IValue jobj, Player proponent, Player opponent)
			{
				return new ICard()
				{
					@Override
					public IValue returnValue(final IValue nobj, Player proponent, Player opponent)
					{
						if(iobj instanceof Value && nobj instanceof Value)
						{
							int i = ((Value)iobj).getValue();
							int n = ((Value)nobj).getValue();
							int v = proponent.getSlot(i).getVitality();
							
							if(i >= 0 && i <= 255 && n <= v)
							{
								if(jobj instanceof Value)
								{
									int j = ((Value)jobj).getValue();
									
									if(j >= 0 && j <= 255)
									{
										int w = proponent.getSlot(j).getVitality();
										
										if(w > 0)
										{
											int winc = (int) Math.floor(11 * n / 10);
											int resw = w + winc;
											
											proponent.getSlot(j).setVitality(resw > 65535 ? 65535 : resw);
										}
										
										return new I();
									}
									else
									{
										throw new ICFPCException();
									}
								}
								else
								{
									throw new ICFPCException();
								}
							}
							else
							{
								throw new ICFPCException();
							}
						}
						else
						{
							throw new ICFPCException();
						}
					}
				};
			}
		};
	}
}
