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