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

import speiger.src.coreengine.rendering.guiOld.GuiComponent;
import speiger.src.coreengine.rendering.guiOld.base.IButtonComponent;
import speiger.src.coreengine.rendering.guiOld.components.misc.ICheckBox;
import speiger.src.coreengine.rendering.guiOld.helper.UIShapes;
import speiger.src.coreengine.rendering.guiOld.renderer.buffer.RenderBuffer;

public class CheckBoxComponent extends GuiComponent implements IButtonComponent, ICheckBox<CheckBoxComponent>
{
	boolean isChecked = false;
	int color;
	RenderBuffer buffer;
	
	public CheckBoxComponent(int color)
	{
		super(0F, 0F, 0F, 0F);
		setFlag(FLAG_SUPPORT_BINDING);
		this.color = color;
	}
	
	public CheckBoxComponent(int color, boolean checked)
	{
		super(0F, 0F, 0F, 0F);
		setFlag(FLAG_SUPPORT_BINDING);
		this.color = color;
	}
	
	public CheckBoxComponent(float x, float y, float width, float height, int color)
	{
		super(x, y, width, height);
		setFlag(FLAG_SUPPORT_BINDING);
		this.color = color;
	}
	
	public CheckBoxComponent(float x, float y, float width, float height, int color, boolean checked)
	{
		super(x, y, width, height);
		setFlag(FLAG_SUPPORT_BINDING);
		this.color = color;
		isChecked = checked;
	}
	
	@Override
	public void init()
	{
		onClose(buffer = getRenderer().createBuffer());
	}
	
	@Override
	public final CheckBoxComponent setChecked(boolean isChecked)
	{
		this.isChecked = isChecked;
		return this;
	}
	
	public final CheckBoxComponent setColor(int color)
	{
		if(this.color != color)
		{
			this.color = color;
			onChanged(true);
		}
		return this;
	}
	
	@Override
	public boolean isChecked()
	{
		return isChecked;
	}
	
	@Override
	protected void repaint()
	{
		float width = getBox().getWidth();
		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
	public boolean renderSelf(int mouseX, int mouseY, float particalTicks)
	{
		float brightness = getActiveBrightness();
		float centerX = getBox().getCenterX();
		float centerY = getBox().getCenterY();
		getRenderer().setBrightness(brightness).drawQuad(getBox(), color).setBrightness(brightness * 0.8F).drawFrame(getBox(), 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(isTopHovered(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);
		}
		getRenderer().setBrightness(1F).translate(-centerX, -centerY);
		return true;
	}
	
	@Override
	public boolean onClick(int button, int mouseX, int mouseY)
	{
		return true;
	}
	
	@Override
	public void onRelease(int button, int mouseX, int mouseY)
	{
		isChecked = !isChecked;
		notifyListeners(LISTENER_USER_ACTION);
	}
	
	@Override
	protected boolean onUserKey()
	{
		isChecked = !isChecked;
		notifyListeners(LISTENER_USER_ACTION);
		return true;
	}
}
