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

import speiger.src.coreengine.math.color.ColorUtils;
import speiger.src.coreengine.rendering.guiOld.GuiComponent;
import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.ConditionalConstraint;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.Constrains;

public class ScrollPanelComponent extends GuiComponent
{
	public static final int FLAG_CORNER = 1 << 20;
	
	ScrollBarComponent horizontalBar = new ScrollBarComponent(ColorUtils.LIGHT_GRAY).setHorizontal(true);
	ScrollBarComponent verticalBar = new ScrollBarComponent(ColorUtils.LIGHT_GRAY);
	protected PanelComponent container = new PanelComponent().setManualRenderer(true).setScissorsTest(true).cast();
	PanelComponent viewPort = new PanelComponent().setManualRenderer(true).cast();
	
	public ScrollPanelComponent()
	{
		super(0F, 0F, 0F, 0F);
	}
	
	public ScrollPanelComponent(float x, float y, float width, float height)
	{
		super(x, y, width, height);
	}
	
	@Override
	public void init()
	{
		addChild(horizontalBar, Constrains.scrollBar(isFlagSet(FLAG_CORNER) ? () -> true : verticalBar::isInUse, true, 5F));
		addChild(verticalBar, Constrains.scrollBar(isFlagSet(FLAG_CORNER) ? () -> true : horizontalBar::isInUse, false, 5F));
		viewPort.addChild(container);
		container.onChange(this::recalculateSize);
		addChild(viewPort, Constrains.width(ConditionalConstraint.parent(verticalBar::isInUse, 0F, 5F)).height(ConditionalConstraint.parent(horizontalBar::isInUse, 0F, 5F)).build());
	}
	
	public ScrollPanelComponent setForcedCorner(boolean value)
	{
		if(setFlag(FLAG_CORNER, value) && getGui() != null)
		{
			addConstrains(horizontalBar, Constrains.scrollBar(isFlagSet(FLAG_CORNER) ? () -> true : verticalBar::isInUse, true, 5F));
			addConstrains(verticalBar, Constrains.scrollBar(isFlagSet(FLAG_CORNER) ? () -> true : horizontalBar::isInUse, false, 5F));
			onChanged(false);
		}
		return this;
	}
	
	public PanelComponent getContainer()
	{
		return container;
	}
	
	public PanelComponent getViewPort()
	{
		return viewPort;
	}
	
	protected void recalculateSize(GuiComponent owner)
	{
		float maxX = 0F;
		float maxY = 0F;
		for(GuiComponent component : container.getChildren())
		{
			IGuiBox box = component.getBox();
			maxX = Math.max(maxX, box.getBaseX() + box.getBaseWidth());
			maxY = Math.max(maxY, box.getBaseY() + box.getBaseHeight());
		}
		horizontalBar.setScrollMax((int)maxX);
		verticalBar.setScrollMax((int)maxY);
		owner.bounds(maxX, maxY).set(-horizontalBar.getScroll(), -verticalBar.getScroll());
	}
	
	@Override
	protected boolean updateSelf(int mouseX, int mouseY, float particalTicks)
	{
		super.updateSelf(mouseX, mouseY, particalTicks);
		container.set(-horizontalBar.getScroll(), -verticalBar.getScroll());
		return true;
	}
	
	@Override
	protected boolean renderSelf(int mouseX, int mouseY, float particalTicks)
	{
		renderChildren(mouseX, mouseY, particalTicks);
		if(container.isVisible())
		{
			enableScissors(viewPort.getBox());
			getRenderer().translate(0F, 0F, 0.1F);
			container.render(mouseX, mouseY, particalTicks);
			getRenderer().translate(0F, 0F, -0.1F);
			disableScissors();
		}
		return false;
	}
}
