package org.project;

import org.project.entity.enemies.*;
import org.project.entity.players.*;

import java.util.*;

public class Main {
    private static final Set<String> collectedKeys = new HashSet<>();
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("\u001B[33m⚔️ WELCOME TO JAVA KNIGHT: THE LEGEND OF JAVANEST ⚔️\u001B[0m\n");

        System.out.println("Choose your Class:");
        System.out.println("1. Knight (Highest Damage | Special: Shield Bash)");
        System.out.println("2. Wizard (Highest HP | Special: Devastating Spell)");
        System.out.println("3. Assassin (Highest Mana | Special: Invisibility & Critical Hit)");
        System.out.print("→ Choice: ");

        int choice = scanner.nextInt();
        scanner.nextLine();
        System.out.print("Enter your Hero's name: ");
        String name = scanner.nextLine();

        Player player;
        if (choice == 2) player = new Wizard(name);
        else if (choice == 3) player = new Assassin(name);
        else player = new Knight(name);

        System.out.println("\n\u001B[32mWelcome " + player.getName() + "! Your journey begins...\u001B[0m\n");

        while (player.isAlive()) {
            System.out.println("\n------------------------------------------------");
            System.out.println("🔑 Keys Collected: " + collectedKeys.size() + "/3 " + collectedKeys);
            System.out.println("1. Explore Location (Fight Monster)");
            System.out.println("2. Move to Another Location");
            if (collectedKeys.size() >= 3) {
                System.out.println("\u001B[31m3. Enter the Castle to Fight the Dragon! 🏰🐉\u001B[0m");
            }

            System.out.print("→ Choose Action: ");
            int menuChoice = scanner.nextInt();

            if (menuChoice == 1) {
                Enemy enemy = getRandomEnemy();
                startCombat(player, enemy);
            } else if (menuChoice == 2) {
                System.out.println("🏃 You moved to a new location...");
            } else if (menuChoice == 3 && collectedKeys.size() >= 3) {
                System.out.println("\n\u001B[31m🔥 ENTERING THE DRAGON'S LAIR... 🔥\u001B[0m");
                startCombat(player, new Dragon());
                if (player.isAlive()) {
                    System.out.println("\n\u001B[32m🎉 VICTORY! You defeated the Dragon and saved Javanest!\u001B[0m");
                }
                break;
            }
        }

        if (!player.isAlive()) {
            System.out.println("\n\u001B[31m💀 GAME OVER! You have fallen in combat.\u001B[0m");
        }
    }

    private static Enemy getRandomEnemy() {
        int rand = new Random().nextInt(3);
        if (rand == 0) return new Goblin();
        if (rand == 1) return new Skeleton();
        return new Vampire();
    }

    private static void startCombat(Player player, Enemy enemy) {
        System.out.println("\n⚔️ \u001B[31mA wild " + enemy.getName() + " appeared!\u001B[0m");

        while (player.isAlive() && enemy.isAlive()) {
            System.out.println("\n[" + player.getName() + " - \u001B[32m" + player.getHp() + "/" + player.getMaxHP() + " HP\u001B[0m | \u001B[34m" + player.getMp() + "/" + player.getMaxMP() + " Mana\u001B[0m]");
            System.out.println("[" + enemy.getName() + " - \u001B[31m" + enemy.getHp() + "/" + enemy.getMaxHP() + " HP\u001B[0m]");
            System.out.println("---");
            System.out.println("Your Turn:");
            System.out.println("1. Light Attack | 2. Heavy Attack (-8 Mana) | 3. Defend (-6 Mana) | 4. Heal (-12 Mana) | 5. Special Ability");
            System.out.print("→ Chose: ");

            int action = scanner.nextInt();
            switch (action) {
                case 1 -> player.lightAttack(enemy);
                case 2 -> player.heavyAttack(enemy);
                case 3 -> player.defend();
                case 4 -> player.castHeal();
                case 5 -> player.specialAbility(enemy);
                default -> player.lightAttack(enemy);
            }

            if (enemy.isAlive()) {
                System.out.println("\n" + enemy.getName() + "'s Turn:");
                enemy.attack(player);
            }
        }

        if (player.isAlive()) {
            System.out.println("\n\u001B[32m🏆 You defeated the " + enemy.getName() + "!\u001B[0m");
            player.addXp(enemy.getXpReward());

            if (!(enemy instanceof Dragon)) {
                String key = enemy.getKeyType();
                if (!collectedKeys.contains(key) && Math.random() < 0.5) {
                    collectedKeys.add(key);
                    System.out.println("\u001B[33m🔑 KEY DROPPED! Obtained: " + key + "\u001B[0m");
                }
            }
            player.restoreStats();
        }
    }
}