package net.minecraft.world.inventory;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.animal.horse.AbstractChestedHorse;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.animal.horse.AbstractHorse;
import net.minecraft.world.Container;

public class HorseInventoryMenu extends AbstractContainerMenu {
    private final Container horseContainer;
    private final AbstractHorse horse;
    
    public HorseInventoryMenu(final int integer, final Inventory ayi, final Container aje, final AbstractHorse aue) {
        super(null, integer);
        this.horseContainer = aje;
        this.horse = aue;
        final int integer2 = 3;
        aje.startOpen(ayi.player);
        final int integer3 = -18;
        this.addSlot(new Slot(aje, 0, 8, 18) {
            @Override
            public boolean mayPlace(final ItemStack ben) {
                return ben.getItem() == Items.SADDLE && !this.hasItem() && aue.canBeSaddled();
            }
            
            @Override
            public boolean isActive() {
                return aue.canBeSaddled();
            }
        });
        this.addSlot(new Slot(aje, 1, 8, 36) {
            @Override
            public boolean mayPlace(final ItemStack ben) {
                return aue.isArmor(ben);
            }
            
            @Override
            public boolean isActive() {
                return aue.wearsArmor();
            }
            
            @Override
            public int getMaxStackSize() {
                return 1;
            }
        });
        if (aue instanceof AbstractChestedHorse && ((AbstractChestedHorse)aue).hasChest()) {
            for (int integer4 = 0; integer4 < 3; ++integer4) {
                for (int integer5 = 0; integer5 < ((AbstractChestedHorse)aue).getInventoryColumns(); ++integer5) {
                    this.addSlot(new Slot(aje, 2 + integer5 + integer4 * ((AbstractChestedHorse)aue).getInventoryColumns(), 80 + integer5 * 18, 18 + integer4 * 18));
                }
            }
        }
        for (int integer4 = 0; integer4 < 3; ++integer4) {
            for (int integer5 = 0; integer5 < 9; ++integer5) {
                this.addSlot(new Slot(ayi, integer5 + integer4 * 9 + 9, 8 + integer5 * 18, 102 + integer4 * 18 - 18));
            }
        }
        for (int integer4 = 0; integer4 < 9; ++integer4) {
            this.addSlot(new Slot(ayi, integer4, 8 + integer4 * 18, 142));
        }
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return this.horseContainer.stillValid(ayj) && this.horse.isAlive() && this.horse.distanceTo(ayj) < 8.0f;
    }
    
    @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 int integer2 = this.horseContainer.getContainerSize();
            if (integer < integer2) {
                if (!this.moveItemStackTo(ben5, integer2, this.slots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (this.getSlot(1).mayPlace(ben5) && !this.getSlot(1).hasItem()) {
                if (!this.moveItemStackTo(ben5, 1, 2, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (this.getSlot(0).mayPlace(ben5)) {
                if (!this.moveItemStackTo(ben5, 0, 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer2 <= 2 || !this.moveItemStackTo(ben5, 2, integer2, false)) {
                final int integer3 = integer2;
                final int integer5;
                final int integer4 = integer5 = integer3 + 27;
                final int integer6 = integer5 + 9;
                if (integer >= integer5 && integer < integer6) {
                    if (!this.moveItemStackTo(ben5, integer3, integer4, false)) {
                        return ItemStack.EMPTY;
                    }
                }
                else if (integer >= integer3 && integer < integer4) {
                    if (!this.moveItemStackTo(ben5, integer5, integer6, false)) {
                        return ItemStack.EMPTY;
                    }
                }
                else if (!this.moveItemStackTo(ben5, integer5, integer4, false)) {
                    return ItemStack.EMPTY;
                }
                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.horseContainer.stopOpen(ayj);
    }
}
