package net.minecraft.world.inventory;

import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.entity.player.StackedContents;
import com.mojang.datafixers.util.Pair;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.resources.ResourceLocation;

public class InventoryMenu extends RecipeBookMenu<CraftingContainer> {
    public static final ResourceLocation BLOCK_ATLAS;
    public static final ResourceLocation EMPTY_ARMOR_SLOT_HELMET;
    public static final ResourceLocation EMPTY_ARMOR_SLOT_CHESTPLATE;
    public static final ResourceLocation EMPTY_ARMOR_SLOT_LEGGINGS;
    public static final ResourceLocation EMPTY_ARMOR_SLOT_BOOTS;
    public static final ResourceLocation EMPTY_ARMOR_SLOT_SHIELD;
    private static final ResourceLocation[] TEXTURE_EMPTY_SLOTS;
    private static final EquipmentSlot[] SLOT_IDS;
    private final CraftingContainer craftSlots;
    private final ResultContainer resultSlots;
    public final boolean active;
    private final Player owner;
    
    public InventoryMenu(final Inventory beb, final boolean boolean2, final Player bec) {
        super(null, 0);
        this.craftSlots = new CraftingContainer(this, 2, 2);
        this.resultSlots = new ResultContainer();
        this.active = boolean2;
        this.owner = bec;
        this.addSlot(new ResultSlot(beb.player, this.craftSlots, this.resultSlots, 0, 154, 28));
        for (int integer5 = 0; integer5 < 2; ++integer5) {
            for (int integer6 = 0; integer6 < 2; ++integer6) {
                this.addSlot(new Slot(this.craftSlots, integer6 + integer5 * 2, 98 + integer6 * 18, 18 + integer5 * 18));
            }
        }
        for (int integer5 = 0; integer5 < 4; ++integer5) {
            final EquipmentSlot aor6 = InventoryMenu.SLOT_IDS[integer5];
            this.addSlot(new Slot(beb, 39 - integer5, 8, 8 + integer5 * 18) {
                @Override
                public int getMaxStackSize() {
                    return 1;
                }
                
                @Override
                public boolean mayPlace(final ItemStack bki) {
                    return aor6 == Mob.getEquipmentSlotForItem(bki);
                }
                
                @Override
                public boolean mayPickup(final Player bec) {
                    final ItemStack bki3 = this.getItem();
                    return (bki3.isEmpty() || bec.isCreative() || !EnchantmentHelper.hasBindingCurse(bki3)) && super.mayPickup(bec);
                }
                
                @Override
                public Pair<ResourceLocation, ResourceLocation> getNoItemIcon() {
                    return (Pair<ResourceLocation, ResourceLocation>)Pair.of(InventoryMenu.BLOCK_ATLAS, InventoryMenu.TEXTURE_EMPTY_SLOTS[aor6.getIndex()]);
                }
            });
        }
        for (int integer5 = 0; integer5 < 3; ++integer5) {
            for (int integer6 = 0; integer6 < 9; ++integer6) {
                this.addSlot(new Slot(beb, integer6 + (integer5 + 1) * 9, 8 + integer6 * 18, 84 + integer5 * 18));
            }
        }
        for (int integer5 = 0; integer5 < 9; ++integer5) {
            this.addSlot(new Slot(beb, integer5, 8 + integer5 * 18, 142));
        }
        this.addSlot(new Slot(beb, 40, 77, 62) {
            @Override
            public Pair<ResourceLocation, ResourceLocation> getNoItemIcon() {
                return (Pair<ResourceLocation, ResourceLocation>)Pair.of(InventoryMenu.BLOCK_ATLAS, InventoryMenu.EMPTY_ARMOR_SLOT_SHIELD);
            }
        });
    }
    
    @Override
    public void fillCraftSlotsStackedContents(final StackedContents bee) {
        this.craftSlots.fillStackedContents(bee);
    }
    
    @Override
    public void clearCraftingContent() {
        this.resultSlots.clearContent();
        this.craftSlots.clearContent();
    }
    
    @Override
    public boolean recipeMatches(final Recipe<? super CraftingContainer> bmu) {
        return bmu.matches(this.craftSlots, this.owner.level);
    }
    
