package OpenTechnology.environment;

import OpenTechnology.packet.PacketTeleporter;
import OpenTechnology.proxy.CommonProxy;
import OpenTechnology.tileentities.TileEntityTeleporter;
import OpenTechnology.utils.Utils;
import li.cil.oc.api.Network;
import li.cil.oc.api.internal.Agent;
import li.cil.oc.api.internal.Robot;
import li.cil.oc.api.machine.Arguments;
import li.cil.oc.api.machine.Callback;
import li.cil.oc.api.machine.Context;
import li.cil.oc.api.machine.Machine;
import li.cil.oc.api.network.Connector;
import li.cil.oc.api.network.EnvironmentHost;
import li.cil.oc.api.network.Visibility;
import li.cil.oc.api.prefab.ManagedEnvironment;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;

import java.util.HashMap;

public class TesseractUpgrade extends ManagedEnvironment{
    public static HashMap<String, TesseractUpgrade> tesseractList = new HashMap<String, TesseractUpgrade>(  );

    private boolean connect;
    private EnvironmentHost container;

    public TesseractUpgrade( EnvironmentHost container ) {
        this.container = container;
        setNode( Network.newNode( this, Visibility.Network ).withComponent( "tesseract" ).withConnector(  ).create(  ) );
        connect = false;
    }

    @Override
    public boolean canUpdate(  ) {
        return true;
    }

    @Override
    public void update(  ) {
        if ( !connect && node(  ) != null ){
            tesseractList.put( node(  ).address(  ), this );
            connect = true;
        }
    }

    @Callback( doc="function( uuid:string, slot:number, count:number )" )
    public Object[] transferOfSlot( Context context, Arguments arguments ) throws Exception{
        String uuid = arguments.checkString( 0 );
        int slot = arguments.checkInteger( 1 ) + 4;
        int count = arguments.checkInteger( 2 );

        Machine machine = ( Machine ) context.node(  ).host(  );
        Robot robot = ( Robot ) machine.host(  );

        if ( slot < 0 || slot > robot.getSizeInventory(  ) )
            return new Object[]{false, "invalid slot"};

        if ( tesseractList.containsKey( uuid ) ){
            TesseractUpgrade tesseract = tesseractList.get( uuid );
            if ( tesseract.container == null )
                return new Object[]{false, "tesseract not sync."};

            Connector connector = ( Connector ) node(  );

            ItemStack stack = robot.getStackInSlot( slot );
            if ( stack != null && stack.stackSize >= count ) {
                double dist = Utils.distance( node(  ), tesseract.node(  ) );
                double energy = 4 * ( dist / 10 ) * dist * count;
                if ( tesseract.addToInventory( stack.splitStack( count ) ) ) {
                    if ( connector.tryChangeBuffer( -energy ) ) {
                        if ( stack.stackSize <= 0 )
                            robot.setInventorySlotContents( slot, null );

                        CommonProxy.wrapper.sendToDimension( new PacketTeleporter( ( int )tesseract.container.xPosition(  ), ( int )tesseract.container.yPosition(  ), ( int )tesseract.container.zPosition(  ) ), tesseract.container.world(  ).provider.dimensionId );
                        tesseract.container.world(  ).playSoundEffect( ( int )tesseract.container.xPosition(  ), ( int )tesseract.container.yPosition(  ), ( int )tesseract.container.zPosition(  ), "mob.endermen.portal", 1.0F, 1.0F );
                        return new Object[]{true};
                    }else{
                        return new Object[]{false, "not enough energy"};
                    }
                }else{
                    return new Object[]{false, "inventory full"};
                }
            }
        }else if ( TileEntityTeleporter.teleporterList.containsKey( uuid ) ){
            TileEntityTeleporter teleporter = TileEntityTeleporter.teleporterList.get( uuid );
            Connector connector = ( Connector ) node(  );

            ItemStack stack = robot.getStackInSlot( slot );
            if ( stack != null && stack.stackSize >= count ) {
                double dist = Utils.distance( robot.xPosition(  ), robot.yPosition(  ), robot.zPosition(  ), teleporter.xCoord, teleporter.yCoord, teleporter.zCoord );
                double energy = 4 * ( dist / 10 ) * dist * count;
                if ( teleporter.addToInventory( stack.splitStack( count ) ) ) {
                    if ( connector.tryChangeBuffer( -energy ) ) {
                        if ( stack.stackSize <= 0 )
                            robot.setInventorySlotContents( slot, null );

                        CommonProxy.wrapper.sendToDimension( new PacketTeleporter( teleporter.xCoord, teleporter.yCoord, teleporter.zCoord ), teleporter.getWorldObj(  ).provider.dimensionId );
                        teleporter.getWorldObj(  ).playSoundEffect( teleporter.xCoord, teleporter.yCoord, teleporter.zCoord, "mob.endermen.portal", 1.0F, 1.0F );
                        return new Object[]{true};
                    }else{
                        return new Object[]{false, "not enough energy"};
                    }
                }else{
                    return new Object[]{false, "inventory full"};
                }
            }
        }

        return new Object[]{false};
    }

    public boolean addToInventory( ItemStack stack ){
        if ( container != null ){
            return Utils.addToIInventory(((Agent) container).mainInventory(), 4, stack );
        }
        return false;
    }

    public EnvironmentHost getContainer(  ){
        return container;
    }
}
@@ -1777,17 +1777,17 @@
 ( 1 ) + 
-4
+3
 ;%0A      
@@ -1942,24 +1942,106 @@
 .host(  );%0A%0A
+        if (count %3C= 0)%0A            return new Object%5B%5D%7Bfalse, %22invalid count%22%7D;%0A%0A
         if (
@@ -2048,17 +2048,17 @@
  slot %3C 
-0
+4
  %7C%7C slot

fix dupe negative count.