package org.project;

import org.project.entity.players.Player;
import org.project.item.consumables.Flask;
import org.project.item.consumables.ManaPotion;
import org.project.item.weapons.GreatAxe;
import org.project.item.weapons.Weapon;
import org.project.util.ConsoleColors;

import java.util.Scanner;

/**
 * Bonus feature: a shop where the player spends coins earned in battle on
 * consumables, a stronger weapon, or armor repairs.
 */
public class Merchant {

    private static final int REPAIR_COST = 15;

    private final Scanner scanner;

    public Merchant(Scanner scanner) {
        this.scanner = scanner;
    }

    public void visit(Player player) {
        System.out.println(ConsoleColors.yellow("\n🧙 Merchant: \"Welcome, traveler. Coin buys survival out here.\""));

        boolean shopping = true;
        while (shopping) {
            System.out.println(ConsoleColors.bold("\n--- Merchant's Wares ---"));
            System.out.println("You have " + ConsoleColors.yellow(player.getCoins() + " coins") + ".");
            System.out.println("1. Health Flask (heals 25 HP) - 20 coins");
            System.out.println("2. Mana Potion (restores 20 Mana) - 20 coins");
            System.out.println("3. Great Axe (12 damage weapon) - 80 coins");
            System.out.println("4. Repair Armor - " + REPAIR_COST + " coins");
            System.out.println("5. Leave");

            int choice = readChoice(1, 5);
            switch (choice) {
                case 1 -> buyConsumable(player, new Flask(), 20);
                case 2 -> buyConsumable(player, new ManaPotion(), 20);
                case 3 -> buyWeapon(player, new GreatAxe());
                case 4 -> repairArmor(player);
                case 5 -> shopping = false;
            }
        }
        System.out.println(ConsoleColors.yellow("🧙 Merchant: \"Safe travels, hero.\"\n"));
    }

    private void buyConsumable(Player player, org.project.item.consumables.Consumable item, int price) {
        if (player.spendCoins(price)) {
            player.addConsumable(item);
            System.out.println(ConsoleColors.green("Bought a " + item.getName() + "! Added to your inventory."));
        } else {
            notEnough();
        }
    }

    private void buyWeapon(Player player, Weapon weapon) {
        if (player.spendCoins(weapon.getPrice())) {
            player.setWeapon(weapon);
            System.out.println(ConsoleColors.green("You equip the " + weapon.getName()
                    + " (" + weapon.getDamage() + " damage)!"));
        } else {
            notEnough();
        }
    }

    private void repairArmor(Player player) {
        if (player.spendCoins(REPAIR_COST)) {
            player.getArmor().repair();
            System.out.println(ConsoleColors.green("Your " + player.getArmor().getName() + " is fully repaired."));
        } else {
            notEnough();
        }
    }

    private void notEnough() {
        System.out.println(ConsoleColors.red("Not enough coins!"));
    }

    private int readChoice(int min, int max) {
        while (true) {
            System.out.print("> ");
            if (!scanner.hasNextLine()) {
                return max; // treat end-of-input as "Leave"
            }
            try {
                int value = Integer.parseInt(scanner.nextLine().trim());
                if (value >= min && value <= max) {
                    return value;
                }
            } catch (NumberFormatException ignored) {
                // fall through and re-prompt
            }
            System.out.println("Please enter a number between " + min + " and " + max + ".");
        }
    }
}
