package net.minecraft.world;

import java.util.AbstractList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.entity.player.Player;
import java.util.Iterator;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.Item;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import java.util.List;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.NonNullList;
import net.minecraft.world.inventory.StackedContentsCompatible;

public class SimpleContainer implements Container, StackedContentsCompatible {
    private final int size;
    private final NonNullList<ItemStack> items;
    private List<ContainerListener> listeners;
    
    public SimpleContainer(final int integer) {
        this.size = integer;
        this.items = NonNullList.<ItemStack>withSize(integer, ItemStack.EMPTY);
    }
    
    public SimpleContainer(final ItemStack... arr) {
        this.size = arr.length;
        this.items = NonNullList.<ItemStack>of(ItemStack.EMPTY, arr);
    }
    
    public void addListener(final ContainerListener anb) {
        if (this.listeners == null) {
            this.listeners = Lists.newArrayList();
        }
        this.listeners.add(anb);
    }
    
    public void removeListener(final ContainerListener anb) {
        this.listeners.remove(anb);
    }
    
    @Override
    public ItemStack getItem(final int integer) {
        if (integer < 0 || integer >= this.items.size()) {
            return ItemStack.EMPTY;
        }
        return this.items.get(integer);
    }
    
    public List<ItemStack> removeAllItems() {
        final List<ItemStack> list2 = this.items.stream().filter(bki -> !bki.isEmpty()).collect(Collectors.toList());
        this.clearContent();
        return list2;
    }
    
    @Override
    public ItemStack removeItem(final int integer1, final int integer2) {
        final ItemStack bki4 = ContainerHelper.removeItem(this.items, integer1, integer2);
        if (!bki4.isEmpty()) {
            this.setChanged();
        }
        return bki4;
    }
    
    public ItemStack removeItemType(final Item bke, final int integer) {
        final ItemStack bki4 = new ItemStack(bke, 0);
        for (int integer2 = this.size - 1; integer2 >= 0; --integer2) {
            final ItemStack bki5 = this.getItem(integer2);
            if (bki5.getItem().equals(bke)) {
                final int integer3 = integer - bki4.getCount();
                final ItemStack bki6 = bki5.split(integer3);
                bki4.grow(bki6.getCount());
                if (bki4.getCount() == integer) {
                    break;
                }
            }
        }
        if (!bki4.isEmpty()) {
            this.setChanged();
        }
        return bki4;
    }
    
    public ItemStack addItem(final ItemStack bki) {
        final ItemStack bki2 = bki.copy();
        this.moveItemToOccupiedSlotsWithSameType(bki2);
        if (bki2.isEmpty()) {
            return ItemStack.EMPTY;
        }
        this.moveItemToEmptySlots(bki2);
        if (bki2.isEmpty()) {
            return ItemStack.EMPTY;
        }
        return bki2;
    }
    
    public boolean canAddItem(final ItemStack bki) {
        boolean boolean3 = false;
        for (final ItemStack bki2 : this.items) {
            if (bki2.isEmpty() || (this.isSameItem(bki2, bki) && bki2.getCount() < bki2.getMaxStackSize())) {
                boolean3 = true;
                break;
            }
        }
        return boolean3;
    }
    
    @Override
    public ItemStack removeItemNoUpdate(final int integer) {
        final ItemStack bki3 = this.items.get(integer);
        if (bki3.isEmpty()) {
            return ItemStack.EMPTY;
        }
        this.items.set(integer, ItemStack.EMPTY);
        return bki3;
    }
    
    @Override
    public void setItem(final int integer, final ItemStack bki) {
        this.items.set(integer, bki);
        if (!bki.isEmpty() && bki.getCount() > this.getMaxStackSize()) {
            bki.setCount(this.getMaxStackSize());
        }
        this.setChanged();
    }
    
    @Override
    public int getContainerSize() {
        return this.size;
    }
    
    @Override
    public boolean isEmpty() {
        for (final ItemStack bki3 : this.items) {
            if (!bki3.isEmpty()) {
                return false;
            }
        }
        return true;
    }
    
    @Override
    public void setChanged() {
        if (this.listeners != null) {
            for (final ContainerListener anb3 : this.listeners) {
                anb3.containerChanged(this);
            }
        }
    }
    
    @Override
    public boolean stillValid(final Player bec) {
        return true;
    }
    
    @Override
    public void clearContent() {
        this.items.clear();
        this.setChanged();
    }
    
    @Override
    public void fillStackedContents(final StackedContents bee) {
        for (final ItemStack bki4 : this.items) {
            bee.accountStack(bki4);
        }
    }
    
    @Override
    public String toString() {
        return this.items.stream().filter(bki -> !bki.isEmpty()).collect(Collectors.toList()).toString();
    }
    
    private void moveItemToEmptySlots(final ItemStack bki) {
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            final ItemStack bki2 = this.getItem(integer3);
            if (bki2.isEmpty()) {
                this.setItem(integer3, bki.copy());
                bki.setCount(0);
                return;
            }
        }
    }
    
    private void moveItemToOccupiedSlotsWithSameType(final ItemStack bki) {
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            final ItemStack bki2 = this.getItem(integer3);
            if (this.isSameItem(bki2, bki)) {
                this.moveItemsBetweenStacks(bki, bki2);
                if (bki.isEmpty()) {
                    return;
                }
            }
        }
    }
    
    private boolean isSameItem(final ItemStack bki1, final ItemStack bki2) {
        return bki1.getItem() == bki2.getItem() && ItemStack.tagMatches(bki1, bki2);
    }
    
    private void moveItemsBetweenStacks(final ItemStack bki1, final ItemStack bki2) {
        final int integer4 = Math.min(this.getMaxStackSize(), bki2.getMaxStackSize());
        final int integer5 = Math.min(bki1.getCount(), integer4 - bki2.getCount());
        if (integer5 > 0) {
            bki2.grow(integer5);
            bki1.shrink(integer5);
            this.setChanged();
        }
    }
    
    public void fromTag(final ListTag lk) {
        for (int integer3 = 0; integer3 < lk.size(); ++integer3) {
            final ItemStack bki4 = ItemStack.of(lk.getCompound(integer3));
            if (!bki4.isEmpty()) {
                this.addItem(bki4);
            }
        }
    }
    
    public ListTag createTag() {
        final ListTag lk2 = new ListTag();
        for (int integer3 = 0; integer3 < this.getContainerSize(); ++integer3) {
            final ItemStack bki4 = this.getItem(integer3);
            if (!bki4.isEmpty()) {
                ((AbstractList<CompoundTag>)lk2).add(bki4.save(new CompoundTag()));
            }
        }
        return lk2;
    }
}
