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

import com.google.common.collect.ImmutableList;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.ComposterBlock;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Optional;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.core.GlobalPos;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.item.Item;
import java.util.List;

public class WorkAtComposter extends WorkAtPoi {
    private static final List<Item> COMPOSTABLE_ITEMS;
    
    @Override
    protected void useWorkstation(final ServerLevel zd, final Villager bdp) {
        final Optional<GlobalPos> optional4 = bdp.getBrain().<GlobalPos>getMemory(MemoryModuleType.JOB_SITE);
        if (!optional4.isPresent()) {
            return;
        }
        final GlobalPos gc5 = optional4.get();
        final BlockState cfj6 = zd.getBlockState(gc5.pos());
        if (cfj6.is(Blocks.COMPOSTER)) {
            this.makeBread(bdp);
            this.compostItems(zd, bdp, gc5, cfj6);
        }
    }
    
    private void compostItems(final ServerLevel zd, final Villager bdp, final GlobalPos gc, BlockState cfj) {
        if (cfj.<Integer>getValue((Property<Integer>)ComposterBlock.LEVEL) == 8) {
            cfj = ComposterBlock.extractProduce(cfj, zd, gc.pos());
        }
        int integer6 = 20;
        final int integer7 = 10;
        final int[] arr8 = new int[WorkAtComposter.COMPOSTABLE_ITEMS.size()];
        final SimpleContainer anm9 = bdp.getInventory();
        final int integer8 = anm9.getContainerSize();
        for (int integer9 = integer8 - 1; integer9 >= 0 && integer6 > 0; --integer9) {
            final ItemStack bki12 = anm9.getItem(integer9);
            final int integer10 = WorkAtComposter.COMPOSTABLE_ITEMS.indexOf(bki12.getItem());
            if (integer10 != -1) {
                final int integer11 = bki12.getCount();
                final int integer12 = arr8[integer10] + integer11;
                arr8[integer10] = integer12;
                final int integer13 = Math.min(Math.min(integer12 - 10, integer6), integer11);
                if (integer13 > 0) {
                    integer6 -= integer13;
                    for (int integer14 = 0; integer14 < integer13; ++integer14) {
                        cfj = ComposterBlock.insertItem(cfj, zd, bki12, gc.pos());
                        if (cfj.<Integer>getValue((Property<Integer>)ComposterBlock.LEVEL) == 7) {
                            return;
                        }
                    }
                }
            }
        }
    }
    
    private void makeBread(final Villager bdp) {
        final SimpleContainer anm3 = bdp.getInventory();
        if (anm3.countItem(Items.BREAD) > 36) {
            return;
        }
        final int integer4 = anm3.countItem(Items.WHEAT);
        final int integer5 = 3;
        final int integer6 = 3;
        final int integer7 = Math.min(3, integer4 / 3);
        if (integer7 == 0) {
            return;
        }
        final int integer8 = integer7 * 3;
        anm3.removeItemType(Items.WHEAT, integer8);
        final ItemStack bki9 = anm3.addItem(new ItemStack(Items.BREAD, integer7));
        if (!bki9.isEmpty()) {
            bdp.spawnAtLocation(bki9, 0.5f);
        }
    }
    
    static {
        COMPOSTABLE_ITEMS = ImmutableList.<Item>of(Items.WHEAT_SEEDS, Items.BEETROOT_SEEDS);
    }
}
