package ch.zhaw.catan;

import org.beryx.textio.TextIO;
import org.beryx.textio.TextIoFactory;
import org.beryx.textio.TextTerminal;

import java.awt.Point;
import java.util.HashMap;

/**
 * This class performs all communication with the player, includes asking for input and showing the map.
 *
 * @author Leonardo Brandenberger
 */
public class Parser {
    private final TextIO textIO;
    private final TextTerminal<?> textTerminal;

    /**
     * Constructs a parser Object, initializes the textIO components TextTerminal and textIO.
     */
    public Parser() {
        textIO = TextIoFactory.getTextIO();
        textTerminal = textIO.getTextTerminal();
    }

    /**
     * Ask user to input two coordinates with have to be higher than 0 and returns them as a Point.
     *
     * @return Point that the user specified with an input
     */
    public Point getPoint() {
        return new Point(textIO.newIntInputReader().withMinVal(0).read("x coordinate:"),
                textIO.newIntInputReader().withMinVal(0).read("y coordinate:"));
    }

    /**
     * Takes care of displaying the gameboard that it receives as parameter.
     *
     * @param gameboard that has to be displayed
     */
    public void displayGameboard(String gameboard) {
        textTerminal.println(gameboard);
    }

    /**
     * Displays Player info that includes his current resources and his winpoint he currently owns
     *
     * @param currentPlayerResource Hashmap of all Resources with corresponding number that a player currently owns.
     * @param winpoints             that the player is currently holding
     */
    public void displayPlayerInfo(HashMap<Config.Resource, Integer> currentPlayerResource, int winpoints) {
        textTerminal.println("You are currently holding " + winpoints + " winpoints.");
        textTerminal.println("You own the follwing resources:");
        for (Config.Resource resource : currentPlayerResource.keySet()) {
            textTerminal.println(resource.name() + ":" + currentPlayerResource.get(resource));
        }
    }

    /**
     * Displays the winner of the game in the Console.
     *
     * @param winner the faction that has won
     */
    public void displayWinnertext(Config.Faction winner) {
        textTerminal.println(winner.name() + " won the game!");
    }

    /**
     * Method that is used at the start of the game to get how many players will be playing and how many winpoints
     * should be set.
     *
     * @return HashMap that includes the number of players and how many winpoints the game should have.
     */
    public HashMap<String, Integer> gameStart() {
        HashMap<String, Integer> gameStartValues = new HashMap<>();
        gameStartValues.put("NumberOfPlayers", textIO.newIntInputReader().withMinVal(Config.MIN_NUMBER_OF_PLAYERS).withMaxVal(4).read("Number of players:"));
        gameStartValues.put("NumberOfWinPoints", textIO.newIntInputReader().withMinVal(3).read("Winpoints needed for Victory:"));
        return gameStartValues;
    }

    /**
     * Asks the player for coordinates to place a structure if the structure is a road the player will be notified
     * that he will need to input two coordinates a start and a finish one.
     *
     * @param structure the structure that should be asked for coordinates
     */
    public void giveCoordinatesForStructures(Config.Structure structure) {
        textTerminal.println("Please insert coordinates for " + structure);
        if (structure == Config.Structure.ROAD) {
            textTerminal.println("Please first insert the start coordinate and when prompted again the coordinate of the end of the road.");
        }
    }

    /**
     * Displays the dice number that has been thrown.
     *
     * @param number the thrown dice number
     */
    public void thrownDices(int number) {
        textTerminal.println("Dices have been thrown, the combined value is: " + number);
    }

    /**
     * Outputs which player currently is at turn.
     *
     * @param faction the faction which turn it is
     */
    public void playerTurn(Config.Faction faction) {
        textTerminal.println("It is " + faction.name() + "'s turn.");
    }

    /**
     * Outputs error message.
     */
    public void errorMessage() {
        textTerminal.println("The command was not excecuted successfully!");
    }

    /**
     * Ask the player what action he would like to perform.
     * As possible possibilities the Enum Command is used.
     *
     * @return Command which action has been choosen
     */
    public Command getAction() {
        return textIO.newEnumInputReader(Command.class).read("What would you like to do?");
    }

    /**
     * Asks the player which resources he would like to trade.
     * As possibilites the Enum in Config.Resource.class is used.
     *
     * @param give if true ask for resource to give if false for resource to receive
     * @return Config.Resource the resource the player would like to give or receive
     */

    public Config.Resource trade(boolean give) {
        String output = "give";
        if (!give) {
            output = "receive";
        }
        return textIO.newEnumInputReader(Config.Resource.class).read("Which Resource would you like to " + output);
    }

    /**
     * Clears the instances of textIO and textTerminal.
     */
    public void quit() {
        textTerminal.dispose();
        textIO.dispose();
    }
}
