package org.project;

import org.project.entity.players.Assassin;
import org.project.entity.players.Knight;
import org.project.entity.players.Player;
import org.project.entity.players.Wizard;
import java.util.Scanner;
import static org.project.ConsoleColors.*;

public class Menu {
    private Scanner scanner = new Scanner(System.in);
    private MenuActions actions = new MenuActions();
    private Help helpMenu = new Help();

    public void start() {       //main menu, where everything starts from
        while (true) {
            System.out.println("\n" + CYAN + BOLD + "===== Java Knight =====" + RESET);
            System.out.println(YELLOW + "Main menu" + RESET);
            System.out.println("What would you like to do, sir?");
            System.out.println("1-Start a game");
            System.out.println("2-Show help and game tips.");
            System.out.println("0-Exit");
            System.out.println(CYAN + "=======================" + RESET);

            int choice = getValidIntInput();
            switch(choice) {
                case 0: return;
                case 1: startMenu(); break;
                case 2: helpMenu.showHelp(); break;
                default:
                    System.out.println(RED + "Invalid choice, please choose a number between 0-2" + RESET);
            }
        }
    }

    private void startMenu() {      //game start menu, player chooses its character then the game itself is called.
        Player player = chooseCharacter();
        if (player == null) {
            System.out.println(YELLOW + "Returning to the main menu..." + RESET);
            start();
            return;
        }
        actions.startGame(player);
    }

    public Player chooseCharacter() {
        String name;
        Player character = null;
        System.out.println(CYAN + "Enter Your name: " + RESET);
        name = scanner.nextLine();
        System.out.println("\n" + YELLOW + "Choose a character: " + RESET);
        System.out.println("1-Knight");
        System.out.println("2-Wizard");
        System.out.println("3-Assassin");
        System.out.println("0-back");

        boolean keepAsking = true;
        while (keepAsking) {
            int choice = getValidIntInput();
            switch (choice) {
                case 1: character = new Knight(name);
                    System.out.println(GREEN + "You are a knight!" + RESET);
                    keepAsking = false; break;
                case 2: character = new Wizard(name);
                    System.out.println(GREEN + "You are a wizard!" + RESET);
                    keepAsking = false; break;
                case 3: character = new Assassin(name);
                    System.out.println(GREEN + "You are an assassin!" + RESET);
                    keepAsking = false; break;
                case 0: keepAsking = false; break;
                default: System.out.println(RED + "Invalid choice, please choose a number between 0-3" + RESET);
            }
        }
        return character;
    }

    private int getValidIntInput() {        //a function to receive user's choices as console input
        while (!scanner.hasNextInt()) {
            System.out.print(RED + "Please enter a valid number: " + RESET);
            scanner.next();
        }
        int input = scanner.nextInt();
        scanner.nextLine(); // Clear buffer
        return input;
    }
}