package projectzombie.menu.gui;

import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec3d;
import projectzombie.Main;
import projectzombie.entity.EntityItem;
import projectzombie.model.Model;
import projectzombie.text.Text;
import projectzombie.util.math.ItemStack;
import projectzombie.world.layer.Layer;

public class GUIItemHolder implements GUIComponent
{
	private GUIItemSlot hover = null;
	private ItemStack holding = ItemStack.getEmpty();
	
	public void setHover(GUIItemSlot slot) {
		this.hover = slot;
	}

	@Override
	public void render(Matrix4 matrix, Vec2d mousePos, boolean canHover)
	{
		if(!holding.isEmpty())
		{
			Model model = holding.item.getModel(holding.meta).getGuiModel();
			matrix = Matrix4.multiply(Matrix4.translate(mousePos.x - 0.425, mousePos.y - 0.425, 0), matrix);
			
			model.setModel(matrix);
			model.render();
			
			if(holding.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("" + holding.count, text_matrix);
			}
		}
		
		hover = null;
	}

	@Override
	public void update(Vec2d mousePos) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean checkMouseHover(Vec2d pos) {
		return true;
	}
	
	@Override
	public void onRightClick(Vec2d pos)
	{
		// Drop part of the holding item if the hover item doesn't exist
		if(hover == null)
		{
			if(!holding.isEmpty())
			{
				Layer layer = Main.world.getLayer();
				
				layer.spawnEntity(new EntityItem(
						Main.player.getPos(), Main.player.getVelocity(),
						new ItemStack(holding.item, 1, holding.meta),
						Math.toRadians(Main.player.angle)));
				
				holding.count -= 1;
			}
			
			return;
		}
		
		ItemStack hover = this.hover.getItemStack();
		
		// Split the hovered slot in half if the holding slot is empty
		if(holding.isEmpty())
		{
			holding.item = hover.item;
			holding.meta = hover.meta;
			
			holding.count = (int)Math.ceil(hover.count / 2.0);
			hover.count = (int)Math.floor(hover.count / 2.0);
			
			this.hover.setItemStack(hover);
		}
		
		// Put 1 item into the slot if the hovered slot is empty or equal to the holding slot
		else if((hover.isEmpty() && this.hover.isItemAllowed(holding)) || hover.stackEquals(holding))
		{
			hover.count += 1;
			holding.count -= 1;
			
			hover.item = holding.item;
			hover.meta = holding.meta;
			
			this.hover.setItemStack(hover);
		}
		
		this.hover = null;
	}

	@Override
	public void onMouseClick(Vec2d pos)
	{
		// Drop the holding item if the hover item doesn't exist
		if(hover == null)
		{
			if(!holding.isEmpty())
			{
				Layer layer = Main.world.getLayer();
				
				layer.spawnEntity(new EntityItem(
						Main.player.getPos(), Main.player.getVelocity(),
						holding.copy(), Math.toRadians(Main.player.angle)));
				
				holding.count = 0;
			}
			
			return;
		}
		
		ItemStack hover = this.hover.getItemStack();
		
		// Are these the same item
		if(holding.stackEquals(hover) && !holding.isEmpty() && !hover.isEmpty())
		{
			if(this.hover.isItemAllowed(holding))
			{
				// Merge the hover stack into the holding stack
				if(holding.count + hover.count > holding.item.max) {
					int overflow = holding.count + hover.count - holding.item.max;
					hover.count = holding.item.max;
					holding.count = overflow;
				}
				
				else {
					hover.count += holding.count;
					holding.count = 0;
				}
				
				this.hover.setItemStack(hover);
			}
		}
		
		else if(holding.isEmpty() || this.hover.isItemAllowed(holding))
		{
			// Swap the stacks
			ItemStack stack = holding.copy();
			
			holding.count = hover.count;
			holding.item = hover.item;
			holding.meta = hover.meta;
			
			hover.count = stack.count;
			hover.item = stack.item;
			hover.meta = stack.meta;
			
			this.hover.setItemStack(hover);
		}
		
		this.hover = null;
	}

	@Override
	public void onActivate() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onBack()
	{
		if(!holding.isEmpty())
		{
			Layer layer = Main.world.getLayer();
			
			layer.spawnEntity(new EntityItem(
					Main.player.getPos(), Main.player.getVelocity(),
					holding.copy(), Math.toRadians(Main.player.angle)));
			
			holding.count = 0;
		}
	}
	
	@Override
	public void onScroll(Vec2d mousePos, double amount) {
	}

}
