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

import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.coreengine.math.color.ColorUtils;
import speiger.src.coreengine.rendering.guiOld.components.ButtonComponent;
import speiger.src.coreengine.rendering.guiOld.components.TextComponent;
import speiger.src.coreengine.rendering.guiOld.components.WindowComponent;
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.RelativeConstrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.TextConstrain;

public class ChoiceComponent extends WindowComponent
{
	TextComponent message = new TextComponent().limitHeight(false).setTextScale(0.5F);
	ButtonComponent yesButton = new ButtonComponent("Yes", ColorUtils.GRAY);
	ButtonComponent noButton = new ButtonComponent("No", ColorUtils.GRAY);
	BooleanConsumer listener;
	
	public ChoiceComponent(float width, String windowTitle, String message, BooleanConsumer listener)
	{
		this(0F, 0F, width, windowTitle, message, listener);
	}
	
	public ChoiceComponent(float x, float y, float width, String windowTitle, String message, BooleanConsumer listener)
	{
		super(x, y, width, 25F, FIXED_SIZE_POPUP, windowTitle);
		this.message.setText(message);
		this.listener = listener;
	}
	
	public static ChoiceComponent createChoice(float width, String windowTitle, String message, BooleanConsumer listener)
	{
		return new ChoiceComponent(width, windowTitle, message, listener);
	}
	
	public static ChoiceComponent createChoice(String windowTitle, String message, BooleanConsumer listener)
	{
		return new ChoiceComponent(150, windowTitle, message, listener);
	}
	
	@Override
	public void init()
	{
		super.init();
		yesButton.getText().setTextScale(0.5F);
		noButton.getText().setTextScale(0.5F);
		addChild(yesButton.onChange(minimizedListener).onAction(closeListener).onAction(_ -> listener.accept(true)), new Constrains(new RelativeConstrain(0F), new ParentConstrain(10F).invert(), new RelativeConstrain(0.5F), new PixelConstrain(10F)));
		addChild(noButton.onChange(minimizedListener).onAction(closeListener).onAction(_ -> listener.accept(false)), new Constrains(new RelativeConstrain(0.5F), new ParentConstrain(10F).invert(), new RelativeConstrain(0.5F), new PixelConstrain(10F)));
		addChild(message, new Constrains(new PixelConstrain(10F), new PixelConstrain(11F), new ParentConstrain(10F), TextConstrain.height(message)));
		getBox().setHeight(25F + message.getMetadata().getMaxHeight());
	}
	
	@Override
	public boolean isPopup()
	{
		return true;
	}
}
