package projectzombie.menu.gui;

import org.lwjgl.opengl.GL33;

import gl_engine.MathHelpers;
import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec3d;
import projectzombie.Main;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.model.ModelItem;
import projectzombie.text.Text;
import projectzombie.util.ItemStack;

public class GUIItemSlot implements GUIComponent
{
	GUIItemSlotGetter getter;
	private Vec2d pos = new Vec2d(0, 0);
	private double hitboxSize;
	private boolean renderItem;
	private Model model_empty = Models.ITEM_EMPTY.getGuiModel();
	
	public GUIItemSlot(double hitboxSize, boolean renderItem, GUIItemSlotGetter getter) {
		this.renderItem = renderItem;
		this.hitboxSize = hitboxSize;
		this.getter = getter;
	}
	
	public void setPos(Vec2d pos) {
		this.pos = pos;
	}
	
	public Vec2d getPos() {
		return pos;
	}
	
	public boolean isItemAllowed(ItemStack stack) {
		return getter.isAllowed(stack);
	}
	
	public void setItemStack(ItemStack stack) {
		getter.setItemStack(stack);
	}
	
	public ItemStack getItemStack() {
		return getter.getItemStack();
	}
	
	public void setModelEmpty(ModelItem model) {
		model_empty = model.getGuiModel();
	}

	@Override
	public void render(Matrix4 matrix, Vec2d mousePos, boolean canHover)
	{
		ItemStack stack = getter.getItemStack();
		boolean hover = checkMouseHover(mousePos) && canHover;
		
		matrix = Matrix4.multiply(Matrix4.translate(pos.x, pos.y, 0), matrix);
		Model model = stack.item.getModel(stack).getGuiModel();
		
		if(getter.renderSlot())
		{
			Models.UI_ITEM_SLOT_STORAGE.setModel(Matrix4.multiply(matrix, Matrix4.translate(-0.325, -0.325, 0)));
			Models.UI_ITEM_SLOT_STORAGE.render();
		}
		
		if(stack.isEmpty()) {
			model = model_empty;
		}
		
		if(renderItem)
		{
			model.setModel(matrix);
			model.render();
		}
		
		if(!getter.isReadOnly() && hover)
		{
			double offset = (0.85 - hitboxSize) / 2;
			Matrix4 hover_matrix = Matrix4.multiply(matrix, Matrix4.translate(offset, offset, 0));
			hover_matrix = Matrix4.multiply(Matrix4.scale(new Vec3d(hitboxSize, hitboxSize, hitboxSize)), hover_matrix);
			
			Models.UI_ITEM_HOVER.setModel(hover_matrix);
			Models.UI_ITEM_HOVER.render();
		}
		
		if(renderItem && !stack.isEmpty())
		{
			double damage = stack.item.getDamage(stack);
			
			if(damage > 0)
			{
				Matrix4 matrix_damage = Matrix4.multiply(matrix, Matrix4.translate(0, -0.85 / 16, 0));
				matrix_damage = Matrix4.multiply(Matrix4.scale(new Vec3d(damage * 0.85, 0.85 / 16, 0)), matrix_damage);
				
				GL33.glUniform4f(Main.window.glsl_color,
						1 - (float)MathHelpers.squared(1 - damage),
						1 - (float)MathHelpers.squared(damage), 0, 1);
				
				Models.UI_PIXEL_WHITE.setModel(matrix_damage);
				Models.UI_PIXEL_WHITE.render();
				
				GL33.glUniform4f(Main.window.glsl_color, 1, 1, 1, 1);
			}
			
			if(stack.count > 1)
			{
				Matrix4 text_matrix = Matrix4.multiply(matrix, Matrix4.translate(-0.75 / 4, -0.75 / 4, 0));
				text_matrix = Matrix4.multiply(Matrix4.scale(new Vec3d(0.4, 0.4, 0.4)), text_matrix);
				
				Text.render("" + stack.count, text_matrix);
			}
		}
	}

	@Override
	public boolean checkMouseHover(Vec2d pos)
	{
		double offset = (0.85 - hitboxSize) / 2;
		
		double px = this.pos.x + offset;
		double py = this.pos.y + offset;
		
		return pos.x > px && pos.x < px + hitboxSize && pos.y > py && pos.y < py + hitboxSize;
	}

	@Override
	public void onActivate() {
	}

	@Override
	public void onBack() {
	}
	
	@Override
	public void update(Vec2d mousePos) {
	}
	
	@Override
	public void onScroll(Vec2d mousePos, double amount) {
	}

	@Override
	public void onRightClick(Vec2d mousePos) {
	}

	@Override
	public void onMouseClick(Vec2d mousePos) {
	}
	
}
