package net.minecraft.world.inventory;

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.Container;

public class ChestMenu extends AbstractContainerMenu {
    private final Container container;
    private final int containerRows;
    
    private ChestMenu(final MenuType<?> bbo, final int integer2, final Inventory ayi, final int integer4) {
        this(bbo, integer2, ayi, new SimpleContainer(9 * integer4), integer4);
    }
    
    public static ChestMenu oneRow(final int integer, final Inventory ayi) {
        return new ChestMenu(MenuType.GENERIC_9x1, integer, ayi, 1);
    }
    
    public static ChestMenu twoRows(final int integer, final Inventory ayi) {
        return new ChestMenu(MenuType.GENERIC_9x2, integer, ayi, 2);
    }
    
    public static ChestMenu threeRows(final int integer, final Inventory ayi) {
        return new ChestMenu(MenuType.GENERIC_9x3, integer, ayi, 3);
    }
    
    public static ChestMenu fourRows(final int integer, final Inventory ayi) {
        return new ChestMenu(MenuType.GENERIC_9x4, integer, ayi, 4);
    }
    
    public static ChestMenu fiveRows(final int integer, final Inventory ayi) {
        return new ChestMenu(MenuType.GENERIC_9x5, integer, ayi, 5);
    }
    
    public static ChestMenu sixRows(final int integer, final Inventory ayi) {
        return new ChestMenu(MenuType.GENERIC_9x6, integer, ayi, 6);
    }
    
    public static ChestMenu threeRows(final int integer, final Inventory ayi, final Container aje) {
        return new ChestMenu(MenuType.GENERIC_9x3, integer, ayi, aje, 3);
    }
    
    public static ChestMenu sixRows(final int integer, final Inventory ayi, final Container aje) {
        return new ChestMenu(MenuType.GENERIC_9x6, integer, ayi, aje, 6);
    }
    
    public ChestMenu(final MenuType<?> bbo, final int integer2, final Inventory ayi, final Container aje, final int integer5) {
        super(bbo, integer2);
        AbstractContainerMenu.checkContainerSize(aje, integer5 * 9);
        this.container = aje;
        this.containerRows = integer5;
        aje.startOpen(ayi.player);
        final int integer6 = (this.containerRows - 4) * 18;
        for (int integer7 = 0; integer7 < this.containerRows; ++integer7) {
            for (int integer8 = 0; integer8 < 9; ++integer8) {
                this.addSlot(new Slot(aje, integer8 + integer7 * 9, 8 + integer8 * 18, 18 + integer7 * 18));
            }
        }
        for (int integer7 = 0; integer7 < 3; ++integer7) {
            for (int integer8 = 0; integer8 < 9; ++integer8) {
                this.addSlot(new Slot(ayi, integer8 + integer7 * 9 + 9, 8 + integer8 * 18, 103 + integer7 * 18 + integer6));
            }
        }
        for (int integer7 = 0; integer7 < 9; ++integer7) {
            this.addSlot(new Slot(ayi, integer7, 8 + integer7 * 18, 161 + integer6));
        }
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return this.container.stillValid(ayj);
    }
    
    @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();
            if (integer < this.containerRows * 9) {
                if (!this.moveItemStackTo(ben5, this.containerRows * 9, this.slots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (!this.moveItemStackTo(ben5, 0, this.containerRows * 9, false)) {
                return ItemStack.EMPTY;
            }
            if (ben5.isEmpty()) {
                bca5.set(ItemStack.EMPTY);
            }
            else {
                bca5.setChanged();
            }
        }
        return ben4;
    }
    
    @Override
    public void removed(final Player ayj) {
        super.removed(ayj);
        this.container.stopOpen(ayj);
    }
    
    public Container getContainer() {
        return this.container;
    }
    
    public int getRowCount() {
        return this.containerRows;
    }
}
