package org.project;

import org.project.entity.Entity;
import org.project.entity.enemies.Enemy;
import org.project.entity.enemies.Skeleton;
import org.project.entity.players.Player;
import org.project.location.Location;

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

public class MenuActions {
    private ArrayList<Location> locations = new ArrayList<>();
    Scanner scanner = new Scanner(System.in);

    public void startGame(Player player) {
        while (player.getKeysCollected() < 3) {
            System.out.println("Keys collected: " + player.getKeysCollected());
            askInventory(player);
            Enemy enemy = chooseEnemy();
            Entity winner = fight(player, enemy);
            if (winner != player) {
                System.out.println("Game over!\nreturning to main menu...");
                break;
            }
        }
            askInventory(player);
    }

    private Enemy chooseEnemy() {
        while (true) {
            Location location = new Location();
            Enemy enemy = location.getEnemies().get(0);
            String enemyName = enemy.getType();
            System.out.println("====================================");
            System.out.println("You are in the " + location.getName() + ".\nYou may fight with a "
            + enemyName);
            System.out.println("Which option do you choose?" +"\n1- Fight with the "
                    + enemyName + "\n2- Skip to Another Location");
            outerLoop:
            while (true) {
                int choice = getValidIntInput();
                switch (choice) {
                    case 1:
                        return enemy;
                    case 2:
                        System.out.println("You chose not to fight...Skipping to next location...");break outerLoop;
                    default:
                        System.out.println("Invalid choice. Please choose a number (1 or 2).");
                }
            }
        }

    }

    public void addLocation(String name) {
        locations.add(new Location(name));
    }
    public Entity fight(Player player, Enemy enemy) {
        player.heal(player.getMaxHP());
        player.fillMana(player.getMaxMP());
        Entity winner = null;
        while (true) {
            playerTurn(player, enemy);
            if (enemy.getHp() == 0) {
                winner = player;
                System.out.println(player.getName() + " won the fight!");
                player.setXp(player.getXp()+5);
                System.out.println(player.getName() + " got 5 XP for winning the fight!");
                System.out.println("Keys collected: " + player.getKeysCollected());
                if (enemy instanceof Skeleton) {
                    boolean hasKey = ((Skeleton) enemy).generateKey();
                    if (hasKey) {
                        player.collectKey();
                        player.setXp(player.getXp()+20);
                        System.out.println(player.getName() + " got a key and 20 XP!");
                    }
                }
                return winner;
            }
            enemyTurn(player, enemy);
            if (player.getHp() == 0) {
                winner = enemy;
                System.out.println(winner.getType() + " won the fight!");
                return winner;
            }
        }
    }

    private void enemyTurn(Player player, Enemy enemy) {
        System.out.println("====================================");
        System.out.println("Enemy turn. Let's see what it does!");
        System.out.println("Your hp: " + player.getHp() + "/" + player.getMaxHP());
        System.out.println("Enemy hp: " + enemy.getHp() + "/" + enemy.getMaxHP());
//        System.out.println("1-Light attack, mana cost: 0");
//        System.out.println("2-Heavy attack, mana cost: ");
//        System.out.println("3-Special Ability, mana cost: ");
//        System.out.println("4-Defend, mana cost: 10");
//        System.out.println("5-Heal, mana cost: 20");
        while (true) {
            Random random = new Random();
            int choice = random.nextInt(5) + 1;
            switch (choice) {
                case 1: enemy.getWeapon().lightAttack(player); return;
                case 2: if(enemy.getMp() >= enemy.getWeapon().getManaCost()) {
                    enemy.getWeapon().heavyAttack(enemy, player); return;
                } break;
                case 3: if (enemy.getMp() >= enemy.getWeapon().getSpecialMana()){
                    enemy.getWeapon().useSpecialAbility(enemy, player); return;
                } break;
                case 4: if (enemy.getMp() >= enemy.getDefendManaCost())  {
                    enemy.defend(); return;
                } break;
                case 5: if (enemy.getMp() >= enemy.getHealManaCost()) {
                    enemy.heal(20); return;
                } break;
                default:
                    System.out.println("Invalid choice");
            }
        }
    }

