package ru.alphatech.icfpc.policy;

import java.util.ArrayList;

import ru.alphatech.icfpc.cards.Dbl;
import ru.alphatech.icfpc.cards.ICard;
import ru.alphatech.icfpc.cards.Put;
import ru.alphatech.icfpc.cards.Zero;
import ru.alphatech.icfpc.game.Batch;
import ru.alphatech.icfpc.game.IValue;
import ru.alphatech.icfpc.game.Player;
import ru.alphatech.icfpc.game.Slot;
import ru.alphatech.icfpc.game.Value;
import ru.alphatech.icfpc.game.Batch.OperationType;

public abstract class AbstractPolicy {

	protected Player player = null;
	
	protected ArrayList<Batch> batches = new ArrayList<Batch>();
	
	public AbstractPolicy(Player player){
		this.player = player;
	}	
	
	public void setRegister0( int num ){
		
	}
	
	public void setRegister1( int num ){
		
	}
	
	public void setRegister2( int num ){
		
	}
	
	protected ArrayList<Batch> numberIntoSlotField(int slot, int number)
	{	
		ArrayList<Batch> retArray = new ArrayList<Batch>();
		
		Slot targetSlot = player.getSlot(slot);
		IValue targetValue = targetSlot.getValue();
		
		if(targetValue instanceof ICard){
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
		}
		
		Value numTargetValue = (Value) targetValue;
		if( numTargetValue.getValue() == number ){
			return retArray;
		}else
		if( numTargetValue.getValue() < number ){
			int buffValue = numTargetValue.getValue();
			while( buffValue*2 < number){
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
			}
			while( buffValue != number){
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
			}
		}else{
			retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), targetSlot));
			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), targetSlot));
			int buffValue = numTargetValue.getValue();
			while( buffValue*2 < number){
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
			}
			while( buffValue != number){
				retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Dbl(), targetSlot));
			}
		}
		
		return retArray;
		
	}
	
}
