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

import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.pathfinder.Node;
import net.minecraft.world.level.pathfinder.Path;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.DoorBlock;
import net.minecraft.world.entity.ai.navigation.GroundPathNavigation;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Mob;

public abstract class DoorInteractGoal extends Goal {
    protected Mob mob;
    protected BlockPos doorPos;
    protected boolean hasDoor;
    private boolean passed;
    private float doorOpenDirX;
    private float doorOpenDirZ;
    
    public DoorInteractGoal(final Mob ala) {
        this.doorPos = BlockPos.ZERO;
        this.mob = ala;
        if (!(ala.getNavigation() instanceof GroundPathNavigation)) {
            throw new IllegalArgumentException("Unsupported mob type for DoorInteractGoal");
        }
    }
    
    protected boolean isOpen() {
        if (!this.hasDoor) {
            return false;
        }
        final BlockState byj2 = this.mob.level.getBlockState(this.doorPos);
        if (!(byj2.getBlock() instanceof DoorBlock)) {
            return this.hasDoor = false;
        }
        return byj2.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN);
    }
    
    protected void setOpen(final boolean boolean1) {
        if (this.hasDoor) {
            final BlockState byj3 = this.mob.level.getBlockState(this.doorPos);
            if (byj3.getBlock() instanceof DoorBlock) {
                ((DoorBlock)byj3.getBlock()).setOpen(this.mob.level, this.doorPos, boolean1);
            }
        }
    }
    
    @Override
    public boolean canUse() {
        if (!this.mob.horizontalCollision) {
            return false;
        }
        final GroundPathNavigation arq2 = (GroundPathNavigation)this.mob.getNavigation();
        final Path cqq3 = arq2.getPath();
        if (cqq3 == null || cqq3.isDone() || !arq2.canOpenDoors()) {
            return false;
        }
        for (int integer4 = 0; integer4 < Math.min(cqq3.getIndex() + 2, cqq3.getSize()); ++integer4) {
            final Node cqo5 = cqq3.get(integer4);
            this.doorPos = new BlockPos(cqo5.x, cqo5.y + 1, cqo5.z);
            if (this.mob.distanceToSqr(this.doorPos.getX(), this.mob.getY(), this.doorPos.getZ()) <= 2.25) {
                this.hasDoor = isDoor(this.mob.level, this.doorPos);
                if (this.hasDoor) {
                    return true;
                }
            }
        }
        this.doorPos = new BlockPos(this.mob).above();
        return this.hasDoor = isDoor(this.mob.level, this.doorPos);
    }
    
    @Override
    public boolean canContinueToUse() {
        return !this.passed;
    }
    
    @Override
    public void start() {
        this.passed = false;
        this.doorOpenDirX = (float)(this.doorPos.getX() + 0.5f - this.mob.getX());
        this.doorOpenDirZ = (float)(this.doorPos.getZ() + 0.5f - this.mob.getZ());
    }
    
    @Override
    public void tick() {
        final float float2 = (float)(this.doorPos.getX() + 0.5f - this.mob.getX());
        final float float3 = (float)(this.doorPos.getZ() + 0.5f - this.mob.getZ());
        final float float4 = this.doorOpenDirX * float2 + this.doorOpenDirZ * float3;
        if (float4 < 0.0f) {
            this.passed = true;
        }
    }
    
    public static boolean isDoor(final Level bjw, final BlockPos fk) {
        final BlockState byj3 = bjw.getBlockState(fk);
        return byj3.getBlock() instanceof DoorBlock && byj3.getMaterial() == Material.WOOD;
    }
}
