package zmaster587.advancedRocketry.tile;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import zmaster587.advancedRocketry.Inventory.modules.IModularInventory;
import zmaster587.advancedRocketry.Inventory.modules.ModuleBase;
import zmaster587.advancedRocketry.Inventory.modules.ModuleLiquidIndicator;
import zmaster587.advancedRocketry.Inventory.modules.ModulePower;
import zmaster587.advancedRocketry.api.AdvancedRocketryFluids;
import zmaster587.advancedRocketry.api.armor.ItemSpaceArmor;
import zmaster587.libVulpes.tile.TileInventoriedRFConsumerTank;

public class TileOxygenCharger extends TileInventoriedRFConsumerTank implements IModularInventory {
	public TileOxygenCharger() {
		super(1000, 2, ItemSpaceArmor.getMaxAir());
	}

	@Override
	public int[] getAccessibleSlotsFromSide(int slots) {
		return new int[] {};
	}

	@Override
	public boolean isItemValidForSlot(int slot, ItemStack stack) {
		return false;
	}

	@Override
	public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
		if(resource.getFluid() == AdvancedRocketryFluids.fluidOxygen)
			return super.fill(from, resource, doFill);
		return 0;
	}

	@Override
	public boolean canFill(ForgeDirection from, Fluid fluid) {
		return fluid == AdvancedRocketryFluids.fluidOxygen;
	}	

	@Override
	public int getPowerPerOperation() {
		return 1;
	}
	
	@Override
	public boolean canPerformFunction() {
		if(!worldObj.isRemote) {
			for( Object player : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(this.xCoord, this.yCoord, this.zCoord, this.xCoord + 1, this.yCoord + 2, this.zCoord + 1))) {
				ItemStack stack = ((EntityPlayer)player).getEquipmentInSlot(3);

				if(stack != null && stack.getItem() instanceof ItemSpaceArmor) {
					FluidStack fluidStack = this.drain(ForgeDirection.UNKNOWN, 1, false);
					
					
					
					if(((ItemSpaceArmor)stack.getItem()).getAirRemaining(stack) < ((ItemSpaceArmor)stack.getItem()).getMaxAir() &&
							fluidStack != null && fluidStack.getFluid() == AdvancedRocketryFluids.fluidOxygen && fluidStack.amount > 0)  {
						this.drain(ForgeDirection.UNKNOWN, 1, true);
						return true;
					}
					return false;
				}
			}
		}
		return false;
	}

	@Override
	public void performFunction() {
		for( Object player : this.worldObj.getEntitiesWithinAABB(EntityPlayer.class, AxisAlignedBB.getBoundingBox(this.xCoord, this.yCoord, this.zCoord, this.xCoord + 1, this.yCoord + 2, this.zCoord + 1))) {
			ItemStack stack = ((EntityPlayer)player).getEquipmentInSlot(3);

			if(stack != null && stack.getItem() instanceof ItemSpaceArmor) {
				((ItemSpaceArmor)stack.getItem()).increment(stack, 100);
			}
		}
	}

	@Override
	public List<ModuleBase> getModules() {
		ArrayList<ModuleBase> modules = new ArrayList<ModuleBase>();

		modules.add(new ModulePower(18, 20, this));
		modules.add(new ModuleLiquidIndicator(32, 20, this));
		//modules.add(toggleSwitch = new ModuleToggleSwitch(160, 5, 0, "", this, TextureResources.buttonToggleImage, 11, 26, getMachineEnabled()));
		//TODO add itemStack slots for liqiuid
		return modules;
	}

	@Override
	public String getModularInventoryName() {
		return "tile.oxygenCharger.name";
	}

	@Override
	public boolean canInteractWithContainer(EntityPlayer entity) {
		return true;
	}
}

@@ -309,16 +309,64 @@
 .Fluid;%0A
+import net.minecraftforge.fluids.FluidRegistry;%0A
 import n
@@ -398,24 +398,24 @@
 FluidStack;%0A
-
 import zmast
@@ -1341,36 +1341,69 @@
 ource.getFluid()
- == 
+.getID() == FluidRegistry.getFluidID(
 AdvancedRocketry
@@ -1421,16 +1421,17 @@
 dOxygen)
+)
 %0A%09%09%09retu
@@ -1570,20 +1570,53 @@
 rn fluid
- == 
+.getID() == FluidRegistry.getFluidID(
 Advanced
@@ -1641,16 +1641,17 @@
 idOxygen
+)
 ;%0A%09%7D%09%0A%0A%09
@@ -2391,16 +2391,24 @@
 tFluid()
+.getID()
  == Adva
@@ -2437,16 +2437,24 @@
 idOxygen
+.getID()
  && flui

Fix Oxygen from other mods not working with space suit equipment