package net.minecraft.world;

import java.util.stream.Collector;
import java.util.stream.Collectors;
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 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 ajg) {
        if (this.listeners == null) {
            this.listeners = Lists.newArrayList();
        }
        this.listeners.add(ajg);
    }
    
    public void removeListener(final ContainerListener ajg) {
        this.listeners.remove(ajg);
    }
    
    @Override
    public ItemStack getItem(final int integer) {
        if (integer < 0 || integer >= this.items.size()) {
            return ItemStack.EMPTY;
        }
        return this.items.get(integer);
    }
    
    @Override
    public ItemStack removeItem(final int integer1, final int integer2) {
        final ItemStack ben4 = ContainerHelper.removeItem(this.items, integer1, integer2);
        if (!ben4.isEmpty()) {
            this.setChanged();
        }
        return ben4;
    }
    
    public ItemStack removeItemType(final Item bei, final int integer) {
        final ItemStack ben4 = new ItemStack(bei, 0);
        for (int integer2 = this.size - 1; integer2 >= 0; --integer2) {
            final ItemStack ben5 = this.getItem(integer2);
            if (ben5.getItem().equals(bei)) {
                final int integer3 = integer - ben4.getCount();
                final ItemStack ben6 = ben5.split(integer3);
                ben4.grow(ben6.getCount());
                if (ben4.getCount() == integer) {
                    break;
                }
            }
        }
        if (!ben4.isEmpty()) {
            this.setChanged();
        }
        return ben4;
    }
    
    public ItemStack addItem(final ItemStack ben) {
        final ItemStack ben2 = ben.copy();
        this.moveItemToOccupiedSlotsWithSameType(ben2);
        if (ben2.isEmpty()) {
            return ItemStack.EMPTY;
        }
        this.moveItemToEmptySlots(ben2);
        if (ben2.isEmpty()) {
            return ItemStack.EMPTY;
        }
        return ben2;
    }
    
    @Override
    public ItemStack removeItemNoUpdate(final int integer) {
        final ItemStack ben3 = this.items.get(integer);
        if (ben3.isEmpty()) {
            return ItemStack.EMPTY;
        }
        this.items.set(integer, ItemStack.EMPTY);
        return ben3;
    }
    
    @Override
    public void setItem(final int integer, final ItemStack ben) {
        this.items.set(integer, ben);
        if (!ben.isEmpty() && ben.getCount() > this.getMaxStackSize()) {
            ben.setCount(this.getMaxStackSize());
        }
        this.setChanged();
    }
    
    @Override
    public int getContainerSize() {
        return this.size;
    }
    
    @Override
    public boolean isEmpty() {
        for (final ItemStack ben3 : this.items) {
            if (!ben3.isEmpty()) {
                return false;
            }
        }
        return true;
    }
    
    @Override
    public void setChanged() {
        if (this.listeners != null) {
            for (final ContainerListener ajg3 : this.listeners) {
                ajg3.containerChanged(this);
            }
        }
    }
    
    @Override
    public boolean stillValid(final Player ayj) {
        return true;
    }
    
    @Override
    public void clearContent() {
        this.items.clear();
        this.setChanged();
    }
    
    @Override
    public void fillStackedContents(final StackedContents ayl) {
        for (final ItemStack ben4 : this.items) {
            ayl.accountStack(ben4);
        }
    }
    
    @Override
    public String toString() {
        return this.items.stream().filter(ben -> !ben.isEmpty()).collect(Collectors.toList()).toString();
    }
    
    private void moveItemToEmptySlots(final ItemStack ben) {
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            final ItemStack ben2 = this.getItem(integer3);
            if (ben2.isEmpty()) {
                this.setItem(integer3, ben.copy());
                ben.setCount(0);
                return;
            }
        }
    }
    
    private void moveItemToOccupiedSlotsWithSameType(final ItemStack ben) {
        for (int integer3 = 0; integer3 < this.size; ++integer3) {
            final ItemStack ben2 = this.getItem(integer3);
            if (ItemStack.isSame(ben2, ben)) {
                this.moveItemsBetweenStacks(ben, ben2);
                if (ben.isEmpty()) {
                    return;
                }
            }
        }
    }
    
    private void moveItemsBetweenStacks(final ItemStack ben1, final ItemStack ben2) {
        final int integer4 = Math.min(this.getMaxStackSize(), ben2.getMaxStackSize());
        final int integer5 = Math.min(ben1.getCount(), integer4 - ben2.getCount());
        if (integer5 > 0) {
            ben2.grow(integer5);
            ben1.shrink(integer5);
            this.setChanged();
        }
    }
}
