package com.tiedup.remake.items;

import com.tiedup.remake.core.SystemMessageManager;
import com.tiedup.remake.items.base.ItemCollar;
import com.tiedup.remake.state.IBondageState;
import com.tiedup.remake.util.KidnappedHelper;
import java.util.List;
import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
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;

/**
 * Shock Collar - Advanced collar that can be remotely triggered.
 *
 * <p>Mechanics:</p>
 * <ul>
 *   <li><b>Remote Shocking:</b> Can be triggered by anyone holding a linked Shocker Controller.</li>
 *   <li><b>Struggle Penalty:</b> If locked, has a chance to shock the wearer during struggle attempts, interrupting them.</li>
 *   <li><b>Public Mode:</b> Can be set to public mode, allowing anyone to shock the wearer even if they aren't the owner.</li>
 * </ul>
 */
public class ItemShockCollar extends ItemCollar {

    private static final String NBT_PUBLIC_MODE = "public_mode";

    public ItemShockCollar() {
        super(new Item.Properties());
    }

    @Override
    public boolean canShock() {
        return true;
    }

    /**
     * Shows current mode (PUBLIC/PRIVATE) and usage instructions in tooltip.
     */
    @Override
    public void appendHoverText(
        ItemStack stack,
        @Nullable Level level,
        List<Component> tooltip,
        TooltipFlag flag
    ) {
        super.appendHoverText(stack, level, tooltip, flag);

        tooltip.add(
            Component.literal("Shock Feature: ")
                .withStyle(ChatFormatting.YELLOW)
                .append(
                    Component.literal(
                        isPublic(stack) ? "PUBLIC" : "PRIVATE"
                    ).withStyle(
                        isPublic(stack)
                            ? ChatFormatting.GREEN
                            : ChatFormatting.RED
                    )
                )
        );

        tooltip.add(
            Component.literal("Shift + Right-click to toggle public mode")
                .withStyle(ChatFormatting.DARK_GRAY)
                .withStyle(ChatFormatting.ITALIC)
        );
    }

    /**
     * Toggles Public mode when shift-right-clicking in air.
     */
    @Override
    public InteractionResultHolder<ItemStack> use(
        Level level,
        Player player,
        InteractionHand hand
    ) {
        ItemStack stack = player.getItemInHand(hand);

        if (player.isShiftKeyDown()) {
            if (!level.isClientSide) {
                boolean newState = !isPublic(stack);
                setPublic(stack, newState);
                SystemMessageManager.sendToPlayer(
                    player,
                    SystemMessageManager.MessageCategory.SHOCKER_MODE_SET,
                    (newState ? "PUBLIC" : "PRIVATE")
                );
            }
            return InteractionResultHolder.sidedSuccess(
                stack,
                level.isClientSide()
            );
        }

        return super.use(level, player, hand);
    }

    /**
     * Handles the risk of shocking the wearer during a struggle attempt.
     *
     * NOTE: For the new continuous struggle mini-game, shock logic is handled
     * directly in MiniGameSessionManager.tickContinuousSessions(). This method
     * is now a no-op that always returns true, kept for API compatibility.
     *
     * @param entity The wearer of the collar
     * @param stack The collar instance
     * @return Always true (shock logic moved to MiniGameSessionManager)
     */
    public boolean notifyStruggle(LivingEntity entity, ItemStack stack) {
        // Shock collar checks during continuous struggle are now handled by
        // MiniGameSessionManager.shouldTriggerShock() with 10% chance every 5 seconds.
        // This method is kept for backwards compatibility but no longer performs the check.
        return true;
    }

    public boolean isPublic(ItemStack stack) {
        CompoundTag tag = stack.getTag();
        return tag != null && tag.getBoolean(NBT_PUBLIC_MODE);
    }

    public void setPublic(ItemStack stack, boolean publicMode) {
        stack.getOrCreateTag().putBoolean(NBT_PUBLIC_MODE, publicMode);
    }
}
