#include "engine.h"
#include "render.h"
#include "imguimanager.h"
#include <imgui_impl_sdl3.h>
#include <imgui_impl_engine.h>

ImGuiManager g_ImGuiManager;

void ImGuiManager::Init()
{
	IMGUI_CHECKVERSION();
	ImGui::CreateContext();
	ImGuiIO& io = ImGui::GetIO();
	(void)io;

	ImGui::StyleColorsDark();

	// Initialize backend

	ImGui_ImplSDL3_InitForOpenGL(GetEngine()->GetWindow(), g_render->GetGLContext());

	ImGui_ImplEngine_Init();

	m_ready = true;
}

void ImGuiManager::Shutdown()
{
	ImGui_ImplEngine_Shutdown();

	ImGui_ImplSDL3_Shutdown();

	ImGui::DestroyContext();
}

void ImGuiManager::BeginFrame()
{
	ImGui_ImplSDL3_NewFrame();

	ImGui_ImplEngine_NewFrame();

	ImGui::NewFrame();
}

void ImGuiManager::EndFrame()
{
	Draw();
}

void ImGuiManager::PollEvents(SDL_Event* _p_event)
{
	SDL_assert(_p_event);

	if (!m_ready)
		return;

	ImGui_ImplSDL3_ProcessEvent(_p_event);
}

void ImGuiManager::Draw()
{
	ImGui::Render();

	ImGui_ImplEngine_RenderDrawData(ImGui::GetDrawData());
}
