package com.tiedup.remake.items;

import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.entities.EntityDamsel;
import com.tiedup.remake.entities.EntityKidnapper;
import com.tiedup.remake.util.TiedUpSounds;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
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;

/**
 * Paddle - Tool for disciplining NPCs (gentle discipline).
 *
 * Refactored: Tighten moved to keybind, paddle now only does discipline on NPCs
 */
public class ItemPaddle extends Item {

    public ItemPaddle() {
        super(
            new Item.Properties()
                .stacksTo(1) // Paddles don't stack (tool)
                .durability(64) // 64 uses
        );
    }

    /**
     * Called when player right-clicks another entity with the paddle.
     * Applies gentle discipline to NPCs.
     *
     * Note: Tighten functionality moved to keybind (key.tiedup.tighten)
     *
     * @param stack The item stack
     * @param player The player using the paddle
     * @param target The entity being interacted with
     * @param hand The hand holding the paddle
     * @return SUCCESS if discipline applied, PASS otherwise
     */
    @Override
    public InteractionResult interactLivingEntity(
        ItemStack stack,
        Player player,
        LivingEntity target,
        InteractionHand hand
    ) {
        // Only run on server side
        if (player.level().isClientSide) {
            return InteractionResult.SUCCESS;
        }

        // NPC discipline - visual/sound feedback only (no personality effect)
        if (
            target instanceof com.tiedup.remake.entities.AbstractTiedUpNpc npc
        ) {
            // Visual feedback - gentler than whip
            TiedUpSounds.playSlapSound(target);
            if (player.level() instanceof ServerLevel serverLevel) {
                serverLevel.sendParticles(
                    ParticleTypes.SMOKE,
                    target.getX(),
                    target.getY() + target.getBbHeight() / 2.0,
                    target.getZ(),
                    5,
                    0.3,
                    0.3,
                    0.3,
                    0.05
                );
            }

            // Consume durability
            stack.hurtAndBreak(1, player, p -> p.broadcastBreakEvent(hand));

            TiedUpMod.LOGGER.debug(
                "[ItemPaddle] {} disciplined {} with paddle",
                player.getName().getString(),
                target.getName().getString()
            );

            return InteractionResult.SUCCESS;
        }

        // Paddle only works on NPCs now
        // Use keybind (T) to tighten binds on any target
        return InteractionResult.PASS;
    }
}
