package refinedstorage.tile.externalstorage;

import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IVoidable;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemHandlerHelper;
import refinedstorage.api.storage.AccessType;
import refinedstorage.apiimpl.API;
import refinedstorage.tile.config.IFilterable;

import java.util.Collections;
import java.util.List;

public class ItemStorageDrawer extends ItemStorageExternal {
    private TileExternalStorage externalStorage;
    private IDrawer drawer;

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

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

    @Override
    public List<ItemStack> getItems() {
        if (!drawer.isEmpty() && drawer.getStoredItemCount() > 0) {
            return Collections.singletonList(drawer.getStoredItemCopy());
        }

        return Collections.emptyList();
    }

    private boolean isVoidable() {
        return drawer instanceof IVoidable && ((IVoidable) drawer).isVoid();
    }

    @Override
    public ItemStack insertItem(ItemStack stack, int size, boolean simulate) {
        if (IFilterable.canTake(externalStorage.getItemFilters(), externalStorage.getMode(), externalStorage.getCompare(), stack) && drawer.canItemBeStored(stack)) {
            if (!drawer.isEmpty()) {
                if (getStored() + size > drawer.getMaxCapacity(stack)) {
                    int remainingSpace = getCapacity() - getStored();

                    if (remainingSpace <= 0) {
                        return isVoidable() ? null : ItemHandlerHelper.copyStackWithSize(stack, size);
                    }

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

                    return isVoidable() ? null : ItemHandlerHelper.copyStackWithSize(stack, size - remainingSpace);
                } else {
                    if (!simulate) {
                        drawer.setStoredItemCount(drawer.getStoredItemCount() + size);
                    }

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

                    if (remainingSpace <= 0) {
                        return isVoidable() ? null : ItemHandlerHelper.copyStackWithSize(stack, size);
                    }

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

                    return isVoidable() ? null : ItemHandlerHelper.copyStackWithSize(stack, size - remainingSpace);
                } else {
                    if (!simulate) {
                        drawer.setStoredItemRedir(stack, size);
                    }

                    return null;
                }
            }
        }

        return ItemHandlerHelper.copyStackWithSize(stack, size);
    }

    @Override
    public ItemStack extractItem(ItemStack stack, int size, int flags) {
        if (API.instance().getComparer().isEqual(stack, drawer.getStoredItemPrototype(), flags) && drawer.canItemBeExtracted(stack)) {
            if (size > drawer.getStoredItemCount()) {
                size = drawer.getStoredItemCount();
            }

            ItemStack stored = drawer.getStoredItemPrototype();

            drawer.setStoredItemCount(drawer.getStoredItemCount() - size);

            return ItemHandlerHelper.copyStackWithSize(stored, size);
        }

        return null;
    }

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

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

    @Override
    public AccessType getAccessType() {
        return externalStorage.getAccessType();
    }
}

@@ -1503,110 +1503,49 @@
    i
-f (!drawer.isEmpty()) %7B%0A                if (getStored() + size %3E drawer.getMaxCapacity(stack)) %7B%0A     
+nt stored = drawer.getStoredItemCount();%0A
     
@@ -1544,35 +1544,32 @@
 ();%0A            
-   
 int remainingSpa
@@ -1577,35 +1577,45 @@
 e = 
-getCapacity() - getS
+drawer.getMaxCapacity(stack) - s
 tored
-()
 ;%0A%0A 
@@ -1629,645 +1629,343 @@
     
-        if (remainingSpace %3C= 0) %7B%0A                        return isVoidable() ? null : ItemHandlerHelper.copyStackWithSize(stack, size);%0A                    %7D%0A%0A                    if (!simulate) %7B%0A                        drawer.setStoredItemCount(drawer.getStoredItemCount() + remainingSpace);%0A                    %7D%0A%0A                    return isVoidable() ? null : ItemHandlerHelper.copyStackWithSize(stack, size - remainingSpace);%0A                %7D else %7B%0A                    if (!simulate) %7B%0A                        drawer.setStoredItemCount(drawer.getStoredItemCount() + size);%0A                    %7D%0A%0A                    return null
+int inserted = remainingSpace %3E size ? size : (remainingSpace %3C= 0) ? 0 : remainingSpace;%0A%0A            if (!simulate && remainingSpace %3E 0) %7B%0A                if (drawer.isEmpty()) %7B%0A                    drawer.setStoredItemRedir(stack, inserted);%0A                %7D else %7B%0A                    drawer.setStoredItemCount(stored + inserted)
 ;%0A  
@@ -1989,36 +1989,26 @@
            %7D
- else %7B%0A    
+%0A%0A
             
@@ -2015,310 +2015,119 @@
 if (
-getStored() + size %3E drawer.getMaxCapacity(stack)) %7B%0A                    int remainingSpace = getCapacity() - getStored();%0A%0A                    if (remainingSpace %3C= 0) %7B%0A                        return isVoidable() ? null : ItemHandlerHelper.copyStackWithSize(stack, size);%0A                    %7D%0A%0A     
+inserted == size) %7B%0A                return null;%0A            %7D%0A%0A            int returnSize = size - inserted;%0A%0A
     
@@ -2134,32 +2134,32 @@
         
-   
 if (
-!simulate
+isVoidable()
 ) %7B%0A    
@@ -2174,93 +2174,49 @@
     
-   
-     drawer.setStoredItemRedir(stack, remainingSpace);%0A                    %7D%0A%0A    
+returnSize = -returnSize;%0A            %7D%0A%0A
     
@@ -2227,40 +2227,14 @@
     
-    
 return
- isVoidable() ? null :
  Ite
@@ -2277,245 +2277,20 @@
 ck, 
-size - remainingSpace);%0A                %7D else %7B%0A                    if (!simulate) %7B%0A                        drawer.setStoredItemRedir(stack, size);%0A                    %7D%0A%0A                    return null;%0A                %7D%0A            %7D
+returnSize);
 %0A   

Fix for void upgraded storage drawers using the previous developed void handling. (#454)