package com.tiedup.remake.compat.mca.ai.goals;

import com.tiedup.remake.compat.mca.MCABondageManager;
import com.tiedup.remake.state.IBondageState;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.PanicGoal;

/**
 * AI Goal: MCA villager panics when hurt or restrained.
 *
 * Extends vanilla PanicGoal with bondage-aware conditions.
 * Active when villager is being tied (but can still move).
 */
public class MCAPanicGoal extends PanicGoal {

    private final PathfinderMob mob;

    /** Ticks since last panic trigger (for brief panic after state changes) */
    private int panicCooldown = 0;

    /** How long panic lasts after trigger (in ticks) */
    private static final int PANIC_DURATION = 60; // 3 seconds

    /**
     * Create panic goal.
     *
     * @param mob The MCA villager mob (must be PathfinderMob)
     * @param speedModifier Speed multiplier when panicking
     */
    public MCAPanicGoal(PathfinderMob mob, double speedModifier) {
        super(mob, speedModifier);
        this.mob = mob;
    }

    /**
     * Trigger panic for a short duration.
     * Call this when something scary happens (being tied, etc.)
     */
    public void triggerPanic() {
        this.panicCooldown = PANIC_DURATION;
    }

    @Override
    public boolean canUse() {
        // Check bondage state
        IBondageState state = MCABondageManager.getInstance().getKidnappedState(
            mob
        );
        if (state == null) return false;

        // Can't panic if fully tied (can't move)
        if (state.hasArmsBound() && state.hasLegsBound()) {
            return false;
        }

        // Can't panic if collared (obedient)
        if (state.hasCollar()) {
            return false;
        }

        // Check for recent damage OR active panic cooldown
        if (panicCooldown > 0) {
            panicCooldown--;
            return true;
        }

        return super.canUse();
    }

    @Override
    public boolean canContinueToUse() {
        // Check bondage state
        IBondageState state = MCABondageManager.getInstance().getKidnappedState(
            mob
        );
        if (state == null) return false;

        // Stop if fully tied or collared
        if (state.hasArmsBound() && state.hasLegsBound()) {
            return false;
        }
        if (state.hasCollar()) {
            return false;
        }

        // Continue if cooldown active OR parent says so
        if (panicCooldown > 0) {
            panicCooldown--;
            return true;
        }

        return super.canContinueToUse();
    }

    @Override
    public void start() {
        super.start();
        // Could add sound/particle effects here
    }

    @Override
    public void stop() {
        super.stop();
        panicCooldown = 0;
    }
}
