package com.tiedup.remake.items.base;

import com.tiedup.remake.core.SystemMessageManager;
import com.tiedup.remake.util.EquipmentInteractionHelper;
import com.tiedup.remake.util.GagMaterial;
import com.tiedup.remake.v2.BodyRegionV2;
import java.util.List;
import net.minecraft.ChatFormatting;
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 gag items (ball gag, cloth gag, tape, etc.)
 * These items prevent or muffle a player's speech when equipped.
 *
 * Based on original ItemGag from 1.12.2
 *
 */
public abstract class ItemGag
    extends Item
    implements IBondageItem, IHasGaggingEffect, IAdjustable, ILockable
{

    private final GagMaterial material;

    public ItemGag(Properties properties, GagMaterial material) {
        super(properties);
        this.material = material;
    }

    public GagMaterial getGagMaterial() {
        return this.material;
    }

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

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

    /**
     * All gags can be adjusted to better fit player skins.
     * @return true - gags support position adjustment
     */
    @Override
    public boolean canBeAdjusted() {
        return true;
    }

    /**
     * Called when player right-clicks another entity with this gag.
     * Allows putting gag 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.isGagged(),
            (state, item) -> state.equip(BodyRegionV2.MOUTH, item),
            (state, item) ->
                state.replaceEquipment(BodyRegionV2.MOUTH, item, false),
            SystemMessageManager::sendGagged,
            "ItemGag"
        );
    }

    // ILockable implementation inherited from interface default methods
}
