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

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.TextFieldComponent;
import speiger.src.coreengine.rendering.guiOld.components.WindowComponent;
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 TextInputComponent extends WindowComponent
{
	TextComponent message = new TextComponent().limitHeight(false).setTextScale(0.5F);
	TextFieldComponent input = new TextFieldComponent(ColorUtils.GRAY).setCanLoseFocus(false).setInfiniteText(true).setMaxTextLength(Integer.MAX_VALUE).setFocused(true);
	ButtonComponent confirm = new ButtonComponent("Confirm", ColorUtils.DARK_GREEN);
	ButtonComponent cancel = new ButtonComponent("Cancel", ColorUtils.RED);
	
	public TextInputComponent(float width, String name, String message)
	{
		this(0F, 0F, width, name, message);
	}
	
	public TextInputComponent(float x, float y, float width, String name, String message)
	{
		super(x, y, width, 35F, FIXED_SIZE_POPUP, name);
		this.message.setText(message);
	}
	
	public TextFieldComponent getInput()
	{
		return input;
	}
	
	public String getText()
	{
		return input.getText();
	}
	
	@Override
	public void init()
	{
		super.init();
		confirm.getText().setTextScale(0.5F);
		cancel.getText().setTextScale(0.5F);
		input.getRawText().setTextScale(0.5F);
		addChild(confirm.onChange(minimizedListener).onAction(this::onListen), new RelativeConstrain(0F), new ParentConstrain(10F).invert(), new RelativeConstrain(0.5F), new PixelConstrain(10F));
		addChild(cancel.onChange(minimizedListener).onAction(closeListener), new RelativeConstrain(0.5F), new ParentConstrain(10F).invert(), new RelativeConstrain(0.5F), new PixelConstrain(10F));
		addChild(message.onChange(minimizedListener), new PixelConstrain(10F), new PixelConstrain(11F), new ParentConstrain(10F), TextConstrain.height(message));
		addChild(input.onChange(minimizedListener).onAction(this::onListen), new PixelConstrain(10F), TextConstrain.height(message).setPadding(15F), new ParentConstrain(10F), new PixelConstrain(12F));
		getBox().setHeight(45F + message.getMetadata().getMaxHeight());
	}
	
	private void onListen()
	{
		notifyListeners(LISTENER_USER_ACTION);
		closeListener.accept(this);
	}
	
	@Override
	public boolean isPopup()
	{
		return true;
	}
}
