package com.tiedup.remake.items;

import com.tiedup.remake.core.SystemMessageManager;
import com.tiedup.remake.core.TiedUpMod;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

/**
 * Chloroform Bottle - Used to soak rags for knocking out targets
 * Has limited durability.
 *
 *
 * Usage:
 * - Hold chloroform bottle in main hand, rag in offhand
 * - Right-click to soak the rag with chloroform
 * - Consumes 1 durability from the bottle
 */
public class ItemChloroformBottle extends Item {

    public ItemChloroformBottle() {
        super(
            new Item.Properties().stacksTo(1).durability(9) // Default durability (ModConfig not loaded yet during item registration)
        );
    }

    /**
     * Called when player right-clicks with the chloroform bottle.
     * If holding a rag in offhand, soaks it with chloroform.
     */
    @Override
    public InteractionResultHolder<ItemStack> use(
        Level level,
        Player player,
        InteractionHand hand
    ) {
        ItemStack bottle = player.getItemInHand(hand);

        // Only works from main hand
        if (hand != InteractionHand.MAIN_HAND) {
            return InteractionResultHolder.pass(bottle);
        }

        // Check for rag in offhand
        ItemStack offhandItem = player.getItemInHand(InteractionHand.OFF_HAND);
        if (
            offhandItem.isEmpty() || !(offhandItem.getItem() instanceof ItemRag)
        ) {
            if (!level.isClientSide) {
                SystemMessageManager.sendToPlayer(
                    player,
                    SystemMessageManager.MessageCategory.INFO,
                    "Hold a rag in your offhand to soak it"
                );
            }
            return InteractionResultHolder.pass(bottle);
        }

        // Server side only
        if (!level.isClientSide) {
            // Check if rag is already wet
            if (ItemRag.isWet(offhandItem)) {
                SystemMessageManager.sendToPlayer(
                    player,
                    SystemMessageManager.MessageCategory.INFO,
                    "The rag is already soaked with chloroform"
                );
                return InteractionResultHolder.pass(bottle);
            }

            // Soak the rag
            ItemRag.soak(offhandItem, ItemRag.getDefaultWetTime());

            // Damage the bottle (consume 1 use)
            bottle.hurtAndBreak(1, player, p -> {
                p.broadcastBreakEvent(InteractionHand.MAIN_HAND);
            });

            // Play bottle pour sound
            level.playSound(
                null,
                player.blockPosition(),
                SoundEvents.BOTTLE_EMPTY,
                SoundSource.PLAYERS,
                1.0f,
                1.0f
            );

            SystemMessageManager.sendToPlayer(
                player,
                SystemMessageManager.MessageCategory.RAG_SOAKED
            );

            TiedUpMod.LOGGER.info(
                "[ItemChloroformBottle] {} soaked rag with chloroform",
                player.getName().getString()
            );
        }

        return InteractionResultHolder.success(bottle);
    }
}
