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 ayi, final boolean boolean2, final Player ayj) {
        super(null, 0);
        this.craftSlots = new CraftingContainer(this, 2, 2);
        this.resultSlots = new ResultContainer();
        this.active = boolean2;
        this.owner = ayj;
        this.addSlot(new ResultSlot(ayi.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 akv6 = InventoryMenu.SLOT_IDS[integer5];
            this.addSlot(new Slot(ayi, 39 - integer5, 8, 8 + integer5 * 18) {
                @Override
                public int getMaxStackSize() {
                    return 1;
                }
                
                @Override
                public boolean mayPlace(final ItemStack ben) {
                    return akv6 == Mob.getEquipmentSlotForItem(ben);
                }
                
                @Override
                public boolean mayPickup(final Player ayj) {
                    final ItemStack ben3 = this.getItem();
                    return (ben3.isEmpty() || ayj.isCreative() || !EnchantmentHelper.hasBindingCurse(ben3)) && super.mayPickup(ayj);
                }
                
                @Override
                public Pair<ResourceLocation, ResourceLocation> getNoItemIcon() {
                    return (Pair<ResourceLocation, ResourceLocation>)Pair.of(InventoryMenu.BLOCK_ATLAS, InventoryMenu.TEXTURE_EMPTY_SLOTS[akv6.getIndex()]);
                }
            });
        }
        for (int integer5 = 0; integer5 < 3; ++integer5) {
            for (int integer6 = 0; integer6 < 9; ++integer6) {
                this.addSlot(new Slot(ayi, integer6 + (integer5 + 1) * 9, 8 + integer6 * 18, 84 + integer5 * 18));
            }
        }
        for (int integer5 = 0; integer5 < 9; ++integer5) {
            this.addSlot(new Slot(ayi, integer5, 8 + integer5 * 18, 142));
        }
        this.addSlot(new Slot(ayi, 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 ayl) {
        this.craftSlots.fillStackedContents(ayl);
    }
    
    @Override
    public void clearCraftingContent() {
        this.resultSlots.clearContent();
        this.craftSlots.clearContent();
    }
    
    @Override
    public boolean recipeMatches(final Recipe<? super CraftingContainer> bgw) {
        return bgw.matches(this.craftSlots, this.owner.level);
    }
    
    @Override
    public void slotsChanged(final Container aje) {
        CraftingMenu.slotChangedCraftingGrid(this.containerId, this.owner.level, this.owner, this.craftSlots, this.resultSlots);
    }
    
    @Override
    public void removed(final Player ayj) {
        super.removed(ayj);
        this.resultSlots.clearContent();
        if (ayj.level.isClientSide) {
            return;
        }
        this.clearContainer(ayj, ayj.level, this.craftSlots);
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return true;
    }
    
    @Override
    public ItemStack quickMoveStack(final Player ayj, final int integer) {
        ItemStack ben4 = ItemStack.EMPTY;
        final Slot bca5 = this.slots.get(integer);
        if (bca5 != null && bca5.hasItem()) {
            final ItemStack ben5 = bca5.getItem();
            ben4 = ben5.copy();
            final EquipmentSlot akv7 = Mob.getEquipmentSlotForItem(ben4);
            if (integer == 0) {
                if (!this.moveItemStackTo(ben5, 9, 45, true)) {
                    return ItemStack.EMPTY;
                }
                bca5.onQuickCraft(ben5, ben4);
            }
            else if (integer >= 1 && integer < 5) {
                if (!this.moveItemStackTo(ben5, 9, 45, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 5 && integer < 9) {
                if (!this.moveItemStackTo(ben5, 9, 45, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (akv7.getType() == EquipmentSlot.Type.ARMOR && !this.slots.get(8 - akv7.getIndex()).hasItem()) {
                final int integer2 = 8 - akv7.getIndex();
                if (!this.moveItemStackTo(ben5, integer2, integer2 + 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (akv7 == EquipmentSlot.OFFHAND && !this.slots.get(45).hasItem()) {
                if (!this.moveItemStackTo(ben5, 45, 46, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 9 && integer < 36) {
                if (!this.moveItemStackTo(ben5, 36, 45, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer >= 36 && integer < 45) {
                if (!this.moveItemStackTo(ben5, 9, 36, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(ben5, 9, 45, false)) {
                return ItemStack.EMPTY;
            }
            if (ben5.isEmpty()) {
                bca5.set(ItemStack.EMPTY);
            }
            else {
                bca5.setChanged();
            }
            if (ben5.getCount() == ben4.getCount()) {
                return ItemStack.EMPTY;
            }
            final ItemStack ben6 = bca5.onTake(ayj, ben5);
            if (integer == 0) {
                ayj.drop(ben6, false);
            }
        }
        return ben4;
    }
    
    @Override
    public boolean canTakeItemForPickAll(final ItemStack ben, final Slot bca) {
        return bca.container != this.resultSlots && super.canTakeItemForPickAll(ben, bca);
    }
    
    @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;
    }
    
    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 };
    }
}
