package org.cyclops.everlastingabilities.inventory.container;

import com.google.common.collect.Lists;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.cyclops.cyclopscore.helper.InventoryHelpers;
import org.cyclops.cyclopscore.inventory.IGuiContainerProviderConfigurable;
import org.cyclops.cyclopscore.inventory.container.ItemInventoryContainer;
import org.cyclops.everlastingabilities.ability.AbilityHelpers;
import org.cyclops.everlastingabilities.api.Ability;
import org.cyclops.everlastingabilities.api.capability.IMutableAbilityStore;
import org.cyclops.everlastingabilities.capability.MutableAbilityStoreConfig;
import org.cyclops.everlastingabilities.client.gui.GuiAbilityContainer;
import org.cyclops.everlastingabilities.item.ItemAbilityTotem;
import org.cyclops.everlastingabilities.item.ItemGuiAbilityContainer;

import javax.annotation.Nullable;
import java.util.List;

/**
 * Container for the labeller.
 * @author rubensworks
 */
public class ContainerAbilityContainer extends ItemInventoryContainer<ItemGuiAbilityContainer> {

    @SideOnly(Side.CLIENT)
    private GuiAbilityContainer gui;

    /**
     * Make a new instance.
     *
     * @param player The player.
     * @param itemIndex The index of the item in use inside the player inventory.
     */
    public ContainerAbilityContainer(EntityPlayer player, int itemIndex) {
        super(player.inventory, (ItemGuiAbilityContainer) InventoryHelpers.getItemFromIndex(player, itemIndex).getItem(), itemIndex);
        addInventory(player.inventory, 0, 8, 195, 1, 9);
    }

    @Override
    public IGuiContainerProviderConfigurable getGuiProvider() {
        return ItemAbilityTotem.getInstance();
    }

    @SideOnly(Side.CLIENT)
    public void setGui(GuiAbilityContainer gui) {
        this.gui = gui;
    }

    @SideOnly(Side.CLIENT)
    public GuiAbilityContainer getGui() {
        return this.gui;
    }

    @Override
    protected int getSizeInventory() {
        return 0;
    }


    public IMutableAbilityStore getPlayerAbilityStore() {
        return player.getCapability(MutableAbilityStoreConfig.CAPABILITY, null);
    }

    public IMutableAbilityStore getItemAbilityStore() {
        return getItemStack(player).getCapability(MutableAbilityStoreConfig.CAPABILITY, null);
    }

    public @Nullable List<Ability> getPlayerAbilities() {
        return Lists.newArrayList(getPlayerAbilityStore().getAbilities());
    }

    public List<Ability> getItemAbilities() {
        return Lists.newArrayList(getItemAbilityStore().getAbilities());
    }

    public void moveFromPlayer(Ability playerAbility) {
        Ability insertedAbility = AbilityHelpers.insert(playerAbility, getItemAbilityStore());
        AbilityHelpers.removePlayerAbility(player, insertedAbility, true, true);
    }

    public void moveToPlayer(Ability itemAbility) {
        Ability insertedAbility = AbilityHelpers.addPlayerAbility(player, itemAbility, true, true);
        AbilityHelpers.extract(insertedAbility, getItemAbilityStore());

        if(getItemAbilities().isEmpty() && !getItem().canMoveFromPlayer()) {
            player.inventory.setInventorySlotContents(itemIndex, null);

        }
    }

    @Override
    public boolean canInteractWith(EntityPlayer player) {
        return getItem().canMoveFromPlayer() || (getItemStack(player) != null && !getItemAbilities().isEmpty());
    }
}

@@ -141,24 +141,61 @@
 tityPlayer;%0A
+import net.minecraft.item.ItemStack;%0A
 import net.m
@@ -995,24 +995,54 @@
 n.Nullable;%0A
+import java.util.Collections;%0A
 import java.
@@ -2351,159 +2351,427 @@
     
-return getItemStack(player).getCapability(MutableAbilityStoreConfig.CAPABILITY, null);%0A    %7D%0A%0A    public @Nullable List%3CAbility%3E getPlayerAbilities() %7B
+ItemStack itemStack = getItemStack(player);%0A        if (itemStack == null) %7B%0A            return null;%0A        %7D%0A        return itemStack.getCapability(MutableAbilityStoreConfig.CAPABILITY, null);%0A    %7D%0A%0A    public @Nullable List%3CAbility%3E getPlayerAbilities() %7B%0A        IMutableAbilityStore abilityStore = getPlayerAbilityStore();%0A        if (abilityStore == null) %7B%0A            return Collections.emptyList();%0A        %7D
 %0A   
@@ -2801,26 +2801,17 @@
 rayList(
-getPlayerA
+a
 bilitySt
@@ -2805,34 +2805,32 @@
 ist(abilityStore
-()
 .getAbilities())
@@ -2876,32 +2876,189 @@
 emAbilities() %7B%0A
+        IMutableAbilityStore abilityStore = getItemAbilityStore();%0A        if (abilityStore == null) %7B%0A            return Collections.emptyList();%0A        %7D%0A
         return L
@@ -3075,24 +3075,17 @@
 rayList(
-getItemA
+a
 bilitySt
@@ -3087,18 +3087,16 @@
 ityStore
-()
 .getAbil
@@ -3841,66 +3841,66 @@
 Item
-().canMoveFromP
+Stack(p
 layer
-(
 ) 
-%7C%7C (getItemStack(p
+!= null && (getItem().canMoveFromP
 layer
+(
 ) 
-!= null &&
+%7C%7C
  !ge

Add safety for corrupted itemstacks while in inventory, Closes #18