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

import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.entities.EntityKidnapper;

/**
 * AI Goal for EntityKidnapper to flee to safety without a slave.
 *
 *
 * This goal activates when:
 * - Kidnapper does NOT have a slave
 * - getOutState is true (triggered after releasing slave or job/sale completion)
 *
 * Behavior:
 * 1. Walk fast in random direction
 * 2. After reaching safe distance or timeout, clear getOutState
 * 3. Resume normal behavior (patrol, hunt, etc.)
 *
 * Based on original EntityAIGettingAwaySafeState from 1.12.2
 */
public class KidnapperFleeSafeGoal extends AbstractKidnapperFleeGoal {

    /** Speed when fleeing (faster than normal) */
    private static final double FLEE_SPEED = 1.2D;

    /** How long to flee before considering safe (ticks) */
    private static final int FLEE_DURATION = 300; // 15 seconds

    /** How far to flee before considering safe (blocks) */
    private static final double SAFE_DISTANCE = 50.0D;

    /** Search range for flee targets */
    private static final int SEARCH_RANGE = 15;

    /**
     * Create a new flee safe goal.
     *
     * @param kidnapper The kidnapper entity
     */
    public KidnapperFleeSafeGoal(EntityKidnapper kidnapper) {
        super(kidnapper);
    }

    // ==================== CONFIGURATION ====================

    @Override
    protected double getFleeSpeed() {
        return FLEE_SPEED;
    }

    @Override
    protected int getMaxFleeDuration() {
        return FLEE_DURATION;
    }

    @Override
    protected double getMaxFleeDistance() {
        return SAFE_DISTANCE;
    }

    @Override
    protected int getSearchRangeRadius() {
        return SEARCH_RANGE;
    }

    // ==================== LOGIC HOOKS ====================

    @Override
    protected boolean checkCaptiveCondition() {
        // Must NOT have a captive (that's KidnapperFleeWithCaptiveGoal)
        return !this.kidnapper.hasCaptives();
    }

    @Override
    protected void onStart() {
        TiedUpMod.LOGGER.debug(
            "[KidnapperFleeSafeGoal] {} fleeing to safety",
            this.kidnapper.getNpcName()
        );
    }

    @Override
    protected void onFleeComplete() {
        TiedUpMod.LOGGER.info(
            "[KidnapperFleeSafeGoal] {} reached safety, resuming normal behavior",
            this.kidnapper.getNpcName()
        );

        // Clear the get out state
        this.kidnapper.setGetOutState(false);

        // Goal will stop on next canContinueToUse check
    }
}
