package org.project;

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

import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();

        System.out.println("\u001B[33m=== The Legend of Javanest ===\u001B[0m");
        System.out.println("Choose your Hero Class:");
        System.out.println("1. Knight (High Damage)");
        System.out.println("2. Wizard (High Health)");
        System.out.println("3. Assassin (High Mana)");

        int classChoice = scanner.nextInt();
        Player player;
        if (classChoice == 2) {
            player = new Wizard("Merlin");
        } else if (classChoice == 3) {
            player = new Assassin("Ezio");
        } else {
            player = new Knight("Ser Duncan");
        }

        boolean hasGoblinKey = false;
        boolean hasSkeletonKey = false;
        boolean hasVampireKey = false;
        boolean gameRunning = true;

        while (gameRunning && player.isAlive()) {
            boolean hasAllKeys = hasGoblinKey && hasSkeletonKey && hasVampireKey;

            System.out.println("\n\u001B[35mYou arrived at a new location.\u001B[0m");
            Enemy currentEnemy = spawnRandomEnemy(random);
            System.out.println("A wild " + currentEnemy.getName() + " appears!");

            boolean inLocation = true;
            while (inLocation && player.isAlive()) {
                System.out.println("\nWhat would you like to do?");
                System.out.println("1. Fight the enemy");
                System.out.println("2. Move to another location");
                if (hasAllKeys) {
                    System.out.println("\u001B[31m3. Go to the Castle to fight the Dragon\u001B[0m");
                }

                int choice = scanner.nextInt();

                if (choice == 1) {
                    fight(player, currentEnemy, scanner);
                    inLocation = false;

                    if (player.isAlive() && !currentEnemy.isAlive()) {
                        System.out.println("\u001B[32mVictory! " + currentEnemy.getName() + " was defeated.\u001B[0m");
                        player.gainXP(50);
                        player.heal(player.getMaxHP());
                        player.fillMana(player.getMaxMP());

                        if (currentEnemy instanceof Goblin && !hasGoblinKey && random.nextInt(100) < 20) {
                            System.out.println("\u001B[33m🗝️ You found the Goblin Key!\u001B[0m");
                            hasGoblinKey = true;
                        } else if (currentEnemy instanceof Skeleton && !hasSkeletonKey && random.nextInt(100) < 20) {
                            System.out.println("\u001B[33m🗝️ You found the Skeleton Key!\u001B[0m");
                            hasSkeletonKey = true;
                        } else if (currentEnemy instanceof Vampire && !hasVampireKey && random.nextInt(100) < 20) {
                            System.out.println("\u001B[33m🗝️ You found the Vampire Key!\u001B[0m");
                            hasVampireKey = true;
                        }
                    }
                } else if (choice == 2) {
                    System.out.println("You moved to a new location.");
                    inLocation = false;
                } else if (choice == 3 && hasAllKeys) {
                    System.out.println("\n\u001B[31m=== You entered the Castle! ===\u001B[0m");
                    Enemy dragon = new Dragon();
                    fight(player, dragon, scanner);
                    if (player.isAlive()) {
                        System.out.println("\u001B[33m🎉 You defeated the Dragon and saved Javanest! You Win!\u001B[0m");
                    }
                    gameRunning = false;
                    inLocation = false;
                }
            }
        }

        if (!player.isAlive()) {
            System.out.println("\u001B[31m💀 Game Over. You died.\u001B[0m");
        }
        scanner.close();
    }

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

    private static void fight(Player player, Enemy enemy, Scanner scanner) {
        System.out.println("\u001B[31mYou chose to FIGHT!\u001B[0m");
        while (player.isAlive() && enemy.isAlive()) {
            System.out.println("\n[" + player.getName() + " - " + player.getHp() + "/" + player.getMaxHP() + " HP | " + player.getMp() + "/" + player.getMaxMP() + " Mana]");
            System.out.println("[" + enemy.getName() + " - " + enemy.getHp() + "/" + enemy.getMaxHP() + " HP]");

            System.out.println("\nYour Turn:");
            System.out.println("1. Light Attack | 2. Heavy Attack (-8 Mana) | 3. Defend (-6 Mana) | 4. Heal (-12 Mana) | 5. Special Ability (-15 Mana)");
            int action = scanner.nextInt();

            if (player.isStunned()) {
                System.out.println("\u001B[33mYou are stunned and cannot move!\u001B[0m");
                player.setStunned(false);
            } else {
                switch (action) {
                    case 1: player.lightAttack(enemy); break;
                    case 2: player.heavyAttack(enemy); break;
                    case 3: player.defendAction(); break;
                    case 4: player.healAction(); break;
                    case 5: player.specialAbility(enemy); break;
                    default: player.lightAttack(enemy); break;
                }
            }

            if (enemy.isAlive()) {
                enemy.attackAction(player);
            }
        }
    }
}