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

import speiger.src.coreengine.rendering.guiOld.components.misc.ICheckBox;
import speiger.src.coreengine.rendering.guiOld.helper.UIShapes;
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.DynamicConstrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.ParentConstrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.PixelConstrain;
import speiger.src.coreengine.rendering.guiOld.renderer.buffer.RenderBuffer;

public class MenuCheckBoxComponent extends MenuItemComponent implements ICheckBox<MenuCheckBoxComponent>
{
	public static final int FLAG_CHECKED = 1 << 21;
	RenderBuffer buffer;
	
	public MenuCheckBoxComponent(String name)
	{
		super(name);
		setFlag(FLAG_KEEP_MENU_OPEN | FLAG_SUPPORT_BINDING);
	}
	
	public MenuCheckBoxComponent(String name, boolean checked)
	{
		super(name);
		setFlag(FLAG_CHECKED, checked);
		setFlag(FLAG_KEEP_MENU_OPEN | FLAG_SUPPORT_BINDING);
	}

	public MenuCheckBoxComponent(float x, float y, float width, float height, String name)
	{
		super(x, y, width, height, name);
		setFlag(FLAG_KEEP_MENU_OPEN | FLAG_SUPPORT_BINDING);
	}
	
	public MenuCheckBoxComponent(float x, float y, float width, float height, String name, boolean checked)
	{
		super(x, y, width, height, name);
		setFlag(FLAG_CHECKED, checked);
		setFlag(FLAG_KEEP_MENU_OPEN | FLAG_SUPPORT_BINDING);
	}
	
	@Override
	public void init()
	{
		addChild(text, new Constrains(new PixelConstrain(getBox().getHeight() + 1F), new ParentConstrain(), new DynamicConstrain(() -> text.getMetadata().getMaxWidth() + 0.5F), new ParentConstrain()));
		onClose(buffer = getRenderer().createBuffer());
	}
	
	@Override
	protected void repaint()
	{
		float width = getBox().getHeight();
		float height = getBox().getHeight();
		buffer.clear();
		UIShapes.createCross(buffer, width, height, (width / 3.3F), (height / 5F), color);
		UIShapes.createCross(buffer, width, height, (width / 5F), (height / 10F), color);
	}
	
	@Override
	protected Constrain createWidthConstriain()
	{
		return new DynamicConstrain(() -> text.getMetadata().getMaxWidth() + getBox().getHeight() + 3F);
	}
		
	@Override
	public final boolean isChecked()
	{
		return isFlagSet(FLAG_CHECKED);
	}
	
	@Override
	public final MenuCheckBoxComponent setChecked(boolean value)
	{
		setFlag(FLAG_CHECKED, value);
		return this;
	}
	
	@Override
	public boolean renderSelf(int mouseX, int mouseY, float particalTicks)
	{
		float brightness = getActiveBrightness();
		float height = getBox().getHeight();
		float centerX = getBox().getMinX(height * 0.5F);
		float centerY = getBox().getMinY(height * 0.5F);
		getRenderer().setBrightness(brightness * 0.85F).drawQuad(getBox().getMinX(0.2F), getBox().getMinY(), getBox().getMinX(height + 0.2F), getBox().getMinY(height), color).setBrightness(brightness * 0.75F).drawFrame(getBox().getMinX(0.2F), getBox().getMinY(), getBox().getMinX(height + 0.2F), getBox().getMinY(height), color).translate(centerX, centerY);
		if(isChecked())
		{
			getRenderer().setBrightness(brightness * 0.7F).translate(0, 0, 0.001F).drawBuffers(buffer.selectionIterator(1), getBox().getWidth(), getBox().getHeight()).translate(0, 0, -0.001F);
		}
		if(isHovered(mouseX, mouseY))
		{
			getRenderer().setBrightness(brightness * 1.3F).translate(0, 0, 0.002F).drawBuffers(buffer.selectionIterator(0), getBox().getWidth(), getBox().getHeight()).translate(0, 0, -0.002F);
		}
		float maxX = Math.min(boxWidth, getBox().getMaxX());
		getRenderer().setBrightness(1F).translate(-centerX, -centerY).drawQuad(getBox().getMinX(height), getBox().getMinY(), getBox().getMinX(maxX), getBox().getMaxY(), color).setBrightness(brightness * 0.75F).drawFrame(getBox().getMinX(height), getBox().getMinY(), getBox().getMinX(maxX), getBox().getMaxY(), color);
		return true;
	}
	
	protected boolean isCheckBoxHovered(int mouseX, int mouseY)
	{
		float height = getBox().getHeight();
		return getBox().getMinX() <= mouseX && getBox().getMinX(height) >= mouseX && getBox().getMinY() <= mouseY && getBox().getMinY(height) >= mouseY && getGui().hasComponentInTheWay(this, mouseX, mouseY);
	}
	
	@Override
	public boolean onClick(int button, int mouseX, int mouseY)
	{
		return true;
	}
	
	@Override
	public void onRelease(int button, int mouseX, int mouseY)
	{
		setFlag(FLAG_CHECKED, !isFlagSet(FLAG_CHECKED));
		notifyListeners(LISTENER_USER_ACTION);
	}
	
	@Override
	protected boolean onUserKey()
	{
		setFlag(FLAG_CHECKED, !isFlagSet(FLAG_CHECKED));
		notifyListeners(LISTENER_USER_ACTION);
		return true;
	}
}
