package ru.alphatech.icfpc.game;

import ru.alphatech.icfpc.cards.I;
import ru.alphatech.icfpc.cards.ICard;
import ru.alphatech.icfpc.cards.Zero;



public class Player
{
	private int playerNumber = -1;

	private Slot[] slots = new Slot[256];
	private Player myOpponent = null;

	public Player()
    {
		for(int i = 0; i <= 255; i++)
		{
			slots[i] = new Slot(i);
		}
    }
	
	public int getPlayerNumber() {
		return playerNumber;
	}
	
	public void setPlayerNumber(int playerNumber) {
		this.playerNumber = playerNumber;
	}

	public Player getMyOpponent() {
		return myOpponent;
	}
	
	public void setMyOpponent(Player myOpponent) {
		this.myOpponent = myOpponent;
	}
	
	public Slot getSlot(int i)
	{
		return slots[i];
	}
	
	public PlayerStatistic getStatistic(){
		throw new RuntimeException("Not implemented yet.");
	}
	
	public void applyBatch(Batch batch){
		Run.setAutoMode(false);
		Run.resetCallCount();
		try{
			switch(batch.operationType){
			case CARD_TO_SLOT:
				IValue value = getSlot(batch.slot.getId()).getValue();
				
				if(value instanceof Zero)
				{
					value = new Value(0);
				}
				
				if(batch.card instanceof Zero)
				{
					batch.card = (ICard) new Value(0);
				}

				batch.card.returnValue(value, this, myOpponent);
				break;
			case SLOT_TO_CARD:
				getSlot(batch.slot.getId()).getValue().returnValue(batch.card, this, myOpponent);
				break;
			}
		} catch (ICFPCException exception){
			getSlot(batch.slot.getId()).putValue(new I());
		}
		Run.setAutoMode(true);
		Run.resetCallCount();
		for(Slot slot: myOpponent.slots){
			if(slot.isZombie()){
				slot.getValue().returnValue(new I(), myOpponent, myOpponent.getMyOpponent());
			}
		}
	}
	
}
