package net.woodyfolsom.msproj.policy;

import java.io.IOException;
import java.util.Collection;

import net.woodyfolsom.msproj.Action;
import net.woodyfolsom.msproj.GameConfig;
import net.woodyfolsom.msproj.GameState;
import net.woodyfolsom.msproj.Player;

public class HumanKeyboardInput implements Policy {

	@Override
	public Action getAction(GameConfig gameConfig, GameState gameState,
			Player player) {
		byte[] inputBytes = new byte[4];
		Action action = null;
		String input = "";

		do {
			try {
				System.out.println(player
						+ " to move: (enter coord or PASS/RESIGN)");
				System.in.read(inputBytes);
				input = new String(inputBytes).trim().toUpperCase();

				action = Action.getInstance(input);

				if (action.isNone()) {
					System.out
							.println("'"
									+ input
									+ "' is not a valid move.  Please enter a coordinate (A1-T19) or 'PASS'.");
					System.out.println(gameState);
					continue;
				}

			} catch (IOException ioe) {
				System.out.println("IOException: " + ioe.getMessage());
			} catch (IllegalArgumentException iae) {
				System.out.println("IllegalArgumentException - '" + input
						+ "' is not a valid action or coordinate.");
			}
		} while (action == null);

		return action;
	}

	@Override
	public Action getAction(GameConfig gameConfig, GameState gameState,
			Collection<Action> prohibitedActions, Player player) {
		Action action = getAction(gameConfig, gameState, player);

		if (!prohibitedActions.contains(action)) {
			return action;
		}

		do {
			System.out
					.println("Sorry, that action is prohibited.  Please make another move.");
			action = getAction(gameConfig, gameState, player);
		} while (prohibitedActions.contains(action));

		return action;
	}

	@Override
	public int getNumStateEvaluations() {
		// TODO Auto-generated method stub
		return 1;
	}

	@Override
	public void setState(GameState gameState) {
		// TODO Auto-generated method stub
		
	}

}
