package speiger.src.coreengine.rendering.guiOld.components.tree;

import java.util.Map;
import java.util.UUID;

import speiger.src.coreengine.rendering.guiOld.GuiComponent;
import speiger.src.coreengine.rendering.guiOld.base.IButtonComponent;

public class BaseClickableTreeEntry extends BaseTreeEntry implements IButtonComponent
{
	@Override
	public boolean isComponentColliding(int mouseX, int mouseY)
	{
		for(int i = 0,m=components.size();i<m;i++)
		{
			GuiComponent comp = components.get(i);
			if(comp instanceof IButtonComponent && ((IButtonComponent)comp).isComponentColliding(mouseX, mouseY)) return true;
		}
		return false;
	}
	
	@Override
	public boolean onClick(int button, int mouseX, int mouseY)
	{
		for(int i = 0,m=components.size();i<m;i++)
		{
			GuiComponent comp = components.get(i);
			if(!(comp instanceof IButtonComponent)) continue;
			IButtonComponent btn = (IButtonComponent)comp;
			if(btn.isComponentColliding(mouseX, mouseY) && btn.onClick(button, mouseX, mouseY)) return true;
		}
		return false;
	}
	
	@Override
	public boolean onDrag(int mouseX, int mouseY)
	{
		for(int i = 0,m=components.size();i<m;i++)
		{
			GuiComponent comp = components.get(i);
			if(comp instanceof IButtonComponent && ((IButtonComponent)comp).onDrag(mouseX, mouseY)) return true;
		}
		return false;
	}
	
	@Override
	public void onRelease(int button, int mouseX, int mouseY)
	{
		for(int i = 0,m=components.size();i<m;i++)
		{
			GuiComponent comp = components.get(i);
			if(comp instanceof IButtonComponent)
			{
				IButtonComponent btn = (IButtonComponent)comp;
				if(btn.isComponentColliding(mouseX, mouseY))
				{
					btn.onRelease(button, mouseX, mouseY);
					return;
				}
				btn.onFocusLost();
			}
		}
	}
	
	@Override
	public boolean onScroll(int scroll, int mouseX, int mouseY)
	{
		for(int i = 0,m=components.size();i<m;i++)
		{
			GuiComponent comp = components.get(i);
			if(!(comp instanceof IButtonComponent)) continue;
			IButtonComponent btn = (IButtonComponent)comp;
			if(btn.isComponentColliding(mouseX, mouseY) && btn.onScroll(scroll, mouseX, mouseY)) return true;
		}
		return false;
	}
	
	@Override
	public void collectTooltips(GuiComponent comp, int mouseX, int mouseY, float particalTicks, Map<UUID, GuiComponent> collector)
	{
		for(int i = 0,m=components.size();i<m;i++)
		{
			GuiComponent subComb = components.get(i);
			if(!(subComb instanceof IButtonComponent)) continue;
			subComb.collectTooltips(mouseX, mouseY, particalTicks, collector);
		}
	}
}