package projectzombie.menu.gui;

import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec2d;
import projectzombie.init.Models;
import projectzombie.inventory.Crafting;
import projectzombie.inventory.Inventory;
import projectzombie.inventory.recipe.Recipe;
import projectzombie.model.ModelGui;
import projectzombie.util.ItemStack;

public class GUIRecipeCard implements GUIContainer
{
	private static final ModelGui LABEL = Models.UI_LABEL_RECIPE; 

	private Vec2d pos;
	private Recipe recipe;
	private GUIContainer gui;
	private GUIItemSlot result_display_slot;
	private GUIItemSlot result_slot;
	private GUIItemSlot[] ingredients;
	private Inventory inventory;
	private Crafting tool;
	
	public GUIRecipeCard(Inventory inventory, GUIContainer gui, Recipe recipe, Crafting tool, Vec2d pos)
	{
		this.gui = gui;
		this.recipe = recipe;
		this.pos = pos;
		this.inventory = inventory;
		this.tool = tool;
		
		result_slot = new GUIItemSlot(1.5, true, new GUIItemSlotGetter()
		{
			@Override
			public void setItemStack(ItemStack stack) {
			}
			
			@Override
			public boolean isAllowed(ItemStack stack) {
				return false;
			}
			
			@Override
			public ItemStack getItemStack() {
				return (recipe.canCraft(tool) &&
						recipe.hasResourcesToCraft(inventory)) ?
						recipe.getResult().copy() :
						ItemStack.getEmpty();
			}
			
			@Override
			public boolean mustTakeAll() {
				return true;
			}
			
			@Override
			public void onRemoveItemStack() {
				if(recipe.canCraft(tool) && recipe.hasResourcesToCraft(inventory)) {
					recipe.craftResult(inventory);
				}
			}
		});
		
		result_slot.setPos(pos.add(new Vec2d(
				Models.UI_LABEL_RECIPE.getWidth() * 103 / 128.0 + 0.25,
				Models.UI_LABEL_RECIPE.getHeight() * 9 / 32.0 + 0.25)));
		
		result_display_slot = new GUIItemSlotReadonly(1, true, recipe.getResult());
		
		result_display_slot.setPos(pos.add(new Vec2d(
				Models.UI_LABEL_RECIPE.getWidth() * 87 / 128.0 + 0.25,
				Models.UI_LABEL_RECIPE.getHeight() * 9 / 32.0 + 0.25)));
		
		ItemStack[] ingredients = recipe.getIngredients();
		this.ingredients = new GUIItemSlot[ingredients.length];
		
		for(int i=0;i<ingredients.length;i++)
		{
			ItemStack ingredient = ingredients[i];
			GUIItemSlot ingredientDisplay = new GUIItemSlotReadonly(1, true, ingredient);
			
			ingredientDisplay.setPos(pos.add(new Vec2d(
				Models.UI_LABEL_RECIPE.getWidth() * (27 + 16 * i) / 128.0 + 0.25,
				Models.UI_LABEL_RECIPE.getHeight() * 9 / 32.0 + 0.25)));
			
			this.ingredients[i] = ingredientDisplay;
		}
	}
	
	@Override
	public void render(Matrix4 matrix, Vec2d mousePos, boolean canHover)
	{
		LABEL.setModel(Matrix4.multiply(matrix, Matrix4.translate(pos.x, pos.y, 0)));
		LABEL.render();
		
		for(GUIComponent c : allComponents()) {
			c.render(matrix, mousePos, canHover);
		}
	}
	
	private GUIComponent[] allComponents()
	{
		GUIComponent[] components = new GUIComponent[ingredients.length + 2];
		
		components[0] = result_slot;
		components[1] = result_display_slot;
		
		for(int i=0;i<ingredients.length;i++) {
			components[i+2] = ingredients[i];
		}
		
		return components;
	}

	@Override
	public void update(Vec2d mousePos) {
		for(GUIComponent c : allComponents()) {
			c.update(mousePos);
		}
	}

	@Override
	public boolean checkMouseHover(Vec2d mousePos) {
		for(GUIComponent c : allComponents()) {
			if(c.checkMouseHover(mousePos)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void onScroll(Vec2d mousePos, double amount) {
		for(GUIComponent c : allComponents()) {
			c.onScroll(mousePos, amount);
		}
	}

	@Override
	public void onRightClick(Vec2d mousePos) {
		for(GUIComponent c : allComponents()) {
			c.onRightClick(mousePos);
		}
	}

	@Override
	public void onMouseClick(Vec2d mousePos) {
		for(GUIComponent c : allComponents()) {
			c.onMouseClick(mousePos);
		}
	}

	@Override
	public void onActivate() {
		for(GUIComponent c : allComponents()) {
			c.onActivate();
		}
	}

	@Override
	public void onBack() {
		for(GUIComponent c : allComponents()) {
			c.onBack();
		}
	}

	@Override
	public GUIItemSlot getHoveringItemSlot(Vec2d mousePos)
	{
		for(GUIComponent c : allComponents()) {
			if(c.checkMouseHover(mousePos) && c instanceof GUIItemSlot) {
				return (GUIItemSlot)c;
			}
		}
		
		return null;
	}

	@Override
	public void add(GUIComponent c) {
		
	}

	@Override
	public void clear() {
		
	}

	

}
