package ui;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import facade.YahtzeeGame;

public class TUI {

    public static void main(String[] args) throws IOException {

        String choice = "";
        boolean notFound = false;
        Scanner sc = new Scanner(System.in);

        System.out.println("\nYahtzee Star Wars Special!");

        while (!choice.equalsIgnoreCase("Stop")) {

            try {
                YahtzeeGame game = new YahtzeeGame() {
                };
                notFound = false;
                System.out.println("\nMain menu:");
                System.out.println(">Play");
                System.out.println(">Highscores");
                System.out.println(">Stop");
                choice = sc.nextLine();

                if (choice.equalsIgnoreCase("Highscores")) {
                    try {
                        System.out.println("\nChoose your gamemode:");
                        System.out.println(">Normal");
                        System.out.println(">Special8");
                        System.out.println("\n" + game.showHighscores(sc.nextLine()));
                    } catch (FileNotFoundException e) {
                        System.out.println("\nHighscore file not found!\n");
                        notFound = true;
                    }
                    
                    System.out.println("\n>Return");
                    if(!notFound)
                        System.out.println(">Delete");
                    choice = sc.nextLine();
                    if (choice.equalsIgnoreCase("Return")) {
                    }

                    else if (choice.equalsIgnoreCase("Delete")) {
                        System.out.println(
                                "\nAre you sure you want to delete the Highscore file?\nAs a confirmation type: 'I AM SURE': ");
                        choice = sc.nextLine();
                        if (choice.equals("I AM SURE")) {
                            game.deleteHighscores();
                            System.out.println("\nHighscore file got deleted...\nReturning to the main menu...");
                        } else {
                            System.out.println("Highscore file not deleted...\nReturning to the main menu...");
                        }

                    }

                }

                else if (choice.equalsIgnoreCase("Play")) {

                    System.out.println("\nChoose your gamemode: ");
                    System.out.println(">Normal");
                    System.out.println(">Special8");
                    game.setGamemode(sc.nextLine());

                    System.out.println("\nAmount of players: ");
                    System.out.println(">1-6");
                    game.setPlayercount(Integer.parseInt(sc.nextLine()));

                    System.out.println("\nType in the player names, lock in each name with Enter: ");
                    System.out.println(">ex. Lucas [Enter] William [Enter] Lena [Enter]");

                    String playerNames[] = new String[game.getPlayercount()];

                    for (int i = 0; i < game.getPlayercount(); i++) {
                        playerNames[i] = sc.nextLine();
                    }

                    game.setPlayerNames(playerNames);

                    while (!game.gameOverAll()) {

                        for (int i = 0; i < game.getPlayercount(); i++) {

                            if (game.gameOver(i))
                                continue;

                            System.out.println("\n\n\nIts your turn " + game.getPlayerName(i));
                            System.out.println("\nThis is your Yahtzee Box: ");
                            System.out.println("\n" + game.selectedOptionsPrinted(i) + "\n");

                            while (!game.allDicesLockedIn(i) && !game.noMoreRolls(i)) {

                                if (game.getLockedInDiceAmount(i) != 4)
                                    System.out.println("Rolling dices... ");
                                else
                                    System.out.println("Rolling the last dice...");

                                game.rollDices(i);

                                System.out.println("Free dices: " + game.getFreeDiceNumbers(i));
                                System.out.println("Locked in dices: " + game.getLockedInDiceNumbers(i));

                                if (game.autoLockInDices(i)) {
                                    System.out.println("\nNo rolls left => auto locking dices...\n");
                                }
                                if (!game.noMoreRolls(i)) {
                                    System.out.println(
                                            "Type in which free dices you want to keep \n(seperated by space, type 'none' if you dont want to keep any): ");
                                    choice = sc.nextLine();

                                    game.lockInDices(i, choice.split(" "));

                                    if ((game.getLeftRolls(i)) != 1 && !game.allDicesLockedIn(i))
                                        System.out.println("\n" + game.getLeftRolls(i) + " rolls left");
                                    else if ((game.getLeftRolls(i)) == 1 && !game.allDicesLockedIn(i))
                                        System.out.println("\n" + game.getLeftRolls(i) + " roll left");
                                }
                            }

                            System.out.println("\n\nLocked in dices: " + game.getLockedInDiceNumbers(i));
                            System.out.println("Done locking in... Here are your options: ");
                            game.evaluateAllOptions(i);
                            System.out.println("\n" + game.optionsPrinted(i));

                            if (game.onlyOneOption(i)) {
                                game.selectOption(i, game.getAllOptions(i).get(0).split(":")[0]);
                                System.out.println("Only one option possible => auto selecting option...");
                            } else {
                                System.out.println("Which option you want to select?: ");
                                game.selectOption(i, sc.nextLine());
                            }
                            if (game.gameOver(i)) {
                                System.out.println(
                                        "\n\nYour game is over, here is your total score: "
                                                + game.returnTotalPoints(i));
                            }
                        }
                    }

                    System.out.println("\n\nThe game is over, " + game.getWinnerMessage());
                    System.out.println("Saving all highscores to the highscore table...");
                    game.saveAllPlayersHighscores();
                    System.out.println(
                            "\nIf you want to stop the programm type: 'Stop', otherwise you will return to the main menu:");
                    choice = sc.nextLine();
                }
            } catch (Exception e) {
                System.out.println("\nInput not accepted... Returning to the main menu...\n");
            }
        }
        sc.close();
    }
}
