package com.tiedup.remake.items.base;

import com.tiedup.remake.core.SystemMessageManager;
import com.tiedup.remake.util.EquipmentInteractionHelper;
import com.tiedup.remake.util.TiedUpSounds;
import com.tiedup.remake.v2.BodyRegionV2;
import java.util.List;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

/**
 * Base class for earplug items.
 * These items block or reduce sounds when equipped.
 *
 * Based on original ItemEarplugs from 1.12.2
 *
 * Phase future: Sound blocking effect
 */
public abstract class ItemEarplugs
    extends Item
    implements IBondageItem, ILockable
{

    public ItemEarplugs(Properties properties) {
        super(properties.stacksTo(16)); // Earplugs can stack to 16
    }

    @Override
    public BodyRegionV2 getBodyRegion() {
        return BodyRegionV2.EARS;
    }

    @Override
    public void appendHoverText(
        ItemStack stack,
        @Nullable Level level,
        List<Component> tooltip,
        TooltipFlag flag
    ) {
        super.appendHoverText(stack, level, tooltip, flag);
        appendLockTooltip(stack, tooltip);
    }

    /**
     * Called when player right-clicks another entity with earplugs.
     * Allows putting earplugs on tied-up entities (Players and NPCs).
     */
    @Override
    public InteractionResult interactLivingEntity(
        ItemStack stack,
        Player player,
        LivingEntity target,
        InteractionHand hand
    ) {
        return EquipmentInteractionHelper.equipOnTarget(
            stack,
            player,
            target,
            state -> state.hasEarplugs(),
            (state, item) -> state.equip(BodyRegionV2.EARS, item),
            (state, item) ->
                state.replaceEquipment(BodyRegionV2.EARS, item, false),
            (p, t) ->
                SystemMessageManager.sendToTarget(
                    p,
                    t,
                    SystemMessageManager.MessageCategory.EARPLUGS_ON
                ),
            "ItemEarplugs",
            null, // No pre-equip hook
            (s, p, t, state) -> TiedUpSounds.playEarplugsEquipSound(t), // Post-equip: play sound
            null // No replace check
        );
    }

    // Sound blocking implemented in:
    // - client/events/EarplugSoundHandler.java (event interception)
    // - client/MuffledSoundInstance.java (volume/pitch wrapper)
    // - Configurable via ModConfig.CLIENT.earplugVolumeMultiplier

    // ILockable implementation inherited from interface default methods
}
