package org.project;

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.item.armors.Armor;
import org.project.item.armors.KnightArmor;
import org.project.item.weapons.Sword;
import org.project.item.weapons.Weapon;
import org.project.location.Location;

import java.util.Scanner;

public class Main {

    public static void printBorder() {
        for (int i = 0; i < 40; i++)
            System.out.print("-");
        System.out.println();
    }

    public static void printBorder(int x) {
        for (int i = 0; i < 15; i++)
            System.out.println();
        for (int i = 0; i < x; i++)
            System.out.print("-");
        System.out.println();
    }

    public static int moveSelector() {
        System.out.println("Choose your move:");
        System.out.println("1. Light attack (Mana: 0) | 2. Normal attack (mana: 2) | 3. Heavy attack (mana: 3)");
        System.out.println("4. Heal (5 mana) | 5. Skip");
        Scanner in = new Scanner(System.in);
        while (true) {
            int answer = in.nextInt();
            if (answer == 1 || answer == 2 || answer == 3 || answer == 4 || answer == 5)
                return answer;
            System.out.println("Invalid input try again.");
        }
    }

    private static void fightEnemy(Player player, Location location, String enemyName, String locationDesc) {
        String RED = "\u001B[31m";
        String YELLOW = "\u001B[33m";
        String RESET = "\u001B[0m";

        System.out.printf("You're in the %s fighting a %s%s%s!\n", locationDesc, RED, enemyName, RESET);

        while (player.isAlive() && location.getEnemy().getHP() > 0) {
            // Player's turn: up to 5 actions
            for (int i = 0; i < 5; i++) {
                System.out.printf("%s | %sHP%s: %d, %sMP%s: %d\n",
                        player.getName(), RED, RESET, player.getHP(), YELLOW, RESET, player.getMP());
                printBorder();
                System.out.printf("%s | %sHP%s: %d, %sMP%s: %d\n",
                        enemyName, RED, RESET, location.getEnemy().getHP(), YELLOW, RESET, location.getEnemy().getMP());
                System.out.println();

                switch (moveSelector()) {
                    case 1: // Base Attack
                        location.getEnemy().takeDamage(player.getBaseDamage());
                        break;
                    case 2: // Normal Attack (2 mana)
                        if (player.useMana(2)) {
                            player.attack(location.getEnemy());
                        } else {
                            System.out.println("Not enough mana for Normal Attack!");
                            i--;
                        }
                        break;
                    case 3: // Heavy Attack (3 mana)
                        if (player.useMana(3)) {
                            player.heavyAttack(location.getEnemy());
                        } else {
                            System.out.println("Not enough mana for Heavy Attack!");
                            i--;
                        }
                        break;
                    case 4: // Heal (5 mana)
                        if (player.useMana(5)) {
                            player.heal(20);
                        } else {
                            System.out.println("Not enough mana to Heal!");
                            i--;
                        }
                        break;
                }
                printBorder();

                // If enemy died or player died, end the player's turn early
                if (location.getEnemy().getHP() <= 0 || !player.isAlive()) {
                    location.getEnemy().getKilled();
                    i = 5;
                }
            }

            // Enemy's turn
            if (location.getEnemy().getHP() > 0) {
                for (int i = 0; i < 5; i++) {
                    location.getEnemy().attack(player);
                    printBorder();
                    if (!player.isAlive())
                        break;
                }
            }

            // Restore mana after each full round
            player.fillMana(6);
        }
    }

    public static void main(String[] args) {
        String RED = "\u001B[31m";
        String YELLOW = "\u001B[33m";
        String RESET = "\u001B[0m";
        String GREEN = "\u001B[32m";

        printBorder();
        Scanner input = new Scanner(System.in);
        System.out.println("Enter your name:");
        String name = input.next();
        printBorder();

        Weapon sword = new Sword();
        Armor armor = new KnightArmor();
        Player player = null;

        while (true) {
            System.out.println("Chose your character:");
            System.out.printf("1. Knight   | %sHP%s: 100, %sMP%s: 10\n", RED, RESET, YELLOW, RESET);
            System.out.printf("2. Wizard   | %sHP%s: 150, %sMP%s: 10\n", RED, RESET, YELLOW, RESET);
            System.out.printf("3. Assassin | %sHP%s: 80 , %sMP%s: 20\n", RED, RESET, YELLOW, RESET);
            player = switch (input.nextInt()) {
                case 1 -> new Knight(name, sword, armor);
                case 2 -> new Wizard(name, sword, armor);
                case 3 -> new Assassin(name, sword, armor);
                default -> null;
            };
            if (player == null) {
                System.out.println("Invalid input try again.");
            } else
                break;
        }

        Location overworld = new Location("Overworld", new Vampire(sword));
        Location village = new Location("Village", new Goblin(sword));
        Location nether = new Location("Nether", new Skeleton(sword));
        Location end = new Location("End", new Dragon());

        // Fight each enemy
        fightEnemy(player, overworld, "Vampire", "Overworld");
        if (!player.isAlive()) {
            System.out.println("You lost!");
            return;
        }

        fightEnemy(player, village, "Goblin", "Village");
        if (!player.isAlive()) {
            System.out.println("You lost!");
            return;
        }

        fightEnemy(player, nether, "Skeleton", "Nether");
        if (!player.isAlive()) {
            System.out.println("You lost!");
            return;
        }
        fightEnemy(player, end, "Dragon","End");
        if (!player.isAlive()) {
            System.out.println("You lost!");
            return;
        }
        printBorder();
        System.out.println(GREEN + "                                          " + RESET);
        System.out.println(RED + "      VICTORY!                         " + RESET);
        System.out.println(GREEN + "   You have conquered all enemies!        " + RESET);
        System.out.println(GREEN + "   The realm is safe, brave warrior.      " + RESET);
        System.out.println();
        printBorder();

    }
}