package com.tiedup.remake.entities;

import com.tiedup.remake.items.ModItems;
import com.tiedup.remake.v2.bondage.datadriven.DataDrivenBondageItem;
import net.minecraft.resources.ResourceLocation;
import com.tiedup.remake.v2.bondage.IV2BondageItem;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

/**
 * Manages capture equipment for EntityKidnapper.
 *
 * <p>Handles the themed bondage items used during capture sequences:
 * <ul>
 *   <li>Bind (always) - Main hand during capture</li>
 *   <li>Gag (probability) - Off hand during capture</li>
 *   <li>Mittens, Earplugs, Blindfold (probability) - Applied after capture</li>
 * </ul>
 *
 * <p>The theme and items are selected at spawn via {@link KidnapperItemSelector}
 * and stored in the kidnapper's {@code itemSelection} field.
 *
 * <p>Note: This class does NOT handle persistence - the theme is persisted
 * by EntityKidnapper directly.
 */
public class KidnapperCaptureEquipment {

    protected final EntityKidnapper kidnapper;

    public KidnapperCaptureEquipment(EntityKidnapper kidnapper) {
        this.kidnapper = kidnapper;
    }

    // ITEM GETTERS

    /**
     * Get bind item to use for capture.
     * Uses held item if valid, otherwise default ropes.
     *
     * @return The bind ItemStack to use (never null/empty)
     */
    public ItemStack getBindItem() {
        ItemStack mainHand = kidnapper.getMainHandItem();
        if (
            !mainHand.isEmpty() && mainHand.getItem() instanceof IV2BondageItem
        ) {
            return mainHand;
        }
        return DataDrivenBondageItem.createStack(ResourceLocation.fromNamespaceAndPath("tiedup", "ropes"));
    }

    /**
     * Get gag item to use for capture.
     * Uses offhand item if valid, otherwise empty (no gag).
     *
     * @return The gag ItemStack, or empty if no gag selected
     */
    public ItemStack getGagItem() {
        ItemStack offHand = kidnapper.getOffhandItem();
        if (!offHand.isEmpty() && offHand.getItem() instanceof IV2BondageItem) {
            return offHand;
        }
        return ItemStack.EMPTY;
    }

    /**
     * Get mittens item to apply after capture.
     *
     * @return The mittens ItemStack, or null if not selected
     */
    @Nullable
    public ItemStack getMittensItem() {
        var selection = kidnapper.getItemSelection();
        if (selection != null && !selection.mittens.isEmpty()) {
            return selection.mittens.copy();
        }
        return null;
    }

    /**
     * Get earplugs item to apply after capture.
     *
     * @return The earplugs ItemStack, or null if not selected
     */
    @Nullable
    public ItemStack getEarplugsItem() {
        var selection = kidnapper.getItemSelection();
        if (selection != null && !selection.earplugs.isEmpty()) {
            return selection.earplugs.copy();
        }
        return null;
    }

    /**
     * Get blindfold item to apply after capture.
     *
     * @return The blindfold ItemStack, or null if not selected
     */
    @Nullable
    public ItemStack getBlindfoldItem() {
        var selection = kidnapper.getItemSelection();
        if (selection != null && !selection.blindfold.isEmpty()) {
            return selection.blindfold.copy();
        }
        return null;
    }

    /**
     * Get collar item to apply during capture.
     * Returns a basic shock collar that kidnappers use.
     *
     * @return The collar ItemStack, or null if not available
     */
    @Nullable
    public ItemStack getCollarItem() {
        // Kidnappers always have a shock collar to mark their captives
        return com.tiedup.remake.v2.bondage.datadriven.DataDrivenBondageItem.createStack(
            net.minecraft.resources.ResourceLocation.fromNamespaceAndPath("tiedup", "shock_collar"));
    }

    // HELD ITEM MANAGEMENT

    /**
     * Equip themed bind and gag items before capture.
     * Called when starting to chase a target.
     *
     * <p>This makes the kidnapper visually hold their capture tools.
     */
    public void setUpHeldItems() {
        // Initialize theme if not done yet
        var selection = kidnapper.getItemSelection();
        if (selection == null) {
            kidnapper.ensureAppearanceInitialized();
            selection = kidnapper.getItemSelection();
        }

        // Equip bind in main hand (always present)
        kidnapper.setItemSlot(EquipmentSlot.MAINHAND, selection.bind.copy());

        // Equip gag in off hand (if selected)
        if (!selection.gag.isEmpty()) {
            kidnapper.setItemSlot(EquipmentSlot.OFFHAND, selection.gag.copy());
        }
    }

    /**
     * Clear held items (hide capture tools).
     * Called when not chasing anyone.
     *
     * <p>Note: Subclasses may override this to restore different items
     * (e.g., Archer restores bow, Merchant may have different behavior).
     */
    public void clearHeldItems() {
        kidnapper.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
        kidnapper.setItemSlot(EquipmentSlot.OFFHAND, ItemStack.EMPTY);
    }
}
