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

import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import java.util.Random;
import net.minecraft.core.Vec3i;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.EquipmentSlot;
import java.util.EnumSet;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.PathfinderMob;

public class FleeSunGoal extends Goal {
    protected final PathfinderMob mob;
    private double wantedX;
    private double wantedY;
    private double wantedZ;
    private final double speedModifier;
    private final Level level;
    
    public FleeSunGoal(final PathfinderMob apg, final double double2) {
        this.mob = apg;
        this.speedModifier = double2;
        this.level = apg.level;
        this.setFlags(EnumSet.<Flag>of(Flag.MOVE));
    }
    
    @Override
    public boolean canUse() {
        return this.mob.getTarget() == null && this.level.isDay() && this.mob.isOnFire() && this.level.canSeeSky(this.mob.blockPosition()) && this.mob.getItemBySlot(EquipmentSlot.HEAD).isEmpty() && this.setWantedPos();
    }
    
    protected boolean setWantedPos() {
        final Vec3 dem2 = this.getHidePos();
        if (dem2 == null) {
            return false;
        }
        this.wantedX = dem2.x;
        this.wantedY = dem2.y;
        this.wantedZ = dem2.z;
        return true;
    }
    
    @Override
    public boolean canContinueToUse() {
        return !this.mob.getNavigation().isDone();
    }
    
    @Override
    public void start() {
        this.mob.getNavigation().moveTo(this.wantedX, this.wantedY, this.wantedZ, this.speedModifier);
    }
    
    @Nullable
    protected Vec3 getHidePos() {
        final Random random2 = this.mob.getRandom();
        final BlockPos fu3 = this.mob.blockPosition();
        for (int integer4 = 0; integer4 < 10; ++integer4) {
            final BlockPos fu4 = fu3.offset(random2.nextInt(20) - 10, random2.nextInt(6) - 3, random2.nextInt(20) - 10);
            if (!this.level.canSeeSky(fu4) && this.mob.getWalkTargetValue(fu4) < 0.0f) {
                return Vec3.atBottomCenterOf(fu4);
            }
        }
        return null;
    }
}
