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

import com.tiedup.remake.compat.mca.MCABondageManager;
import com.tiedup.remake.state.IBondageState;
import java.util.EnumSet;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.goal.Goal;

/**
 * AI Goal: MCA villager stays in place when fully restrained.
 *
 * Prevents all movement, only allows looking around.
 * Used in OVERRIDE AI level when villager is fully tied.
 */
public class MCAStayGoal extends Goal {

    private final Mob mob;

    /**
     * Create stay goal.
     *
     * @param mob The MCA villager mob
     */
    public MCAStayGoal(Mob mob) {
        this.mob = mob;
        // Lock movement and jumping
        this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.JUMP));
    }

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

        // Active when tied up (arms and/or legs)
        return state.isTiedUp();
    }

    @Override
    public boolean canContinueToUse() {
        return canUse();
    }

    @Override
    public void start() {
        // Stop all navigation immediately
        mob.getNavigation().stop();
    }

    @Override
    public void tick() {
        // Keep stopping navigation in case something tries to move
        if (!mob.getNavigation().isDone()) {
            mob.getNavigation().stop();
        }

        // Zero out any velocity
        mob.setDeltaMovement(mob.getDeltaMovement().multiply(0, 1, 0));
    }

    @Override
    public void stop() {
        // Nothing special on stop
    }
}
