package net.minecraft.world.entity.boss.enderdragon.phases;

import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.pathfinder.Node;
import net.minecraft.world.level.levelgen.feature.EndPodiumFeature;
import net.minecraft.world.level.levelgen.Heightmap;
import javax.annotation.Nullable;
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.entity.ai.targeting.TargetingConditions;

public class DragonLandingApproachPhase extends AbstractDragonPhaseInstance {
    private static final TargetingConditions NEAR_EGG_TARGETING;
    private Path currentPath;
    private Vec3 targetLocation;
    
    public DragonLandingApproachPhase(final EnderDragon aus) {
        super(aus);
    }
    
    @Override
    public EnderDragonPhase<DragonLandingApproachPhase> getPhase() {
        return EnderDragonPhase.LANDING_APPROACH;
    }
    
    @Override
    public void begin() {
        this.currentPath = null;
        this.targetLocation = null;
    }
    
    @Override
    public void doServerTick() {
        final double double2 = (this.targetLocation == null) ? 0.0 : this.targetLocation.distanceToSqr(this.dragon.getX(), this.dragon.getY(), this.dragon.getZ());
        if (double2 < 100.0 || double2 > 22500.0 || this.dragon.horizontalCollision || this.dragon.verticalCollision) {
            this.findNewTarget();
        }
    }
    
    @Nullable
    @Override
    public Vec3 getFlyTargetLocation() {
        return this.targetLocation;
    }
    
    private void findNewTarget() {
        if (this.currentPath == null || this.currentPath.isDone()) {
            final int integer2 = this.dragon.findClosestNode();
            final BlockPos fk3 = this.dragon.level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, EndPodiumFeature.END_PODIUM_LOCATION);
            final Player ayj4 = this.dragon.level.getNearestPlayer(DragonLandingApproachPhase.NEAR_EGG_TARGETING, fk3.getX(), fk3.getY(), fk3.getZ());
            int integer3;
            if (ayj4 != null) {
                final Vec3 cvl6 = new Vec3(ayj4.getX(), 0.0, ayj4.getZ()).normalize();
                integer3 = this.dragon.findClosestNode(-cvl6.x * 40.0, 105.0, -cvl6.z * 40.0);
            }
            else {
                integer3 = this.dragon.findClosestNode(40.0, fk3.getY(), 0.0);
            }
            final Node cqo6 = new Node(fk3.getX(), fk3.getY(), fk3.getZ());
            this.currentPath = this.dragon.findPath(integer2, integer3, cqo6);
            if (this.currentPath != null) {
                this.currentPath.next();
            }
        }
        this.navigateToNextPathNode();
        if (this.currentPath != null && this.currentPath.isDone()) {
            this.dragon.getPhaseManager().setPhase(EnderDragonPhase.LANDING);
        }
    }
    
    private void navigateToNextPathNode() {
        if (this.currentPath != null && !this.currentPath.isDone()) {
            final Vec3 cvl2 = this.currentPath.currentPos();
            this.currentPath.next();
            final double double3 = cvl2.x;
            final double double4 = cvl2.z;
            double double5;
            do {
                double5 = cvl2.y + this.dragon.getRandom().nextFloat() * 20.0f;
            } while (double5 < cvl2.y);
            this.targetLocation = new Vec3(double3, double5, double4);
        }
    }
    
    static {
        NEAR_EGG_TARGETING = new TargetingConditions().range(128.0);
    }
}
