package frogcraftrebirth.common.tile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import frogcraftrebirth.api.FrogAPI;
import frogcraftrebirth.api.recipes.IAdvChemRecRecipe;
import frogcraftrebirth.common.lib.tile.TileFrogMachine;
import frogcraftrebirth.common.lib.util.ItemUtil;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.oredict.OreDictionary;

public class TileAdvChemReactor extends TileFrogMachine {
	public int process, processMax;
	boolean working, changed = false, inputsLocked = false, outputLocked = false;
	private IAdvChemRecRecipe recipe;
	public TileAdvChemReactor() {
		super(13, "TileEntityAdvancedChemicalReactor", 2, 100000);
		//0 for module, 1-5 for input, 6-10 for output, 11 for cell input and 12 for cell output
	}
	
	@Override
	public void readFromNBT(NBTTagCompound tag) {
		super.readFromNBT(tag);
		this.working = tag.getBoolean("working");
		this.process = tag.getInteger("process");
		this.processMax = tag.getInteger("processMax");
	}
	
	@Override
	public void writeToNBT(NBTTagCompound tag) {
		super.writeToNBT(tag);
		tag.setBoolean("working", this.working);
		tag.setInteger("process", this.process);
		tag.setInteger("processMax", this.processMax);
	}
	
	@Override
	public void updateEntity() {
		super.updateEntity();
		if (worldObj.isRemote) 
			return;
		
		recipe = (IAdvChemRecRecipe)FrogAPI.managerACR.<ItemStack[]>getRecipe(Arrays.copyOfRange(inv, 1, 5));
		
		if (recipe == null)
			return;
		
		//Map<String, Integer> recipeInput = recipe.getInputs();
		
		working = canWork(recipe);
		
		if (working) {
			working = canWork(recipe);
		} else 
			return;
		
		if (working && charge >= recipe.getEnergyRate()) {
			this.charge -= recipe.getEnergyRate();
			++process;
		}
		
		if (process == processMax) {
			Map<String, Integer> recipeOutput = recipe.getOutputs();
			ArrayList<ItemStack> product = new ArrayList<ItemStack>();
			//Overhaul required
			for (Entry<String, Integer> ore : recipeOutput.entrySet()) {
				ItemStack stack = OreDictionary.getOres(ore.getKey()).get(0);
				stack.stackSize = ore.getValue();
				product.add(stack);
			}
			
			this.changed = true;
			this.working = false;
		}
		
		if (changed) {
			this.markDirty();
			this.sendTileUpdatePacket(this);
			changed = false;
		}
	}
	
	//TODO: COMPLETE THIS METHOD
	public boolean canWork(IAdvChemRecRecipe recipe) {
		return false;
	}

	@Override
	public int[] getAccessibleSlotsFromSide(int side) {
		switch (side) {
			case 0: //Bottom
				return new int[] {6,7,8,9,10,12};
			case 1: //Top
				return new int[] {1,2,3,4,5,11};
			default: //Disallow auto-insert module
				return null;
		}
	}
	
	@Override
	public boolean canInsertItem(int slot, ItemStack item, int side) {
		if (side != 1)
			return false;
		return Arrays.asList(1,2,3,4,5,11).contains(slot);
	}

	@Override
	public boolean canExtractItem(int slot, ItemStack item, int side) {
		if (side != 0)
			return false;
		return Arrays.asList(6,7,8,9,10,12).contains(slot);
	}

}

@@ -89,31 +89,8 @@
 ys;%0A
-import java.util.List;%0A
 impo
@@ -107,16 +107,16 @@
 il.Map;%0A
+
 import j
@@ -134,16 +134,38 @@
 p.Entry;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -311,58 +311,8 @@
 ne;%0A
-import frogcraftrebirth.common.lib.util.ItemUtil;%0A
 impo
@@ -1493,44 +1493,8 @@
 %0A%09%09%0A
-%09%09if (recipe == null)%0A%09%09%09return;%0A%09%09%0A
 %09%09//
@@ -1607,34 +1607,8 @@
 %0A%09%09%09
-working = canWork(recipe);
 %0A%09%09%7D
@@ -2284,38 +2284,8 @@
 %7D%0A%09%0A
-%09//TODO: COMPLETE THIS METHOD%0A
 %09pub
@@ -2332,16 +2332,442 @@
 cipe) %7B%0A
+%09%09if (recipe == null)%0A%09%09%09return false;%0A%09%09%0A%09%09//TODO%0A%09%09Set%3CEntry%3CString, Integer%3E%3E currentInputs = null;%0A%09%09%0A%09%09for (Entry%3CString, Integer%3E item : recipe.getInputs().entrySet()) %7B%0A%09%09%09boolean checkPass = false;%0A%09%09%09for (Entry%3CString, Integer%3E input : currentInputs) %7B%0A%09%09%09%09if (item.equals(input)) %7B%0A%09%09%09%09%09checkPass = true;%0A%09%09%09%09%09break;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09if (checkPass)%0A%09%09%09%09continue;%0A%09%09%09else return false; //means failed on checking%0A%09%09%7D%0A%09%09%0A
 %09%09return

Inputs check, unfinished