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

import speiger.src.coreengine.rendering.guiOld.GuiComponent;
import speiger.src.coreengine.rendering.guiOld.base.IButtonComponent;
import speiger.src.coreengine.rendering.guiOld.components.TextComponent;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.Constrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.Constrains;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.ParentConstrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.PixelConstrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.TextConstrain;

public class MenuItemComponent extends GuiComponent implements IButtonComponent
{
	public static final int FLAG_KEEP_MENU_OPEN = 1 << 20;
	
	TextComponent text = new TextComponent().singleLine(true).limit(false).setTextScale(0.35F).cast();
	Integer color = null;
	protected float boxWidth;
	
	public MenuItemComponent(String name)
	{
		this(0F, 0F, 0F, 0F, name);
	}
	
	public MenuItemComponent(float x, float y, float width, float height, String name)
	{
		super(x, y, width, height);
		text.setText(name);
		setFlag(FLAG_SUPPORT_BINDING);
	}
	
	protected void setTextScale(float scale)
	{
		text.setTextScale(0.35F * scale);
	}
	
	public TextComponent getText()
	{
		return text;
	}
	
	protected Constrain createWidthConstriain()
	{
		return TextConstrain.width(text).setPadding(2F);
	}
	
	public final MenuItemComponent setIsNotClosingMenu(boolean value)
	{
		setFlag(FLAG_KEEP_MENU_OPEN, value);
		return this;
	}
	
	public final boolean isNotClosingMenu()
	{
		return isFlagSet(FLAG_KEEP_MENU_OPEN);
	}
	
	public MenuItemComponent setCustomColor(int color)
	{
		this.color = color;
		return this;
	}
	
	protected void setMenuColor(int color)
	{
		if(this.color == null)
		{
			this.color = color;
		}
	}
	
	protected void setBoxWidth(float value)
	{
		boxWidth = value;
	}
	
	@Override
	public void init()
	{
		addChild(text, new Constrains(new PixelConstrain(0.25F), new ParentConstrain(), new ParentConstrain(), new ParentConstrain()));
	}
	
	@Override
	protected boolean isOverBox(int mouseX, int mouseY)
	{
		float minX = getBox().getMinX();
		float maxX = Math.max(getBox().getMaxX(), minX + boxWidth);
		return minX <= mouseX && maxX >= mouseX && getBox().getMinY() <= mouseY && getBox().getMaxY() >= mouseY;
	}
	
	@Override
	protected boolean renderSelf(int mouseX, int mouseY, float particalTicks)
	{
		float brigthness = getBrightness(mouseX, mouseY);
		float minX = getBox().getMinX(0.2F);
		float minY = getBox().getMinY(0.2F);
		float maxX = Math.max(getBox().getMaxX(), minX + boxWidth);
		float maxY = getBox().getMaxY(-0.2F);
		getRenderer().setBrightness(brigthness).drawQuad(minX, minY, maxX, maxY, color).setBrightness(brigthness * 0.5F).drawFrame(minX, minY, maxX, maxY, color);
		text.setBrightness(brigthness);
		return true;
	}
	
	@Override
	public boolean onClick(int button, int mouseX, int mouseY)
	{
		return true;
	}
	
	@Override
	public void onRelease(int button, int mouseX, int mouseY)
	{
		notifyListeners(LISTENER_USER_ACTION);
	}
	
	@Override
	protected boolean onUserKey()
	{
		notifyListeners(LISTENER_USER_ACTION);
		return true;
	}
	
	@Override
	public String toString()
	{
		return "Menu Item: "+getText().getText();
	}
}
