package net.woodyfolsom.msproj;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class GameSettings {
	private String playerOne = "UCT";
	private String playerTwo = "UCT";
	private long guiDelay = 1000L;
	private long turnTime = 4000L;
	private int boardSize = 9;
	private double komi = 6.5;
	private int numGames = 10;
	private boolean spectatorBoardShown = false;
	private boolean whiteMoveLogged = true;
	private boolean blackMoveLogged = true;
	
	private GameSettings() {	
	}
	
	public static GameSettings createGameSetings(String fileName) throws IOException {
		File file = new File(fileName);
		FileReader fileReader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		
		GameSettings gameSettings = new GameSettings();
		
		String line;
		System.out.println("Parsing game settings file: " + file.getPath());
		
		while ((line = bufferedReader.readLine()) != null) {
			String[] linePlusComment = line.split("//");
			String[] nameAndValue = linePlusComment[0].split("=");
			String name = nameAndValue[0].trim();
			String value = nameAndValue[1].trim();
			
			if ("PlayerOne".equals(name)) {
				gameSettings.setPlayerOne(value);
			} else if ("PlayerTwo".equals(name)) {
				gameSettings.setPlayerTwo(value);
			} else if ("PlayerOne".equals(name)) {
				gameSettings.setPlayerOne(value);
			} else if ("GUIDelay".equals(name)) {
				gameSettings.setGuiDelay(Long.parseLong(value));
			} else if ("TurnTime".equals(name)) {
				gameSettings.setTurnTime(Long.parseLong(value));
			} else if ("BoardSize".equals(name)) {
				gameSettings.setBoardSize(Integer.parseInt(value));
			} else if ("NumGames".equals(name)) {
				gameSettings.setNumGames(Integer.parseInt(value));
			} else if ("Komi".equals(name)) {
				gameSettings.setKomi(Double.parseDouble(value));
			} else if ("SpectatorBoardShown".equals(name)) {
				gameSettings.setSpectatorBoardShown(Boolean.parseBoolean(value));
			} else if ("WhiteMoveLogged".equals(name)) {
				gameSettings.setWhiteMoveLogged(Boolean.parseBoolean(value));
			} else if ("BlackMoveLogged".equals(name)) {
				gameSettings.setBlackMoveLogged(Boolean.parseBoolean(value));
			} else {
				System.out.println("Ignoring game settings property with unrecognized name: " + name);
			}
		}
		
		bufferedReader.close();
		
		return gameSettings;
	}

	public String getPlayerOne() {
		return playerOne;
	}

	public String getPlayerTwo() {
		return playerTwo;
	}

	public long getGuiDelay() {
		return guiDelay;
	}

	public long getTurnTime() {
		return turnTime;
	}

	public int getBoardSize() {
		return boardSize;
	}

	public double getKomi() {
		return komi;
	}

	public int getNumGames() {
		return numGames;
	}

	private void setPlayerOne(String playerOne) {
		this.playerOne = playerOne;
	}

	private void setPlayerTwo(String playerTwo) {
		this.playerTwo = playerTwo;
	}

	private void setGuiDelay(long guiDelay) {
		this.guiDelay = guiDelay;
	}

	private void setTurnTime(long turnTime) {
		this.turnTime = turnTime;
	}

	private void setBoardSize(int boardSize) {
		this.boardSize = boardSize;
	}

	private void setKomi(double komi) {
		this.komi = komi;
	}

	private void setNumGames(int numGames) {
		this.numGames = numGames;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("PlayerOne=" + playerOne);
		sb.append(", PlayerTwo=" + playerTwo);
		sb.append(", BoardSize=" + boardSize);
		sb.append(", Komi=" + komi);
		sb.append(", NumGames=" + numGames);
		sb.append(", TurnTime=" + turnTime);
		sb.append(", GUIDelay=" + guiDelay);
		return sb.toString();
	}

	public boolean isSpectatorBoardShown() {
		return spectatorBoardShown;
	}

	private void setSpectatorBoardShown(boolean spectatorBoardShown) {
		this.spectatorBoardShown = spectatorBoardShown;
	}

	public boolean isWhiteMoveLogged() {
		return whiteMoveLogged;
	}

	private void setWhiteMoveLogged(boolean whiteMoveLogged) {
		this.whiteMoveLogged = whiteMoveLogged;
	}

	public boolean isBlackMoveLogged() {
		return blackMoveLogged;
	}

	private void setBlackMoveLogged(boolean blackMoveLogged) {
		this.blackMoveLogged = blackMoveLogged;
	}
	
}