    private void playerTurn(Player player, Enemy enemy) {
        System.out.println("====================================");
        System.out.println("Player turn. What do you want to do?");
        System.out.println("Your hp: " + player.getHp() + "/" + player.getMaxHP() +
                "\t Your mana: " + player.getMp() + "/" + player.getMaxMP());
        System.out.println("Enemy hp: " + enemy.getHp() + "/" + enemy.getMaxHP());
        System.out.println("1-Light attack, mana cost: 0");
        System.out.println("2-Heavy attack, mana cost: " + player.getWeapon().getManaCost());
        System.out.println("3-Special Ability, mana cost: " + player.getWeapon().getSpecialMana());
        System.out.println("4-Defend, mana cost: " + player.getDefendManaCost());
        System.out.println("5-Heal, mana cost: " + player.getHealManaCost());
        while (true) {
            int choice = getValidIntInput();
            switch (choice) {
                case 1: player.getWeapon().lightAttack(enemy);
                    System.out.println("You chose light attack.");return;
                case 2: if(player.getMp() >= player.getWeapon().getManaCost()) {
                    player.getWeapon().heavyAttack(player, enemy);
                    System.out.println("You chose heavy attack.");return;
                } else {
                    System.out.println("Not enough mana. Choose Again: "); break;
                }
                case 3: if (player.getMp() >= player.getWeapon().getSpecialMana()){
                    player.getWeapon().useSpecialAbility(player, enemy);
                    System.out.println("You chose special ability.");return;
                } else {
                    System.out.println("Not enough mana. Choose Again: "); break;
                }
                case 4: if (player.getMp() >= player.getDefendManaCost())  {
                    player.defend();
                    System.out.println("You chose to defend."); return;
                } else {
                    System.out.println("Not enough mana. Choose Again: "); break;
                }
                case 5: if (player.getMp() >= player.getHealManaCost()) {
                    player.heal(20); return;
                } else {
                    System.out.println("Not enough mana. Choose Again:");
                    System.out.println("You chose to heal yourself.");break;
                }
                default:
                    System.out.println("Invalid choice");
            }
        }
    }

    private void askInventory(Player player) {
        System.out.println("Choose an option: ");
        System.out.println("1- Go to the inventory");
        System.out.println("2- Continue to next fight");
        while (true) {
            int choice = getValidIntInput();
            switch (choice) {
                case 1: shop(player);
                case 2: return;
                default:
                    System.out.println("Invalid choice, please choose a number (1 or 2)."); break;
            }
        }


    }

    private void shop(Player player) {
        while (true) {
            System.out.println("=====Shop=====");
            System.out.println("You have " + player.getXp() + " XP");
            System.out.println("Each experience point can be converted into either 1 mana or 1 hp");
            System.out.println("Which option do you choose?");
            System.out.println("1- Buy hp");
            System.out.println("2- Buy mana");
            System.out.println("0- Back");
            switchLoop:
            while(true) {
                int choice = getValidIntInput();
                switch (choice) {
                    case 1:
                        while (true) {
                            System.out.println("Enter amount: " + "\tShould be less than or equal to " + player.getXp());
                            int amount = getValidIntInput();
                            if (amount <= player.getXp()) {
                                player.buyHp(amount);
                                System.out.println("Successfully bought " + amount + " hp.");
                                System.out.println("Current hp and mana: " + "\thp: " + player.getMaxHP() +"\tmana: " + player.getMaxMP());
                                break switchLoop;
                            } else if (amount > player.getXp()) {
                                System.out.println("Not enough XP");
                            }
                        }
                    case 2:
                        while (true) {
                            System.out.println("Enter amount: " + "\tShould be less than or equal to " + player.getXp());
                            int amount = getValidIntInput();
                            if (amount <= player.getXp()) {
                                player.buyMp(amount);
                                System.out.println("Successfully bought " + amount + " mana.");
                                System.out.println("Current maximum hp and mana: " + "\thp: " + player.getMaxHP() +"\tmana: " + player.getMaxMP());
                                break switchLoop;
                            } else if (amount > player.getXp()) {
                                System.out.println("Not enough XP");
                            }
                        }
                    case 0: return;
                    default:
                        System.out.println("Invalid choice. Please choose a number from 0-2.");
                }
            }
        }
    }


    private int getValidIntInput() {
        while (!scanner.hasNextInt()) {
            System.out.print("Please enter a valid number: ");
            scanner.next();
        }
        int input = scanner.nextInt();
        scanner.nextLine(); // Clear buffer
        return input;
    }
}
