package net.woodyfolsom.msproj;

import java.util.Arrays;

public class GameBoard {
	public static final char BLACK_STONE = 'X';
	public static final char BLACK_TERRITORY = 'x';
	public static final char EMPTY_INTERSECTION = '.';
	public static final char MARKED_GROUP = 'g';
	public static final char MARKED_TERRITORY = '?';
	public static final char UNOWNED_TERRITORY = '-';
    public static final char WHITE_STONE = 'O';
	public static final char WHITE_TERRITORY = 'o';
	
	private boolean territoryMarked = false;
	private int size;
	private char[] board;
	
	public GameBoard(int size) {
		this.size = size;
		board = new char[size * size];
		Arrays.fill(board, '.');
	}
	
	public GameBoard(GameBoard that) {
		this.size = that.size;
		this.board = Arrays.copyOf(that.board, that.board.length);
	}
	
	public void clear() {
		territoryMarked = false;
		Arrays.fill(board, EMPTY_INTERSECTION);
	}
	
	public int countSymbols(char... symbols) {
		int stoneCount = 0;
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < symbols.length; j++) {
				if (board[i] == symbols[j]) {
					stoneCount++;
				}
			}
		}
		return stoneCount;
	}
	
	@Override
	//TODO: implement as Zobrist hash.
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(board);
		result = prime * result + size;
		result = prime * result + (territoryMarked ? 1231 : 1237);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GameBoard other = (GameBoard) obj;
		if (!Arrays.equals(board, other.board))
			return false;
		if (size != other.size)
			return false;
		if (territoryMarked != other.territoryMarked)
			return false;
		return true;
	}

	public static int getColumnIndex(char columnLabel) {
		if (columnLabel < 'I') {
			return columnLabel - 'A';
		} else if (columnLabel > 'I') {
			return columnLabel - 'A' - 1;
		} else {
			throw new IllegalArgumentException("Invalid column label: "
					+ columnLabel);
		}
	}
	
	public static char getColumnLabel(int columnIndex) {
		if (columnIndex > 7) {
			return (char) ('A' + columnIndex + 1);
		} else {
			return (char) ('A' + columnIndex);
		}
	}
	
	public static String getCoordinate(int colIndex, int rowIndex) {
		return String.valueOf(getColumnLabel(colIndex)) + (rowIndex + 1);
	}
	
	public static char getOpponentSymbol(char stoneSymbol) {
		if (stoneSymbol == GameBoard.BLACK_STONE) {
			return GameBoard.WHITE_STONE;
		} else if (stoneSymbol == GameBoard.WHITE_STONE) {
			return GameBoard.BLACK_STONE;
		} else {
			throw new IllegalArgumentException("StoneSymbol must be BLACK_STONE or WHITE_STONE");
		}
	}
	
	public int getSize() {
		return size;
	}
	
	/**
	 * @param colLabel [A..T] (skipping I
	 * @param rowNumber 1-based
	 * @return
	 */
	public char getSymbolAt(char colLabel, int rowNumber) {
		return getSymbolAt(getColumnIndex(colLabel), rowNumber - 1);
	}

	/**
	 * 0-based.
	 * @param col
	 * @param row
	 * @return
	 */
	public char getSymbolAt(int col, int row) {
		try {
			return board[(size - row - 1) * size + col];
		} catch (ArrayIndexOutOfBoundsException ae) {
			ae.printStackTrace();
			throw ae;
		}
	}
	
	public boolean isEmpty(char col, int row) {
		return getSymbolAt(col, row) == EMPTY_INTERSECTION;
	}
	
	public boolean isTerritoryMarked() {
		return territoryMarked;
	}

	public boolean isStarPoint(char col, int row) {
		switch (size) {
		case 3:
			if (col == 'B' && row == 2) {
				return true;
			} else {
				return false;
			}
		case 4:
			return false;
		default:
			return false;
		}
	}
	
	public boolean markTerritory(int col, int row, char mark) {
		char symbol = getSymbolAt(col, row);
		if (symbol != '.') {
			return false;
		}
		setSymbolAt(col,row,mark);
		return true;
	}
	
	public boolean removeStone(char colLabel, int rowNum) {
		if (getSymbolAt(colLabel,rowNum) == EMPTY_INTERSECTION) {
			return false;
		}
		setSymbolAt(colLabel,rowNum,EMPTY_INTERSECTION);
		return true;
	}
	
	public int replaceSymbol(char symbol, char replacement) {
		int numReplaced = 0;
		for (int i = 0; i < board.length; i++) {
			if (board[i] == symbol) {
				board[i] = replacement;
				numReplaced++;
			}
		}
		return numReplaced;
	}
	
	public void setSymbolAt(char colLabel, int rowNumber, char symbol) {
		setSymbolAt(getColumnIndex(colLabel),rowNumber-1,symbol);
	}
	public void setSymbolAt(int col, int row, char symbol) {
		board[(size - row - 1) * size + col] = symbol;
	}
	
	public void setTerritoryMarked(boolean territoryMarked) {
		this.territoryMarked = territoryMarked;
	}
	
	public void unmarkTerritory() {
		if (territoryMarked == false) {
			return;
		}
		
		replaceSymbol(BLACK_TERRITORY,EMPTY_INTERSECTION);
		replaceSymbol(WHITE_TERRITORY,EMPTY_INTERSECTION);
		replaceSymbol(UNOWNED_TERRITORY,EMPTY_INTERSECTION);
		
		territoryMarked = false;
	}
}