    @Override
    public void slotsChanged(final Container amz) {
        CraftingMenu.slotChangedCraftingGrid(this.containerId, this.owner.level, this.owner, this.craftSlots, this.resultSlots);
    }
    
    @Override
    public void removed(final Player bec) {
        super.removed(bec);
        this.resultSlots.clearContent();
        if (bec.level.isClientSide) {
            return;
        }
        this.clearContainer(bec, bec.level, this.craftSlots);
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return true;
    }
    
    @Override
    public ItemStack quickMoveStack(final Player bec, final int integer) {
        ItemStack bki4 = ItemStack.EMPTY;
        final Slot bhw5 = this.slots.get(integer);
        if (bhw5 != null && bhw5.hasItem()) {
            final ItemStack bki5 = bhw5.getItem();
            bki4 = bki5.copy();
            final EquipmentSlot aor7 = Mob.getEquipmentSlotForItem(bki4);
            if (integer == 0) {
                if (!this.moveItemStackTo(bki5, 9, 45, true)) {
                    return ItemStack.EMPTY;
                }
                bhw5.onQuickCraft(bki5, bki4);
            }
            else if (integer >= 1 && integer < 5) {
                if (!this.moveItemStackTo(bki5, 9, 45, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 5 && integer < 9) {
                if (!this.moveItemStackTo(bki5, 9, 45, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (aor7.getType() == EquipmentSlot.Type.ARMOR && !this.slots.get(8 - aor7.getIndex()).hasItem()) {
                final int integer2 = 8 - aor7.getIndex();
                if (!this.moveItemStackTo(bki5, integer2, integer2 + 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (aor7 == EquipmentSlot.OFFHAND && !this.slots.get(45).hasItem()) {
                if (!this.moveItemStackTo(bki5, 45, 46, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 9 && integer < 36) {
                if (!this.moveItemStackTo(bki5, 36, 45, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 36 && integer < 45) {
                if (!this.moveItemStackTo(bki5, 9, 36, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(bki5, 9, 45, false)) {
                return ItemStack.EMPTY;
            }
            if (bki5.isEmpty()) {
                bhw5.set(ItemStack.EMPTY);
            }
            else {
                bhw5.setChanged();
            }
            if (bki5.getCount() == bki4.getCount()) {
                return ItemStack.EMPTY;
            }
            final ItemStack bki6 = bhw5.onTake(bec, bki5);
            if (integer == 0) {
                bec.drop(bki6, false);
            }
        }
        return bki4;
    }
    
    @Override
    public boolean canTakeItemForPickAll(final ItemStack bki, final Slot bhw) {
        return bhw.container != this.resultSlots && super.canTakeItemForPickAll(bki, bhw);
    }
    
    @Override
    public int getResultSlotIndex() {
        return 0;
    }
    
    @Override
    public int getGridWidth() {
        return this.craftSlots.getWidth();
    }
    
    @Override
    public int getGridHeight() {
        return this.craftSlots.getHeight();
    }
    
    @Override
    public int getSize() {
        return 5;
    }
    
    public CraftingContainer getCraftSlots() {
        return this.craftSlots;
    }
    
    static {
        BLOCK_ATLAS = new ResourceLocation("textures/atlas/blocks.png");
        EMPTY_ARMOR_SLOT_HELMET = new ResourceLocation("item/empty_armor_slot_helmet");
        EMPTY_ARMOR_SLOT_CHESTPLATE = new ResourceLocation("item/empty_armor_slot_chestplate");
        EMPTY_ARMOR_SLOT_LEGGINGS = new ResourceLocation("item/empty_armor_slot_leggings");
        EMPTY_ARMOR_SLOT_BOOTS = new ResourceLocation("item/empty_armor_slot_boots");
        EMPTY_ARMOR_SLOT_SHIELD = new ResourceLocation("item/empty_armor_slot_shield");
        TEXTURE_EMPTY_SLOTS = new ResourceLocation[] { InventoryMenu.EMPTY_ARMOR_SLOT_BOOTS, InventoryMenu.EMPTY_ARMOR_SLOT_LEGGINGS, InventoryMenu.EMPTY_ARMOR_SLOT_CHESTPLATE, InventoryMenu.EMPTY_ARMOR_SLOT_HELMET };
        SLOT_IDS = new EquipmentSlot[] { EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET };
    }
}
