package org.project;

import org.project.entity.enemies.*;
import org.project.entity.players.*;
import org.project.item.armors.*;
import org.project.item.weapons.*;
import org.project.location.Location;

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

public class Main {
    private static Scanner in = new Scanner(System.in);
    private static Random random = new Random();
    private static String playerClass = "";
    private static int totalKeys = 0; 
    
    public static void main(String[] args) {
        System.out.println("Enter your name:");
        String name = in.next();
        
        System.out.println("choose your character:\n1.Knight 2.Wizard 3.Assassin");
        int choice = in.nextInt();
        
        Player player = null;
        
        switch (choice) {
            case 1:
                player = new Knight(name);
                playerClass = "knight";
                break;
            case 2:
                player = new Wizard(name);
                playerClass = "wizard";
                break;
            case 3:
                player = new Assassin(name);
                playerClass = "assassin";
                break;
            default:
                player = new Knight(name);
                playerClass = "knight";
        }
        
        final Player finalPlayer = player;
        
        int menuChoice = 0;
        while (menuChoice != 3) {
            clearScreen();
            displayMenu();
            menuChoice = in.nextInt();
            
            switch (menuChoice) {
                case 1:
                    newGame(1, finalPlayer);
                    break;
                case 2:
                    newGame(2, finalPlayer);
                    break;
                case 3:
                    System.out.println("\n👋 GOODBYE!");
                    break;
            }
        }
    }
    
    public static void displayMenu() {
        System.out.println("╔══════════════════════════════════════════════════════╗");
        System.out.println("║                   🗝️  KEYS: " + totalKeys + " 🗝️                    ║");
        System.out.println("╠══════════════════════════════════════════════════════╣");
        System.out.println("║                                                      ║");
        System.out.println("║            ⚔️  [1] NEW GAME          ⚔️              ║");
        System.out.println("║            🐉  [2] VS DRAGON         🐉              ║");
        System.out.println("║            ❌  [3] EXIT              ❌              ║");
        System.out.println("║                                                      ║");
        System.out.println("╚══════════════════════════════════════════════════════╝");
        System.out.print("➡️  CHOOSE: ");
    }
    
    public static void newGame(int model, Player player) {
        Enemy enemy = null;

        //جنگ با دشمن رندوم
        if (model == 1) {
            Location loc=new Location();
            String location = loc.chooseLocation();
            System.out.println("📍 You are in " + location);
            
            if (loc.thereIsKey()) {
                System.out.println("🔑 There is a Key here!");
            }
            delay(2500);
            
            int chooseEnemy = random.nextInt(3);
            switch (chooseEnemy) {
                case 0:
                    enemy = new Skeleton();
                    System.out.println("You're fighting with skeleton \uD83D\uDC80");
                    break;
                case 1:
                    enemy = new Vampier();
                    System.out.println("You're fighting with Vampire \uD83E\uDDDB");
                    break;
                case 2:
                    enemy = new Orc();
                    System.out.println("You're fighting with Orc \uD83D\uDC79");
                    break;
            }
            delay(3000);
            
            startBattle(player, enemy,loc);

        //جنگ با اژدها
        } else {
            if (totalKeys >= 3) {
                enemy = new Dragon();
                System.out.println("🐉 YOU ARE IN DRAGON'S HOUSE! 🐉");
                System.out.println(" You used 3 keys!");
                totalKeys -= 3;
                Location loc=new Location(1);// تابع سازنده اژدها یک عدد ورودی دریافت میکنه
                delay(2000);
                
                startBattle(player, enemy, loc);
                
                if (enemy.getHp() <= 0) {
                    System.out.println("🏆 LEGEND! YOU DEFEATED THE DRAGON! 🏆");
                }

            //اگر 3 کلید نداشت
            } else {
                System.out.println("╔══════════════════════════════════════════════════════╗");
                System.out.println("║  ❌ YOU NEED 3 KEYS TO FIGHT THE DRAGON! ❌          ║");
                System.out.printf("║         🔑 YOUR KEYS: %d / 3                         ║\n", totalKeys);
                System.out.println("║     ⚔️  Defeat enemies in NEW GAME to get keys!     ║");
                System.out.println("╚══════════════════════════════════════════════════════╝");
                delay(2000);
            }
        }
    }
    
