package com.tiedup.remake.v2.bondage.items;

import com.tiedup.remake.items.base.IHasResistance;
import com.tiedup.remake.items.base.ILockable;
import com.tiedup.remake.v2.BodyRegionV2;
import com.tiedup.remake.v2.bondage.IV2BondageItem;
import com.tiedup.remake.v2.bondage.V2EquipResult;
import com.tiedup.remake.v2.bondage.capability.V2EquipmentHelper;
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.InteractionResultHolder;
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 V2 bondage items.
 *
 * Provides:
 * - Self-equip via right-click in air (use())
 * - Equip on target via right-click on entity (interactLivingEntity())
 * - Lock-aware canUnequip() bridging IV2BondageItem and ILockable
 * - Lock/resistance tooltips
 *
 * Subclasses implement: getOccupiedRegions(), getModelLocation(), getPosePriority(),
 * getResistanceId(), notifyStruggle().
 */
public abstract class AbstractV2BondageItem
    extends Item
    implements IV2BondageItem, ILockable, IHasResistance
{

    protected AbstractV2BondageItem(Properties properties) {
        super(properties);
    }

    // ===== EQUIP: SELF (left-click hold with tying duration) =====
    // Self-equip is handled by SelfBondageInputHandler (left-click hold) which sends
    // PacketSelfBondage, routed to handleV2SelfBondage() with tying progress bar.
    // Right-click in air does nothing for self-equip — consistent with V1 behavior.

    @Override
    public InteractionResultHolder<ItemStack> use(
        Level level,
        Player player,
        InteractionHand hand
    ) {
        return InteractionResultHolder.pass(player.getItemInHand(hand));
    }

    // ===== EQUIP: ON TARGET (right-click on entity) =====

    @Override
    public InteractionResult interactLivingEntity(
        ItemStack stack,
        Player player,
        LivingEntity target,
        InteractionHand hand
    ) {
        // Client returns SUCCESS for arm swing animation. Server may reject —
        // minor visual desync is accepted Forge pattern (same as vanilla food/bow).
        if (player.level().isClientSide) {
            return InteractionResult.SUCCESS;
        }

        // Cannot equip if player's arms are restrained
        if (V2EquipmentHelper.isRegionOccupied(player, BodyRegionV2.ARMS)) {
            return InteractionResult.PASS;
        }

        // Distance + line-of-sight validation
        if (player.distanceTo(target) > 4.0 || !player.hasLineOfSight(target)) {
            return InteractionResult.PASS;
        }

        V2EquipResult result = V2EquipmentHelper.equipItem(target, stack);
        if (result.isSuccess()) {
            // Drop displaced items at target's feet
            for (ItemStack displaced : result.displaced()) {
                target.spawnAtLocation(displaced);
            }
            stack.shrink(1);
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.PASS;
    }

    // ===== LOCK-AWARE CANUNEQUIP =====

    @Override
    public boolean canUnequip(ItemStack stack, LivingEntity entity) {
        return !isLocked(stack);
    }

    // ===== TOOLTIPS =====

    @Override
    public void appendHoverText(
        ItemStack stack,
        @Nullable Level level,
        List<Component> tooltip,
        TooltipFlag flag
    ) {
        super.appendHoverText(stack, level, tooltip, flag);
        // Lock status from ILockable
        appendLockTooltip(stack, tooltip);
        // Escape difficulty
        int difficulty = getEscapeDifficulty(stack);
        if (difficulty > 0) {
            tooltip.add(
                Component.translatable(
                    "item.tiedup.tooltip.escape_difficulty",
                    difficulty
                ).withStyle(ChatFormatting.GRAY)
            );
        }
    }

    // ===== IV2BondageItem DEFAULTS =====

    @Override
    public int getEscapeDifficulty() {
        return 0;
    }

    @Override
    public boolean supportsColor() {
        return false;
    }

    @Override
    public boolean supportsSlimModel() {
        return false;
    }
}
