package com.detrav.gui.containers;

import com.detrav.items.DetravMetaGeneratedTool01;
import gregtech.api.items.GT_MetaBase_Item;
import gregtech.common.items.armor.SlotLocked;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentText;
import net.minecraft.world.World;
import org.lwjgl.Sys;

/**
 * Created by wital_000 on 07.04.2016.
 */
public class DetravPortableChargerContainer extends Container {
    public IInventory slots = new InventoryCrafting(this, 1, 1);
    public ItemStack mItem;
    private World worldObj;

    public DetravPortableChargerContainer(InventoryPlayer inventoryPlayer, World aWorld,ItemStack aStack) {
        this.worldObj = aWorld;
        this.addSlotToContainer(new Slot(slots, 0, 80, 35));
        mItem = aStack;
        //new Slot()
        bindPlayerInventory(inventoryPlayer);

        //DetravMetaGeneratedTool01.INSTANCE.getToolStats(mItem).
    }

    protected void bindPlayerInventory(InventoryPlayer inventoryPlayer) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
                        8 + j * 18, 84 + i * 18));
            }
        }


        for (int i = 0; i < 9; i++) {
            ItemStack stackInSlot = inventoryPlayer.getStackInSlot(i);
            if (mItem!=null && stackInSlot!=null && mItem == stackInSlot) {
                addSlotToContainer(new SlotLocked(inventoryPlayer, i, 8 + i * 18, 142));
            } else {
                addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
            }
        }
    }

    @Override
    public boolean canInteractWith(EntityPlayer p_75145_1_) {
        return true;
    }

    public void onContainerClosed(EntityPlayer p_75134_1_) {
        super.onContainerClosed(p_75134_1_);
        if (!this.worldObj.isRemote) {
            //for (int i = 0; i < 9; ++i)
            //{
            ItemStack itemstack = this.slots.getStackInSlotOnClosing(0);

            if (itemstack != null) {
                p_75134_1_.dropPlayerItemWithRandomChoice(itemstack, false);
            }
            //}
        }
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {
        return null;
    }

    public void onUpdate(GT_MetaBase_Item aItem, ItemStack aStack, World aWorld, Entity aPlayer, int aTimer) {
        if(mItem==null) return;

        ItemStack item = this.slots.getStackInSlot(0);
        if(item == null) return;

        Long[] itemStats = DetravMetaGeneratedTool01.INSTANCE.getElectricStats(item);
        if(itemStats == null) return;
        long itemCharge = DetravMetaGeneratedTool01.INSTANCE.getRealCharge(item);
        if(itemCharge <=0) return;
        long needEnergy = itemStats[1] - itemCharge;
        if(needEnergy == 0) return;

        Long[] selfStats = DetravMetaGeneratedTool01.INSTANCE.getElectricStats(mItem);
        if(selfStats == null) return;

        long selfCharge = DetravMetaGeneratedTool01.INSTANCE.getRealCharge(mItem);
        if(selfCharge <=0) return;

        long loss = DetravMetaGeneratedTool01.INSTANCE.getElectricStatsLoss(mItem);
        if(loss<0) return;

        long energyToTransfer = (Math.min(selfStats[1],itemStats[1]) - loss)*aTimer ;

        if(needEnergy<energyToTransfer)
            energyToTransfer = needEnergy;
        long removeEnergy = Math.min(energyToTransfer+loss*aTimer,selfCharge);

        DetravMetaGeneratedTool01.INSTANCE.setCharge(item,itemCharge+energyToTransfer);
        DetravMetaGeneratedTool01.INSTANCE.setCharge(mItem,selfCharge-removeEnergy);
    }

    /*@Override
    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {
        ItemStack result = super.transferStackInSlot(par1EntityPlayer,par2);
        par1EntityPlayer.addChatMessage(new ChatComponentText(result.getDisplayName()));

        return result;
    }*/
}
@@ -120,24 +120,64 @@
 aBase_Item;%0A
+import gregtech.api.util.GT_ModHandler;%0A
 import gregt
@@ -2919,329 +2919,76 @@
     
-Long%5B%5D itemStats = DetravMetaGeneratedTool01.INSTANCE.getElectricStats(item);%0A        if(itemStats == null) return;%0A        long itemCharge = DetravMetaGeneratedTool01.INSTANCE.getRealCharge(item);%0A        if(itemCharge %3C=0) return;%0A        long needEnergy = itemStats%5B1%5D - itemCharge;%0A        if(needEnergy == 0) return;
+if(GT_ModHandler.isElectricItem(item));%0A%0A        //GT_ModHandler.ch%0A
 %0A%0A  
@@ -3378,17 +3378,8 @@
 r = 
-(Math.min
 (sel
@@ -3391,22 +3391,8 @@
 s%5B1%5D
-,itemStats%5B1%5D)
  - l
@@ -3419,80 +3419,121 @@
    i
-f(needEnergy%3CenergyToTransfer)%0A            energyToTransfer = needEnergy
+nt removeEnergy = GT_ModHandler.chargeElectricItem(item, (int) energyToTransfer, Integer.MAX_VALUE, false, false)
 ;%0A  
@@ -3538,21 +3538,19 @@
         
-long 
+if(
 removeEn
@@ -3557,233 +3557,137 @@
 ergy
- = Math.min(energyToTransfer+loss*aTimer,selfCharge);%0A%0A        DetravMetaGeneratedTool01.INSTANCE.setCharge(item,itemCharge+energyToTransfer);%0A        DetravMetaGeneratedTool01.INSTANCE.setCharge(mItem,selfCharge-removeEnergy
+==0) return;%0A        GT_ModHandler.dischargeElectricItem(mItem,(int)(removeEnergy+loss*aTimer),Integer.MAX_VALUE,false,false,true
 );%0A 

Complete logic final!