package speiger.src.coreengine.application;

import speiger.src.coreengine.math.bits.FlagHolder;
import speiger.src.coreengine.rendering.guiOld.GuiManager;
import speiger.src.coreengine.rendering.guiOld.base.DebugOverlay;
import speiger.src.coreengine.rendering.guiOld.components.TextComponent;
import speiger.src.coreengine.rendering.models.buffers.UniformBuffer;
import speiger.src.coreengine.utils.profiler.EmptyProfiler;
import speiger.src.coreengine.utils.profiler.IProfiler;

public class BaseUIManager extends GuiManager
{
	Application application;
	
	public BaseUIManager(Application application)
	{
		super(application.mainWindow, application.eventBus, application.fonts);
		this.application = application;
	}
	
	public static BaseUIManager create(Application app)
	{
		return new BaseUIManager(app) {
			@Override
			public DebugOverlay createOverlay()
			{
				DebugOverlay overlay = app.createCustomDebug();
				return overlay != null ? overlay : new InternalDebugOverlay();
			}
		};
	}
	
	@Override
	public IProfiler getGPUProfiler()
	{
		return application.gpuProfiler;
	}

	@Override
	public IProfiler getCPUProfiler()
	{
		return application.clientProfiler;
	}

	@Override
	public IProfiler getServerProfiler()
	{
		return EmptyProfiler.INSTANCE;
	}

	@Override
	public UniformBuffer getOrthoMatrixBuffer()
	{
		return application.projectionBuffer.getOrthoViewMatrixBuffer();
	}

	@Override
	public DebugOverlay createOverlay()
	{
		DebugOverlay overlay = application.createCustomDebug();
		return overlay != null ? overlay : new InternalDebugOverlay();
	}
	
	public static class InternalDebugOverlay extends DebugOverlay
	{
		TextComponent text = new TextComponent(0, 0, 200, 100, "Testing string");
		public static final int UPDATING = 1;
		public static final int RENDERING = 2;
		FlagHolder flags = new FlagHolder(RENDERING);
		@Override
		public void toggleUI() { flags.flipFlag(RENDERING); }
		@Override
		public void toggleUpdate() { flags.flipFlag(UPDATING); }
		@Override
		public void toggleDebug() {}
		@Override
		public boolean toggleFPS() { return false; }
		@Override
		public boolean isUpdating() { return flags.isFlagSet(UPDATING); }
		@Override
		public boolean isRendering() { return flags.isFlagSet(RENDERING); }
		
		@Override
		public void onInit() {
			super.onInit();
			addComponent(text);
		}
		
		@Override
		protected void render(int mouseX, int mouseY, float particalTicks) {
			super.render(mouseX, mouseY, particalTicks);
		}
	}
}
