package refinedstorage.tile.externalstorage;

import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;
import refinedstorage.tile.config.ModeFilter;

import java.util.List;

public class DrawerStorage extends ExternalStorage {
    private TileExternalStorage externalStorage;
    private IDrawer drawer;

    public DrawerStorage(TileExternalStorage externalStorage, IDrawer drawer) {
        this.externalStorage = externalStorage;
        this.drawer = drawer;
    }

    @Override
    public int getCapacity() {
        return drawer.getMaxCapacity();
    }

    @Override
    public void addItems(List<ItemStack> items) {
        if (!drawer.isEmpty()) {
            items.add(drawer.getStoredItemCopy());
        }
    }

    @Override
    public ItemStack push(ItemStack stack, boolean simulate) {
        if (ModeFilter.respectsMode(externalStorage.getFilters(), externalStorage, externalStorage.getCompare(), stack) && drawer.canItemBeStored(stack)) {
            if (drawer.isEmpty()) {
                if (getStored() + stack.stackSize > getCapacity()) {
                    int remainingSpace = getCapacity() - getStored();

                    if (remainingSpace <= 0) {
                        return stack;
                    }

                    if (!simulate) {
                        drawer.setStoredItem(stack, remainingSpace);
                    }

                    return ItemHandlerHelper.copyStackWithSize(stack, stack.stackSize - remainingSpace);
                } else {
                    if (!simulate) {
                        drawer.setStoredItem(stack, stack.stackSize);
                    }

                    return null;
                }
            } else {
                if (getStored() + stack.stackSize > getCapacity()) {
                    int remainingSpace = getCapacity() - getStored();

                    if (remainingSpace <= 0) {
                        return stack;
                    }

                    if (!simulate) {
                        drawer.setStoredItemCount(drawer.getStoredItemCount() + remainingSpace);
                    }

                    return ItemHandlerHelper.copyStackWithSize(stack, stack.stackSize - remainingSpace);
                } else {
                    if (!simulate) {
                        drawer.setStoredItemCount(drawer.getStoredItemCount() + stack.stackSize);
                    }

                    return null;
                }
            }
        }

        return stack;
    }

    @Override
    public ItemStack take(ItemStack stack, int size, int flags) {
        return null;
    }

    @Override
    public int getStored() {
        return drawer.getStoredItemCount();
    }

    @Override
    public int getPriority() {
        return externalStorage.getPriority();
    }
}

@@ -1066,16 +1066,17 @@
     if (
+!
 drawer.i
@@ -1131,35 +1131,45 @@
 ack.stackSize %3E 
-get
+drawer.getMax
 Capacity()) %7B%0A  
@@ -1153,32 +1153,37 @@
 .getMaxCapacity(
+stack
 )) %7B%0A           
@@ -1431,23 +1431,51 @@
 oredItem
-(stack,
+Count(drawer.getStoredItemCount() +
  remaini
@@ -1718,23 +1718,51 @@
 oredItem
-(stack,
+Count(drawer.getStoredItemCount() +
  stack.s
@@ -1919,19 +1919,29 @@
 kSize %3E 
-get
+drawer.getMax
 Capacity
@@ -1941,16 +1941,21 @@
 apacity(
+stack
 )) %7B%0A   
@@ -2215,43 +2215,15 @@
 Item
-Count(drawer.getStoredItemCount() +
+(stack,
  rem
@@ -2474,43 +2474,15 @@
 Item
-Count(drawer.getStoredItemCount() +
+(stack,
  sta

Fix pushing to drawers