package ru.alphatech.icfpc.policy;

import java.util.ArrayList;

import ru.alphatech.icfpc.cards.Attack;
import ru.alphatech.icfpc.cards.Get;
import ru.alphatech.icfpc.cards.Help;
import ru.alphatech.icfpc.cards.ICard;
import ru.alphatech.icfpc.cards.K;
import ru.alphatech.icfpc.cards.Put;
import ru.alphatech.icfpc.cards.S;
import ru.alphatech.icfpc.cards.Succ;
import ru.alphatech.icfpc.cards.Zero;
import ru.alphatech.icfpc.game.AbstractPolicy;
import ru.alphatech.icfpc.game.Batch;
import ru.alphatech.icfpc.game.ICFPCException;
import ru.alphatech.icfpc.game.Run;
import ru.alphatech.icfpc.game.Slot;
import ru.alphatech.icfpc.game.Batch.OperationType;

public class AttackHelpPolicy extends AbstractPolicy {

	private int donorSlotIndex   = -1;
	private int targetSlotIndex  = -1;
	private int pointsSlotIndex  = -1;
	private int processingSlotIndex = -1;
	private boolean execute = false;
	
	private TYPE type;
	
	public enum TYPE {
		ATTACK,
		HELP
	}
	
	/**
	 * @param type - ATTACK or HELP
	 * @param processingSlot - в какой ячейке проводить
	 * @param donor - от кого забирать хп
	 * @param target - слот который надо атаковать
	 * @param points - количество хп отнимаемое у донора
	 * @param execute - выполнить после завершения либо просто сохранить в слоте.
	 */
	public AttackHelpPolicy(TYPE type, int processingSlot, int donor, int target, int points, boolean execute) {
		this.donorSlotIndex  = donor;
		this.targetSlotIndex = target;
		this.pointsSlotIndex = points;
		this.processingSlotIndex = processingSlot;
		this.execute = execute;
		
		this.type = type;
	}
	
	@Override
	public ArrayList<Batch> getBatches() {
		ArrayList<Batch> retArray = new ArrayList<Batch>();
		
		Slot processingSlot = Run.getProponent().getSlot(processingSlotIndex);
		
		retArray.addAll(numberIntoSlotField(Run.getRegisterByIndex(0), donorSlotIndex));
		retArray.addAll(numberIntoSlotField(Run.getRegisterByIndex(1), targetSlotIndex));
		retArray.addAll(numberIntoSlotField(Run.getRegisterByIndex(2), pointsSlotIndex));
		
		retArray.addAll(BcombineForFunctWith3ParamsInRegister(getCard(), processingSlot));
		
		return retArray;
	}
	
	public ICard getCard(){
		return type == TYPE.ATTACK ? new Attack() : new Help();
	}
	
	protected ArrayList<Batch> BcombineForFunctWith3ParamsInRegister(ICard function, Slot processingSlot)
	{
		ArrayList<Batch> retArray = new ArrayList<Batch>();
		
		if(!(function instanceof Attack || function instanceof Help)){
			throw new ICFPCException();
		}
		
		//TODO Абстрагироваться от регистров
		
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new Put(), processingSlot));
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, function,  processingSlot));
		
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
		
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));
		
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
		
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
		
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));

		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
		
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
		
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
		
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
		
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Get(), processingSlot));

		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
		
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
		
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new K(), processingSlot));
		retArray.add(new Batch(OperationType.CARD_TO_SLOT, new S(), processingSlot));
		
		retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Succ(), processingSlot));
		
		if(execute)
			retArray.add(new Batch(OperationType.SLOT_TO_CARD, new Zero(), processingSlot));
		
		return retArray;
	}


}