    public static void startBattle(Player player, Enemy enemy, Location loc) {
        while (player.getHp() > 0 && enemy.getHp() > 0) {
            clearScreen();
            displayBattle(player.getHp(), player.getMaxHP(), player.getMp(), player.getMaxMP(),
                         enemy.getHp(), enemy.getMaxHP(), player.getArmor().getDurability(),
                         player.getArmor().getDefense(), playerClass);
            
            int playerChoice = in.nextInt();
            clearScreen();

            System.out.println("You:");
            switch (playerChoice) {
                case 1:
                    player.LightAttack(enemy);
                    break;
                case 2:
                    if (player.getMp() > 0) {
                        player.HeavyAttack(enemy);
                    } else
                    {
                        System.out.println("Your MP is out.");
                        player.LightAttack(enemy); 
                    }
                    break;
                case 3:
                    if(player.getMp()>0)
                        player.defend();
                    else
                    {
                        System.out.println("Your MP is out.");
                        player.LightAttack(enemy);
                    }
                    break;
                case 4:
                    if(player.getMp()>0)
                         player.SpecialAttack(enemy,1);
                    else
                    {
                        System.out.println("Your MP is out.");
                        player.LightAttack(enemy);
                    }
                    break;
                case 5:
                    if(player.getMp()>0)
                        player.heal();
                    else
                    {
                        System.out.println("Your MP is out.");
                        player.LightAttack(enemy);
                    }
                    break;
            }
            
            delay(2500);
            
            if (enemy.getHp() > 0) {
                clearScreen();
                System.out.println("Enemy: ");
                int enemyMove = random.nextInt(3);
                switch (enemyMove) {
                    case 0:
                        if(!player.getIsDefending()) {
                            enemy.LightAttack(player);
                            System.out.println("The enemy made a light attack \uD83D\uDC4A");
                        }
                        else {
                            player.defend();
                            System.out.println("The enemy attacked, but you defended.\uD83D\uDCAF");
                        }
                        break;
                    case 1:
                        enemy.heal();
                        System.out.println("The enemy healed. \uD83E\uDE84");
                        break;
                    case 2:
                        if(!player.getIsDefending())
                        {
                            if (enemy instanceof Vampier) {
                                enemy.HeavyAttack(player);
                                System.out.println("The Vampire made a heavy attack \uD83E\uDE78");
                            } else if (enemy instanceof Dragon) {
                                ((Dragon) enemy).burn(player);
                                System.out.println("The dragon breathed fire \uD83D\uDD25\uD83D\uDD25");
                            } else {
                                enemy.LightAttack(player);
                                System.out.println("The enemy made a light attack \uD83D\uDC4A");
                            }
                        }
                        else
                        {
                            player.defend();
                            System.out.println("The enemy attacked, but you defended.\uD83D\uDCAF");
                        }
                        break;
                }
                delay(2500);
            }
        }
        
        clearScreen();
        if (enemy.getHp() <= 0) {
            System.out.println("╔══════════════════════════════════════════════════════╗");
            System.out.println("║                    🎉 YOU WON! 🎉                     ║");
            System.out.println("╚══════════════════════════════════════════════════════╝");
            
            if (loc.thereIsKey()) {
                totalKeys++;
                System.out.println("🔑 You found a KEY on the enemy!");
                System.out.println("🗝️  Total keys: " + totalKeys);
            }
        } else {
            System.out.println("╔══════════════════════════════════════════════════════╗");
            System.out.println("║                    💀 YOU LOST! 💀                    ║");
            System.out.println("╚══════════════════════════════════════════════════════╝");
        }
        delay(2500);
        player.fillHP();
        player.fillHP();
        player.filldurability();
    }
    
    
    public static void displayBattle(int playerHP, int playerMaxHP, int playerMP, int playerMaxMP,
                                     int enemyHP, int enemyMaxHP, int ArmorDurability, int ArmorDefense, String playerClass) {
        
        String playerEmoji = "";
        if (playerClass.equals("wizard")) {
            playerEmoji = "🧙";
        } else if (playerClass.equals("knight")) {
            playerEmoji = "⚔️";
        } else if (playerClass.equals("assassin")) {
            playerEmoji = "🗡️";
        } else {
            playerEmoji = "🧝";
        }
        
        System.out.println("╔══════════════════════════════════════════════════════════════════════╗");
        System.out.println("║                       ⚔️  BATTLE START!  ⚔️                         ║");
        System.out.println("╠═══════════════════════╦══════════════════════════════════════════════╣");
        System.out.printf("║  %s %-18s║  👾 %-30s║\n", playerEmoji, playerClass.toUpperCase(), "ENEMY");
        System.out.println("╠═══════════════════════╬══════════════════════════════════════════════╣");
        System.out.printf("║  ❤️  HP: %3d/%-3d   ║  ❤️  HP: %3d/%-3d                        ║\n",
                playerHP, playerMaxHP, enemyHP, enemyMaxHP);
        System.out.printf("║  💙  MP: %3d/%-3d   ║  ⚔️  %-40s║\n",
                playerMP, playerMaxMP, "");
        System.out.printf("║   🛡️  Armor:  Durability: %3d   Defense: %3d                        ║\n",
                ArmorDurability,
                ArmorDefense);
        System.out.println("╚═══════════════════════╩══════════════════════════════════════════════╝");
        
        System.out.println("\n📍 ACTIONS:");
        System.out.println("╔══════════════════════════════════════════════════════════════════════╗");
        System.out.println("║  [1] ⚡ Light Attack    [2] 💥 Heavy Attack    [3] 🛡️  Defend       ║");
        System.out.println("║  [4] ✨ Special Attack  [5] 💊 Heal                                 ║");
        System.out.println("╚══════════════════════════════════════════════════════════════════════╝");
        System.out.print("\n➡️  Choose: ");
    }
    
    public static void clearScreen() {
        System.out.print("\033[H\033[2J");
        System.out.flush();
    }
    
    public static void delay(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}