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

import speiger.src.coreengine.rendering.gui.GuiBase;
import speiger.src.coreengine.rendering.gui.GuiComponent;
import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox;

public abstract class Constrain
{
	protected ConstrainTarget target;
	protected GuiComponent owner;
	protected GuiComponent parent;
	
	public void setComponents(GuiComponent owner, GuiComponent parent, ConstrainTarget target)
	{
		this.owner = owner;
		this.parent = parent;
		this.target = target;
	}
	
	public abstract void apply();
	
	protected final void set(float value)
	{
		target.set(owner.getBox(), value);
	}
	
	protected final float get(ConstrainTarget target)
	{
		return parent != null ? target.get(parent.getBox()) : target.get(owner.getGui());
	}
	
	protected final float getOwner(ConstrainTarget target)
	{
		return target.get(owner.getBox());
	}
	
	protected final float getScale()
	{
		return parent != null ? parent.getBox().getScale() : 1F;
	}
	
	public static enum ConstrainTarget
	{
		X_POS,
		Y_POS,
		WIDTH,
		HEIGHT;
		
		public boolean isPosition()
		{
			return this == X_POS || this == Y_POS;
		}
		
		public ConstrainTarget toWidth()
		{
			switch(this)
			{
				case X_POS: return WIDTH;
				case Y_POS: return HEIGHT;
				default: return this;
			}
		}
		
		public float get(GuiBase gui)
		{
			switch(this)
			{
				case X_POS: return 0F;
				case Y_POS: return 0F;
				case WIDTH: return gui.width;
				case HEIGHT: return gui.height;
				default: return 0F;
			}
		}
		
		public float get(IGuiBox box)
		{
			switch(this)
			{
				case X_POS: return box.getMinX();
				case Y_POS: return box.getMinY();
				case WIDTH: return box.getBaseWidth();
				case HEIGHT: return box.getBaseHeight();
				default: return 0F;
			}
		}
		
		public void set(IGuiBox box, float value)
		{
			switch(this)
			{
				case X_POS:
					box.setX(value);
					break;
				case Y_POS:
					box.setY(value);
					break;
				case WIDTH:
					box.setWidth(value);
					break;
				case HEIGHT:
					box.setHeight(value);
					break;
			}
		}
	}
}
