package net.minecraft.world.entity.ai.behavior;

import net.minecraft.server.level.ServerLevel;
import java.util.Map;
import net.minecraft.util.IntRange;
import net.minecraft.world.entity.LivingEntity;

public class RunSometimes<E extends LivingEntity> extends Behavior<E> {
    private boolean resetTicks;
    private boolean wasRunning;
    private final IntRange interval;
    private final Behavior<? super E> wrappedBehavior;
    private int ticksUntilNextStart;
    
    public RunSometimes(final Behavior<? super E> aqh, final IntRange adx) {
        this(aqh, false, adx);
    }
    
    public RunSometimes(final Behavior<? super E> aqh, final boolean boolean2, final IntRange adx) {
        super(aqh.entryCondition);
        this.wrappedBehavior = aqh;
        this.resetTicks = !boolean2;
        this.interval = adx;
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final E aoy) {
        if (!this.wrappedBehavior.checkExtraStartConditions(zd, aoy)) {
            return false;
        }
        if (this.resetTicks) {
            this.resetTicksUntilNextStart(zd);
            this.resetTicks = false;
        }
        if (this.ticksUntilNextStart > 0) {
            --this.ticksUntilNextStart;
        }
        return !this.wasRunning && this.ticksUntilNextStart == 0;
    }
    
    @Override
    protected void start(final ServerLevel zd, final E aoy, final long long3) {
        this.wrappedBehavior.start(zd, aoy, long3);
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel zd, final E aoy, final long long3) {
        return this.wrappedBehavior.canStillUse(zd, aoy, long3);
    }
    
    @Override
    protected void tick(final ServerLevel zd, final E aoy, final long long3) {
        this.wrappedBehavior.tick(zd, aoy, long3);
        this.wasRunning = (this.wrappedBehavior.getStatus() == Status.RUNNING);
    }
    
    @Override
    protected void stop(final ServerLevel zd, final E aoy, final long long3) {
        this.resetTicksUntilNextStart(zd);
        this.wrappedBehavior.stop(zd, aoy, long3);
    }
    
    private void resetTicksUntilNextStart(final ServerLevel zd) {
        this.ticksUntilNextStart = this.interval.randomValue(zd.random);
    }
    
    @Override
    protected boolean timedOut(final long long1) {
        return false;
    }
    
    @Override
    public String toString() {
        return "RunSometimes: " + this.wrappedBehavior;
    }
}
