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

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

import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.rendering.guiOld.GuiBase;
import speiger.src.coreengine.rendering.guiOld.GuiComponent;

public abstract class BaseListEntry implements IListEntry
{
	protected List<GuiComponent> components = new ObjectArrayList<>();
	protected float maxWidth = -1F;
	protected float lastScale = 1F;
	protected <T extends GuiComponent> T addComponent(T comp)
	{
		components.add(comp);
		maxWidth = -1F;
		return comp;
	}
	
	@Override
	public float getWidth()
	{
		if(maxWidth < 0F)
		{
			float[] value = new float[]{Float.MAX_VALUE, Float.MAX_VALUE, Float.MIN_VALUE, Float.MIN_VALUE};
			for(int i = 0,m=components.size();i<m;components.get(i++).calculateActualBounds(value, false));
			maxWidth = Math.max(0F, value[2]+getWidthPadding());
		}
		return maxWidth;
	}
	
	public float getWidthPadding()
	{
		return 1F;
	}
	
	@Override
	public void init(GuiComponent comp, GuiBase owner)
	{
		for(int i = 0;i<components.size();i++)
		{
			components.get(i).setOwner(owner);
		}
	}
	
	@Override
	public void updateState(GuiComponent comp, float scale)
	{
		for(int i = 0;i<components.size();i++)
		{
			components.get(i).setScale(scale);
		}
		lastScale = scale;
		maxWidth = -1F;
	}
	
	@Override
	public void onClosed()
	{
		for(int i = 0;i<components.size();i++)
		{
			components.get(i).onClosed();
		}
		components.clear();
	}
	
	@Override
	public void onFixedUpdate()
	{
		for(int i = 0;i<components.size();i++)
		{
			components.get(i).fixedUpdate();
		}
	}
	
	@Override
	public void onRender(GuiComponent comp, boolean enabled, int mouseX, int mouseY, float particalTicks)
	{
		for(int i = 0;i<components.size();i++)
		{
			GuiComponent entry = components.get(i);
			if(entry.isVisible())
			{
				entry.setEnabled(enabled).render(mouseX, mouseY, particalTicks);
			}
		}		
	}

	@Override
	public void collectTooltips(GuiComponent comp, int mouseX, int mouseY, float particalTicks, Map<UUID, GuiComponent> collector)
	{
		for(int i = 0;i<components.size();i++)
		{
			components.get(i).collectTooltips(mouseX, mouseY, particalTicks, collector);
		}
	}
	
}
