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

import java.util.List;

import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.rendering.guiOld.GuiComponent;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.Constrains;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.MenuConstrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.ParentConstrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.PixelConstrain;

public class MenuBarComponent extends GuiComponent
{
	int color;
	List<MenuItemComponent> menuItems = new ObjectArrayList<MenuItemComponent>();
	float scale = 1F;
	
	public MenuBarComponent(int color)
	{
		super(0F, 0F, 0F, 0F);
		this.color = color;
	}
	
	public MenuBarComponent(float x, float y, float width, float height, int color)
	{
		super(x, y, width, height);
		this.color = color;
	}
	
	@Override
	public void init()
	{
	}
	
	public MenuBarComponent setTextScale(float scale)
	{
		if(this.scale != scale)
		{
			this.scale = scale;
			for(int i = 0,m=menuItems.size();i<m;i++)
			{
				menuItems.get(i).setTextScale(scale);
			}
		}
		return this;
	}
	
	public MenuItemComponent addMenuItem(String name)
	{
		return addMenuItem(new MenuItemComponent(name));
	}
	
	public MenuItemComponent addText(String name)
	{
		return addMenuItem(new MenuTextComponent(name)).cast();
	}
	
	public MenuCheckBoxComponent addCheckBox(String name)
	{
		return addMenuItem(new MenuCheckBoxComponent(name)).cast();
	}
	
	public MenuCheckBoxComponent addCheckBox(String name, boolean defaultValue)
	{
		return addMenuItem(new MenuCheckBoxComponent(name, defaultValue)).cast();
	}
	
	public MenuComponent addMenu(String name)
	{
		return addMenuItem(new MenuComponent(name)).onAction(this::closeMenus).cast();
	}
	
	public MenuItemComponent addMenuItem(MenuItemComponent item)
	{
		menuItems.add(item);
		item.setMenuColor(color);
		addChild(item, new Constrains(new MenuConstrain<MenuItemComponent>(menuItems), new PixelConstrain(), item.createWidthConstriain(), new ParentConstrain()));
		item.setZOffset(0.3F);
		item.setTextScale(scale);
		item.onChanged(false);
		return item;
	}
	
	@Override
	protected boolean renderSelf(int mouseX, int mouseY, float particalTicks)
	{
		getRenderer().drawQuad(getBox(), color);
		return true;
	}
	
	protected void closeMenus(GuiComponent owner)
	{
		for(int i = 0,m=menuItems.size();i<m;i++)
		{
			MenuItemComponent comp = menuItems.get(i);
			if(comp != owner && comp instanceof MenuComponent)
			{
				((MenuComponent)comp).setOpen(false);
			}
		}
	}
}
