package com.tiedup.remake.entities.damsel.components;

import com.tiedup.remake.core.SettingsAccessor;
import com.tiedup.remake.dialogue.EntityDialogueManager;
import com.tiedup.remake.dialogue.EntityDialogueManager.DialogueCategory;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.world.entity.player.Player;

/**
 * Manages all dialogue-related systems for EntityDamsel:
 * - Direct dialogue (talkTo, actionTo)
 * - Radius-based dialogue (talkToPlayersInRadius, actionToPlayersInRadius)
 * - Cooldown management for dialogue categories
 *
 */
public class DamselDialogueHandler {

    private final IDialogueHost host;

    /** Track last time each dialogue category was used (for cooldowns) */
    private final Map<DialogueCategory, Long> dialogueCooldowns =
        new HashMap<>();

    public DamselDialogueHandler(IDialogueHost host) {
        this.host = host;
    }

    // DIRECT DIALOGUE

    /**
     * Send a direct message to a player.
     * @param player Target player
     * @param message Message to send
     */
    public void talkTo(Player player, String message) {
        EntityDialogueManager.talkTo(host.getEntity(), player, message);
    }

    /**
     * Send a dialogue message to a player using a category.
     * @param player Target player
     * @param category Dialogue category (selects random message)
     */
    public void talkTo(Player player, DialogueCategory category) {
        EntityDialogueManager.talkTo(host.getEntity(), player, category);
    }

    /**
     * Send an action message to a player (e.g. "*struggles*").
     * @param player Target player
     * @param action Action description
     */
    public void actionTo(Player player, String action) {
        EntityDialogueManager.actionTo(host.getEntity(), player, action);
    }

    /**
     * Send an action message to a player using a category.
     * @param player Target player
     * @param category Action category
     */
    public void actionTo(Player player, DialogueCategory category) {
        EntityDialogueManager.actionTo(host.getEntity(), player, category);
    }

    // RADIUS-BASED DIALOGUE

    /**
     * Send a message to all players within radius.
     * @param message Message to send
     * @param radius Radius in blocks
     */
    public void talkToPlayersInRadius(String message, int radius) {
        EntityDialogueManager.talkToNearby(host.getEntity(), message, radius);
    }

    /**
     * Send a dialogue message to all players within radius using a category.
     * @param category Dialogue category
     * @param radius Radius in blocks
     */
    public void talkToPlayersInRadius(DialogueCategory category, int radius) {
        EntityDialogueManager.talkToNearby(host.getEntity(), category, radius);
    }

    /**
     * Send a dialogue message to nearby players with cooldown management.
     * Only sends if cooldown has expired for this category.
     *
     * @param category Dialogue category
     * @param radius Radius in blocks
     * @return true if message was sent, false if on cooldown
     */
    public boolean talkToPlayersInRadiusWithCooldown(
        DialogueCategory category,
        int radius
    ) {
        long currentTick = host.getGameTime();
        Long lastUsed = dialogueCooldowns.get(category);

        if (
            lastUsed != null &&
            (currentTick - lastUsed) < SettingsAccessor.getDialogueCooldown()
        ) {
            return false; // Still on cooldown
        }

        dialogueCooldowns.put(category, currentTick);
        EntityDialogueManager.talkToNearby(host.getEntity(), category, radius);
        return true;
    }

    /**
     * Send an action message to all players within radius.
     * @param action Action description
     * @param radius Radius in blocks
     */
    public void actionToPlayersInRadius(String action, int radius) {
        EntityDialogueManager.actionToNearby(host.getEntity(), action, radius);
    }

    /**
     * Send an action message to all players within radius using a category.
     * @param category Action category
     * @param radius Radius in blocks
     */
    public void actionToPlayersInRadius(DialogueCategory category, int radius) {
        EntityDialogueManager.actionToNearby(
            host.getEntity(),
            category,
            radius
        );
    }
}
