package speiger.src.coreengine.rendering.guiOld.helper.constrains;

import java.util.List;

import speiger.src.collections.floats.functions.FloatSupplier;
import speiger.src.coreengine.rendering.guiOld.GuiComponent;

public class MenuConstrain<T extends GuiComponent> extends Constrain
{
	public static final FloatSupplier DEFAULT = () -> 0F;
	List<T> components;
	FloatSupplier offset = DEFAULT;
	float padding = 0F;
	
	public MenuConstrain(List<T> components)
	{
		this.components = components;
	}
	
	public MenuConstrain(List<T> components, FloatSupplier offset)
	{
		this.components = components;
		this.offset = offset;
	}
	
	public MenuConstrain<T> setPadding(float padding)
	{
		this.padding = padding;
		return this;
	}
	
	@Override
	public void apply()
	{
		boolean vertical = target == Target.Y;
		if(!target.isPosition())
		{
			float max = 0F;
			for(int i = 0;i<components.size();i++)
			{
				max = Math.max(max, vertical ? components.get(i).getBox().getHeight() : components.get(i).getBox().getWidth());
			}
			set(max);
			return;
		}
		int index = components.indexOf(owner);
		if(index == -1)
		{
			throw new IllegalStateException("Not Found itself");
		}
		float pos = 0F;
		for(int i = 0;i<index;i++)
		{
			pos += vertical ? components.get(i).getBox().getHeight() : components.get(i).getBox().getWidth();
		}
		set((pos / getScale()) + offset.getAsFloat() + (padding * index));
	}
	
}
