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 beb, final Container amz, final AbstractHorse azm) {
        super(null, integer);
        this.horseContainer = amz;
        this.horse = azm;
        final int integer2 = 3;
        amz.startOpen(beb.player);
        final int integer3 = -18;
        this.addSlot(new Slot(amz, 0, 8, 18) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return bki.getItem() == Items.SADDLE && !this.hasItem() && azm.isSaddleable();
            }
            
            @Override
            public boolean isActive() {
                return azm.isSaddleable();
            }
        });
        this.addSlot(new Slot(amz, 1, 8, 36) {
            @Override
            public boolean mayPlace(final ItemStack bki) {
                return azm.isArmor(bki);
            }
            
            @Override
            public boolean isActive() {
                return azm.canWearArmor();
            }
            
            @Override
            public int getMaxStackSize() {
                return 1;
            }
        });
        if (azm instanceof AbstractChestedHorse && ((AbstractChestedHorse)azm).hasChest()) {
            for (int integer4 = 0; integer4 < 3; ++integer4) {
                for (int integer5 = 0; integer5 < ((AbstractChestedHorse)azm).getInventoryColumns(); ++integer5) {
                    this.addSlot(new Slot(amz, 2 + integer5 + integer4 * ((AbstractChestedHorse)azm).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(beb, integer5 + integer4 * 9 + 9, 8 + integer5 * 18, 102 + integer4 * 18 - 18));
            }
        }
        for (int integer4 = 0; integer4 < 9; ++integer4) {
            this.addSlot(new Slot(beb, integer4, 8 + integer4 * 18, 142));
        }
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return this.horseContainer.stillValid(bec) && this.horse.isAlive() && this.horse.distanceTo(bec) < 8.0f;
    }
    
    @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 int integer2 = this.horseContainer.getContainerSize();
            if (integer < integer2) {
                if (!this.moveItemStackTo(bki5, integer2, this.slots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (this.getSlot(1).mayPlace(bki5) && !this.getSlot(1).hasItem()) {
                if (!this.moveItemStackTo(bki5, 1, 2, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (this.getSlot(0).mayPlace(bki5)) {
                if (!this.moveItemStackTo(bki5, 0, 1, false)) {
                    return ItemStack.EMPTY;
                }
            }
            else if (integer2 <= 2 || !this.moveItemStackTo(bki5, 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(bki5, integer3, integer4, false)) {
                        return ItemStack.EMPTY;
                    }
                }
                else if (integer >= integer3 && integer < integer4) {
                    if (!this.moveItemStackTo(bki5, integer5, integer6, false)) {
                        return ItemStack.EMPTY;
                    }
                }
                else if (!this.moveItemStackTo(bki5, integer5, integer4, false)) {
                    return ItemStack.EMPTY;
                }
                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.horseContainer.stopOpen(bec);
    }
}
