package org.project;

import org.project.entity.players.Knight;
import org.project.entity.players.Player;
import org.project.item.weapons.Sword;

import java.util.Scanner;

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

    public void start() {
        while (true) {
            System.out.println("\n"+"===== Java Knight =====");
            System.out.println("Main menu");
            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("=======================");

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

    private void startMenu() {
        Player player = chooseCharacter();
        if (player == null) {
            System.out.println("Returning to the main menu...");
            start();
            return;
        }
        actions.startGame(player);
    }
    public Player chooseCharacter() {
        String name;
        Player character = null;
        System.out.println("Enter Your name: ");
        name = scanner.nextLine();
        System.out.println("\n" + "Choose a character: ");
        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, 1000, 1000, new Sword(), 5, 5);
                    System.out.println("You are a knight!");
                    keepAsking = false; break;
                case 2: character = new Knight(name);
                    System.out.println("You are a wizard!");
                    keepAsking = false; break;
                case 3: character = new Knight(name);
                    System.out.println("You are an assassin!");
                    keepAsking = false; break;
                case 0: keepAsking = false; break;
                default: System.out.println("Invalid choice, please choose a number between 0-3");
            }
        }
        return character;

    }
    private void showHelp() {
        System.out.println("\n"+"===== Java Knight =====");
        System.out.println("Help menu");
        System.out.println("What do you need help with, sir?");
        System.out.println("1-What is Java-Knight?");
        System.out.println("2-How to play");
        System.out.println("3-About characters");
        System.out.println("4-General tips");
        System.out.println("0-Back");
        System.out.println("=======================");
    }
    private int getValidIntInput() {
        while (!scanner.hasNextInt()) {
            System.out.print("Please enter a valid number: ");
            scanner.next();
        }
        int input = scanner.nextInt();
        scanner.nextLine(); // Clear buffer
        return input;
    }
}
