package org.project.game;

import org.project.entity.enemies.*;
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 org.project.location.Location;
import org.project.utilities.Colors;

import java.util.*;

public class Game {

    private final Scanner scanner = new Scanner(System.in);
    private final Random random = new Random();

    private Player player;

    private final Location[] locations = {
            new Location("Mordor", new Goblin()),
            new Location("Ancient Bones", new Skeleton()),
            new Location("Mystic Falls", new Vampire())
    };


    public void start()
    {
        System.out.println("=== JAVA KNIGHT ⚔️ ===");

        System.out.print("Enter your name: ");
        String name = scanner.nextLine();

        System.out.println("Choose your class:");
        System.out.println("1. Knight");
        System.out.println("2. Wizard");
        System.out.println("3. Assassin");

        int choice = scanner.nextInt();

        switch (choice)
        {
            case 2:
                player = new Wizard(name);
                break;
            case 3:
                player = new Assassin(name);
                break;
            default:
                player = new Knight(name);
        }

        gameLoop();
    }

    private void gameLoop()
    {

        while (player.isAlive())
        {
            System.out.println("\nChoose a location:");

            for (int i = 0; i < locations.length; i++)
                System.out.println((i + 1) + ". " + locations[i].getName());

            int locChoice = scanner.nextInt();

            Location location = locations[locChoice - 1];
            Enemy enemy = location.spawnEnemy();


            System.out.println("\n📍 Location: " + location.getName());
            System.out.println("Enemy appeared: " + enemy.getName());

            System.out.println("\n1. Fight");
            System.out.println("2. Move");

            if (player.hasAllKeys())
                System.out.println("3. Go to Castle");

            int choice = scanner.nextInt();

            if (choice == 1)
            {
                battle(enemy);

                if (!player.isAlive())
                {
                    System.out.println(Colors.RED + "GAME OVER!" + Colors.RESET);
                    return;
                }

            }
            else if (choice == 2)
            {
                System.out.println("You moved away.");
            }
            else if (choice == 3 && player.hasAllKeys())
            {
                battle(new Dragon());

                if (player.isAlive())
                {
                    System.out.println(Colors.GREEN +
                            "🐉 Dragon defeated! Peace restored to Javanest!" +
                            Colors.RESET);
                }
                else
                {
                    System.out.println(Colors.RED + "GAME OVER!" + Colors.RESET);
                }

                return;
            }
        }
    }

    private void battle(Enemy enemy)
    {
        System.out.println("\n⚔️ Battle Started!");

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

            System.out.println("\n[" + player.getName() + " - "
                    + player.getHealth() + "/" + player.getMaxHealth()
                    + " HP | "
                    + player.getMana() + "/" + player.getMaxMana()
                    + " Mana]");

            System.out.println("[" + enemy.getName() + " - "
                    + enemy.getHealth() + "/" + enemy.getMaxHealth()
                    + " HP]");

            System.out.println("\n1.Light Attack");
            System.out.println("2.Heavy Attack");
            System.out.println("3.Defend");
            System.out.println("4.Heal");
            System.out.println("5.Special Ability");

            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.heal();
                    break;
                case 5:
                    player.specialAbility(enemy);
                    break;
            }

            if (!enemy.isAlive())
                break;

            if (enemy.isStunned())
            {
                System.out.println(Colors.YELLOW + enemy.getName() + " is stunned!" + Colors.RESET);
                enemy.setStunned(false);
                continue;
            }

            enemy.attack(player);
        }

        if (player.isAlive())
        {
            System.out.println(Colors.GREEN + enemy.getName() + " defeated!" + Colors.RESET);

            player.gainXP(enemy.getXpReward());

            dropKey(enemy);

            player.restore();

            System.out.println(Colors.BLUE + "HP and Mana restored!" + Colors.RESET);
        }
    }

    private void dropKey(Enemy enemy)
    {
        if (enemy instanceof Dragon)
            return;

        int chance = random.nextInt(100);

        if (chance < 20 && !player.hasKey(enemy.getName()))
            player.addKey(enemy.getName());
    }
}
