package com.tiedup.remake.entities;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;

/**
 * Represents a single trade offer from a Kidnapper Merchant.
 *
 * Price is in gold ingots and/or gold nuggets.
 * Example: 3x Gold Ingot + 5x Gold Nugget for 1x Ropes
 */
public class MerchantTrade {

    private final ItemStack item;
    private final int ingotPrice;
    private final int nuggetPrice;

    public MerchantTrade(ItemStack item, int ingotPrice, int nuggetPrice) {
        this.item = item.copy();
        this.ingotPrice = Math.max(0, ingotPrice);
        this.nuggetPrice = Math.max(0, nuggetPrice);
    }

    /**
     * Check if the player can afford this trade with the given stacks.
     *
     * @param ingots Stack of gold ingots (or empty)
     * @param nuggets Stack of gold nuggets (or empty)
     * @return true if player has enough gold
     */
    public boolean canAfford(ItemStack ingots, ItemStack nuggets) {
        int ingotCount = ingots.is(Items.GOLD_INGOT) ? ingots.getCount() : 0;
        int nuggetCount = nuggets.is(Items.GOLD_NUGGET)
            ? nuggets.getCount()
            : 0;

        return ingotCount >= ingotPrice && nuggetCount >= nuggetPrice;
    }

    /**
     * Consume the payment from the given stacks.
     * MUST call canAfford() before this!
     *
     * @param ingots Stack of gold ingots
     * @param nuggets Stack of gold nuggets
     */
    public void consumePayment(ItemStack ingots, ItemStack nuggets) {
        if (ingots.is(Items.GOLD_INGOT)) {
            ingots.shrink(ingotPrice);
        }
        if (nuggets.is(Items.GOLD_NUGGET)) {
            nuggets.shrink(nuggetPrice);
        }
    }

    /**
     * Get the item being sold.
     * @return Copy of the item stack
     */
    public ItemStack getItem() {
        return item.copy();
    }

    /**
     * Get display name of the item.
     */
    public Component getItemName() {
        return item.getHoverName();
    }

    /**
     * Get ingot price.
     */
    public int getIngotPrice() {
        return ingotPrice;
    }

    /**
     * Get nugget price.
     */
    public int getNuggetPrice() {
        return nuggetPrice;
    }

    /**
     * Get formatted price display.
     * Examples:
     * - "5 gold + 12 nuggets"
     * - "3 gold"
     * - "8 nuggets"
     */
    public Component getPriceDisplay() {
        if (ingotPrice > 0 && nuggetPrice > 0) {
            return Component.translatable(
                "entity.tiedup.trade.price_both", ingotPrice, nuggetPrice
            );
        } else if (ingotPrice > 0) {
            return Component.translatable("entity.tiedup.trade.price_gold", ingotPrice);
        } else if (nuggetPrice > 0) {
            return Component.translatable("entity.tiedup.trade.price_nuggets", nuggetPrice);
        } else {
            return Component.translatable("entity.tiedup.trade.price_free");
        }
    }

    /**
     * Save this trade to NBT.
     */
    public CompoundTag save() {
        CompoundTag tag = new CompoundTag();
        tag.put("Item", item.save(new CompoundTag()));
        tag.putInt("IngotPrice", ingotPrice);
        tag.putInt("NuggetPrice", nuggetPrice);
        return tag;
    }

    /**
     * Load a trade from NBT.
     */
    public static MerchantTrade load(CompoundTag tag) {
        ItemStack item = ItemStack.of(tag.getCompound("Item"));
        int ingotPrice = tag.getInt("IngotPrice");
        int nuggetPrice = tag.getInt("NuggetPrice");
        return new MerchantTrade(item, ingotPrice, nuggetPrice);
    }

    @Override
    public String toString() {
        return (
            item.getHoverName().getString() +
            " for " +
            getPriceDisplay().getString()
        );
    }
}
