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

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.item.Items;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.Position;
import net.minecraft.world.entity.ai.memory.WalkTarget;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.FarmBlock;
import net.minecraft.world.level.block.CropBlock;
import net.minecraft.core.Vec3i;
import net.minecraft.world.entity.npc.VillagerProfession;
import net.minecraft.world.level.GameRules;
import net.minecraft.server.level.ServerLevel;
import com.google.common.collect.Lists;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.npc.Villager;

public class HarvestFarmland extends Behavior<Villager> {
    @Nullable
    private BlockPos aboveFarmlandPos;
    private long nextOkStartTime;
    private int timeWorkedSoFar;
    private final List<BlockPos> validFarmlandAroundVillager;
    
    public HarvestFarmland() {
        super(ImmutableMap.<MemoryModuleType<PositionTracker>, MemoryStatus>of(MemoryModuleType.LOOK_TARGET, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<PositionTracker>)MemoryModuleType.WALK_TARGET, MemoryStatus.VALUE_ABSENT, (MemoryModuleType<PositionTracker>)MemoryModuleType.SECONDARY_JOB_SITE, MemoryStatus.VALUE_PRESENT));
        this.validFarmlandAroundVillager = Lists.newArrayList();
    }
    
    @Override
    protected boolean checkExtraStartConditions(final ServerLevel zd, final Villager bdp) {
        if (!zd.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) {
            return false;
        }
        if (bdp.getVillagerData().getProfession() != VillagerProfession.FARMER) {
            return false;
        }
        final BlockPos.MutableBlockPos a4 = bdp.blockPosition().mutable();
        this.validFarmlandAroundVillager.clear();
        for (int integer5 = -1; integer5 <= 1; ++integer5) {
            for (int integer6 = -1; integer6 <= 1; ++integer6) {
                for (int integer7 = -1; integer7 <= 1; ++integer7) {
                    a4.set(bdp.getX() + integer5, bdp.getY() + integer6, bdp.getZ() + integer7);
                    if (this.validPos(a4, zd)) {
                        this.validFarmlandAroundVillager.add(new BlockPos(a4));
                    }
                }
            }
        }
        this.aboveFarmlandPos = this.getValidFarmland(zd);
        return this.aboveFarmlandPos != null;
    }
    
    @Nullable
    private BlockPos getValidFarmland(final ServerLevel zd) {
        return this.validFarmlandAroundVillager.isEmpty() ? null : this.validFarmlandAroundVillager.get(zd.getRandom().nextInt(this.validFarmlandAroundVillager.size()));
    }
    
    private boolean validPos(final BlockPos fu, final ServerLevel zd) {
        final BlockState cfj4 = zd.getBlockState(fu);
        final Block bvr5 = cfj4.getBlock();
        final Block bvr6 = zd.getBlockState(fu.below()).getBlock();
        return (bvr5 instanceof CropBlock && ((CropBlock)bvr5).isMaxAge(cfj4)) || (cfj4.isAir() && bvr6 instanceof FarmBlock);
    }
    
    @Override
    protected void start(final ServerLevel zd, final Villager bdp, final long long3) {
        if (long3 > this.nextOkStartTime && this.aboveFarmlandPos != null) {
            bdp.getBrain().<BlockPosTracker>setMemory((MemoryModuleType<BlockPosTracker>)MemoryModuleType.LOOK_TARGET, new BlockPosTracker(this.aboveFarmlandPos));
            bdp.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(new BlockPosTracker(this.aboveFarmlandPos), 0.5f, 1));
        }
    }
    
    @Override
    protected void stop(final ServerLevel zd, final Villager bdp, final long long3) {
        bdp.getBrain().<PositionTracker>eraseMemory(MemoryModuleType.LOOK_TARGET);
        bdp.getBrain().<WalkTarget>eraseMemory(MemoryModuleType.WALK_TARGET);
        this.timeWorkedSoFar = 0;
        this.nextOkStartTime = long3 + 40L;
    }
    
    @Override
    protected void stop(final ServerLevel zd, final Villager bdp, final long long3) {
        if (this.aboveFarmlandPos != null && !this.aboveFarmlandPos.closerThan(bdp.position(), 1.0)) {
            return;
        }
        if (this.aboveFarmlandPos != null && long3 > this.nextOkStartTime) {
            final BlockState cfj6 = zd.getBlockState(this.aboveFarmlandPos);
            final Block bvr7 = cfj6.getBlock();
            final Block bvr8 = zd.getBlockState(this.aboveFarmlandPos.below()).getBlock();
            if (bvr7 instanceof CropBlock && ((CropBlock)bvr7).isMaxAge(cfj6)) {
                zd.destroyBlock(this.aboveFarmlandPos, true, bdp);
            }
            if (cfj6.isAir() && bvr8 instanceof FarmBlock && bdp.hasFarmSeeds()) {
                final SimpleContainer anm9 = bdp.getInventory();
                int integer10 = 0;
                while (integer10 < anm9.getContainerSize()) {
                    final ItemStack bki11 = anm9.getItem(integer10);
                    boolean boolean12 = false;
                    if (!bki11.isEmpty()) {
                        if (bki11.getItem() == Items.WHEAT_SEEDS) {
                            zd.setBlock(this.aboveFarmlandPos, Blocks.WHEAT.defaultBlockState(), 3);
                            boolean12 = true;
                        }
                        else if (bki11.getItem() == Items.POTATO) {
                            zd.setBlock(this.aboveFarmlandPos, Blocks.POTATOES.defaultBlockState(), 3);
                            boolean12 = true;
                        }
                        else if (bki11.getItem() == Items.CARROT) {
                            zd.setBlock(this.aboveFarmlandPos, Blocks.CARROTS.defaultBlockState(), 3);
                            boolean12 = true;
                        }
                        else if (bki11.getItem() == Items.BEETROOT_SEEDS) {
                            zd.setBlock(this.aboveFarmlandPos, Blocks.BEETROOTS.defaultBlockState(), 3);
                            boolean12 = true;
                        }
                    }
                    if (boolean12) {
                        zd.playSound(null, this.aboveFarmlandPos.getX(), this.aboveFarmlandPos.getY(), this.aboveFarmlandPos.getZ(), SoundEvents.CROP_PLANTED, SoundSource.BLOCKS, 1.0f, 1.0f);
                        bki11.shrink(1);
                        if (bki11.isEmpty()) {
                            anm9.setItem(integer10, ItemStack.EMPTY);
                            break;
                        }
                        break;
                    }
                    else {
                        ++integer10;
                    }
                }
            }
            if (bvr7 instanceof CropBlock && !((CropBlock)bvr7).isMaxAge(cfj6)) {
                this.validFarmlandAroundVillager.remove(this.aboveFarmlandPos);
                this.aboveFarmlandPos = this.getValidFarmland(zd);
                if (this.aboveFarmlandPos != null) {
                    this.nextOkStartTime = long3 + 20L;
                    bdp.getBrain().<WalkTarget>setMemory(MemoryModuleType.WALK_TARGET, new WalkTarget(new BlockPosTracker(this.aboveFarmlandPos), 0.5f, 1));
                    bdp.getBrain().<BlockPosTracker>setMemory((MemoryModuleType<BlockPosTracker>)MemoryModuleType.LOOK_TARGET, new BlockPosTracker(this.aboveFarmlandPos));
                }
            }
        }
        ++this.timeWorkedSoFar;
    }
    
    @Override
    protected boolean canStillUse(final ServerLevel zd, final Villager bdp, final long long3) {
        return this.timeWorkedSoFar < 200;
    }
}
