package com.tiedup.remake.events.interaction;

import com.tiedup.remake.core.SystemMessageManager;
import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.entities.EntityKidnapperMerchant;
import com.tiedup.remake.network.ModNetwork;
import com.tiedup.remake.network.merchant.PacketOpenMerchantScreen;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

/**
 * Event handler for player interactions with EntityKidnapperMerchant.
 *
 * Handles:
 * - Right-click on merchant to open trading screen
 * - Validation checks (merchant mode, no captives)
 */
@Mod.EventBusSubscriber(modid = TiedUpMod.MOD_ID)
public class MerchantInteractionEventHandler {

    /**
     * Handle right-click interaction with EntityKidnapperMerchant.
     */
    @SubscribeEvent
    public static void onEntityInteract(
        PlayerInteractEvent.EntityInteract event
    ) {
        // Server-side only
        if (event.getLevel().isClientSide()) return;

        // Must be interacting with a merchant
        if (
            !(event.getTarget() instanceof EntityKidnapperMerchant merchant)
        ) return;

        // Must use main hand
        if (event.getHand() != InteractionHand.MAIN_HAND) return;

        // Skip enslaved merchants - allow normal NPC interactions (command wand, conversation)
        if (merchant.isTiedUp()) return;

        Player player = event.getEntity();

        // Validation 1: Merchant must be in MERCHANT mode (not hostile)
        if (merchant.isHostile()) {
            SystemMessageManager.sendToPlayer(
                player,
                SystemMessageManager.MessageCategory.ERROR,
                "The merchant is too angry to trade!"
            );
            event.setCanceled(true);
            return;
        }

        // Validation 2: Merchant must not have captives
        if (merchant.hasCaptives()) {
            SystemMessageManager.sendToPlayer(
                player,
                SystemMessageManager.MessageCategory.ERROR,
                "The merchant is busy with a captive!"
            );
            event.setCanceled(true);
            return;
        }

        // Open trading screen
        if (player instanceof ServerPlayer serverPlayer) {
            // Mark merchant as trading with this player
            merchant.startTrading(player.getUUID());

            ModNetwork.sendToPlayer(
                new PacketOpenMerchantScreen(
                    merchant.getUUID(),
                    merchant.getTrades()
                ),
                serverPlayer
            );

            TiedUpMod.LOGGER.debug(
                "[MerchantInteractionEventHandler] {} opened merchant screen for {}",
                player.getName().getString(),
                merchant.getNpcName()
            );
        }

        event.setCanceled(true);
    }
}
