package com.tiedup.remake.entities.ai;

import java.util.EnumSet;
import java.util.function.BooleanSupplier;
import net.minecraft.world.entity.ai.goal.Goal;

/**
 * Wrapper goal that only executes when a condition is met.
 * Used to conditionally enable/disable AI goals at runtime.
 *
 * Example: ConditionalGoal(new KidnapperFindTargetGoal(), () -> merchant.isHostile())
 */
public class ConditionalGoal extends Goal {

    private final Goal wrappedGoal;
    private final BooleanSupplier condition;

    public ConditionalGoal(Goal goal, BooleanSupplier condition) {
        this.wrappedGoal = goal;
        this.condition = condition;
        this.setFlags(goal.getFlags());
    }

    @Override
    public boolean canUse() {
        return condition.getAsBoolean() && wrappedGoal.canUse();
    }

    @Override
    public boolean canContinueToUse() {
        return condition.getAsBoolean() && wrappedGoal.canContinueToUse();
    }

    @Override
    public boolean isInterruptable() {
        return wrappedGoal.isInterruptable();
    }

    @Override
    public void start() {
        wrappedGoal.start();
    }

    @Override
    public void stop() {
        wrappedGoal.stop();
    }

    @Override
    public boolean requiresUpdateEveryTick() {
        return wrappedGoal.requiresUpdateEveryTick();
    }

    @Override
    public void tick() {
        wrappedGoal.tick();
    }

    @Override
    public void setFlags(EnumSet<Flag> flags) {
        super.setFlags(flags);
        wrappedGoal.setFlags(flags);
    }

    @Override
    public EnumSet<Flag> getFlags() {
        return wrappedGoal.getFlags();
    }
}
