package com.tiedup.remake.events.camp;

import com.tiedup.remake.cells.CampLifecycleManager;
import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.entities.EntityMaid;
import com.tiedup.remake.entities.EntitySlaveTrader;
import com.tiedup.remake.v2.bondage.BindModeHelper;
import java.util.UUID;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

/**
 * Protects camp NPCs (Trader and Maid) from being captured by players.
 * When a player attempts to restrain a camp NPC:
 * 1. The attempt is cancelled
 * 2. The entire camp (trader, maid, all kidnappers) is alerted to attack
 * 3. The player receives a warning message
 */
@Mod.EventBusSubscriber(modid = TiedUpMod.MOD_ID)
public class CampNpcProtectionHandler {

    @SubscribeEvent(priority = EventPriority.HIGHEST)
    public static void onPlayerInteractEntity(
        PlayerInteractEvent.EntityInteract event
    ) {
        Player player = event.getEntity();
        Entity target = event.getTarget();

        // Server-side only
        if (player.level().isClientSide) return;
        if (!(player.level() instanceof ServerLevel serverLevel)) return;

        // Check if player is holding restraint item
        ItemStack heldItem = player.getItemInHand(event.getHand());
        if (!BindModeHelper.isBindItem(heldItem)) return;

        // Check if target is trader or maid with active camp
        UUID campId = null;
        boolean isTrader = false;

        if (target instanceof EntitySlaveTrader trader) {
            if (trader.isTiedUp()) return; // Already captured
            campId = trader.getCampUUID();
            isTrader = true;
        } else if (target instanceof EntityMaid maid) {
            if (maid.isTiedUp() || maid.isFreed()) return; // Already captured or freed
            campId = maid.getCampUUID();
            isTrader = false;
        } else {
            return; // Not a protected NPC
        }

        if (campId == null) return; // No active camp

        // CANCEL THE ATTEMPT
        event.setCanceled(true);

        // ALERT THE CAMP
        CampLifecycleManager.alertCampToDefend(campId, player, serverLevel);

        // Log the attempt
        TiedUpMod.LOGGER.warn(
            "[CampNpcProtection] {} attempted to restrain {} - camp alerted!",
            player.getName().getString(),
            isTrader ? "trader" : "maid"
        );

        // Send warning message to player
        player.sendSystemMessage(
            Component.translatable(
                isTrader
                    ? "msg.tiedup.event.camp_defends_trader"
                    : "msg.tiedup.event.camp_defends_maid"
            ).withStyle(ChatFormatting.RED, ChatFormatting.BOLD)
        );
    }
}
