package org.project.combat;

import org.project.entity.players.Player;
import org.project.entity.Entity;
import org.project.entity.enemies.Goblin;
import org.project.entity.enemies.Skeleton;
import org.project.entity.enemies.Vampire;

import java.util.Scanner;

public class CombatSystem
{

    private static final String RESET = "\u001B[0m";
    private static final String RED = "\u001B[31m";
    private static final String GREEN = "\u001B[32m";
    private static final String YELLOW = "\u001B[33m";
    private static final String BLUE = "\u001B[34m";

    private Scanner scanner = new Scanner(System.in);

    public void startBattle(Player player, Entity enemy)
    {

        System.out.println(RED + "\n⚔️ A battle has started!" + RESET);
        System.out.println(YELLOW + "Enemy: " + enemy.getName() + RESET);

        while (player.isAlive() && enemy.isAlive())
        {

            System.out.println("\n" + BLUE + "----- YOUR TURN -----" + RESET);
            System.out.println("1. Light Attack");
            System.out.println("2. Heavy Attack");
            System.out.println("3. Defend");
            System.out.println("4. Heal");
            System.out.println("5. Special Ability");
            System.out.println("6. Exit Game");

            while (!scanner.hasNextInt())
            {
                scanner.next();
                System.out.println("❌ Please enter a number between 1-6");
            }

            int choice = scanner.nextInt();

            switch (choice)
            {

                case 1:
                    player.lightAttack(enemy);
                    break;

                case 2:
                    player.heavyAttack(enemy);
                    break;

                case 3:
                    player.defend();
                    break;

                case 4:
                    player.specialAbility(enemy);
                    break;

                case 5:
                    if (player.getMP() >= 6)
                    {
                        player.heal(10);
                        player.fillMana(-6);
                        System.out.println("💚 You restored 10 HP at the cost of 6 MP!");
                    } else {
                        System.out.println("❌ Not enough MP to heal!");
                    }
                    break;

                case 6:
                    if (player.getXP() >= 10)
                    {
                        player.gainXP(-10);
                        player.fillMana(20);
                        System.out.println("🔮 +20 MP restored (Cost: 10 XP)");
                    } else {
                        System.out.println("❌ Not enough XP to restore mana!");
                    }
                    break;

                case 7:
                    if (player.getXP() >= 50)
                    {
                        player.gainXP(-50);
                        player.heal(30);
                        System.out.println("❤️ +30 HP restored (Cost: 50 XP)");
                    } else {
                        System.out.println("❌ Not enough XP to restore HP!");
                    }
                    break;

                case 8:
                    System.out.println("👋 Exiting game...");
                    System.exit(0);

                default:
                    System.out.println("❌ Invalid choice!");
            }

            if (!enemy.isAlive())
            {

                System.out.println(GREEN + "\n✅ Enemy defeated!" + RESET);

                player.gainXP(50);

                dropKey(player, enemy);

                recoverPlayer(player);

                break;
            }

            System.out.println(RED + "\n👹 ENEMY TURN!" + RESET);

            enemy.attack(player);

            if (!player.isAlive())
            {
                System.out.println(RED + "\n💀 You were defeated..." + RESET);
            }
        }
    }

    private void dropKey(Player player, Entity enemy)
    {

        double chance = Math.random();

        if (chance > 0.20) return;

        if (enemy instanceof Goblin && !player.hasGoblinKey())
        {
            System.out.println(YELLOW + "🗝️ Goblin Key obtained!" + RESET);
            player.obtainGoblinKey();
        }

        else if (enemy instanceof Skeleton && !player.hasSkeletonKey())
        {
            System.out.println(YELLOW + "🗝️ Skeleton Key obtained!" + RESET);
            player.obtainSkeletonKey();
        }

        else if (enemy instanceof Vampire && !player.hasVampireKey())
        {
            System.out.println(YELLOW + "🗝️ Vampire Key obtained!" + RESET);
            player.obtainVampireKey();
        }
    }

    private void recoverPlayer(Player player)
    {

        player.setHealth(player.getMaxHP());
        player.setMana(player.getMaxMP());

        System.out.println("\u001B[32m✨ Your HP and Mana have been fully restored!\u001B[0m");
    }
}