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

import com.tiedup.remake.dialogue.EntityDialogueManager.DialogueCategory;
import com.tiedup.remake.entities.EntityDamsel;
import net.minecraft.world.entity.ai.goal.PanicGoal;

/**
 * AI Goal: Damsel panics and flees when hurt.
 *
 *
 * <p><b>Behavior:</b></p>
 * <ul>
 *   <li>Only active if NOT tied up (can't panic if bound)</li>
 *   <li>Only active if NOT collared (enslaved shouldn't panic)</li>
 *   <li>Triggers on taking damage</li>
 *   <li>Runs around frantically at high speed</li>
 * </ul>
 */
public class DamselPanicGoal extends PanicGoal {

    private final EntityDamsel damsel;

    /** Radius for dialogue broadcast */
    private static final int DIALOGUE_RADIUS = 20;

    /**
     * Create panic goal.
     *
     * @param damsel Damsel entity
     * @param speedModifier Speed multiplier when panicking
     */
    public DamselPanicGoal(EntityDamsel damsel, double speedModifier) {
        super(damsel, speedModifier);
        this.damsel = damsel;
    }

    @Override
    public boolean canUse() {
        // Can't panic if tied up or collared
        if (damsel.isTiedUp() || damsel.hasCollar()) {
            return false;
        }

        return super.canUse();
    }

    @Override
    public boolean canContinueToUse() {
        // Stop panicking if got tied or collared
        if (damsel.isTiedUp() || damsel.hasCollar()) {
            return false;
        }

        return super.canContinueToUse();
    }

    @Override
    public void start() {
        super.start();

        // Broadcast panic dialogue to nearby players (with cooldown)
        if (!damsel.level().isClientSide() && !damsel.isGagged()) {
            damsel.talkToPlayersInRadiusWithCooldown(
                DialogueCategory.DAMSEL_PANIC,
                DIALOGUE_RADIUS
            );
        }
    }
}
