/*
 * Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
 */

#include <imgui.h>

#include <studio/imguiutil.hpp>
#include "about.hpp"

namespace olympic {
extern ox::String appVersion;
}

namespace studio {

AboutPopup::AboutPopup(turbine::Context &ctx) noexcept:
	Popup("About"),
#ifdef DEBUG
	m_text{sfmt("{} [DEBUG] - {}", keelCtx(ctx).appName, olympic::appVersion)} {
#else
	m_text{sfmt("{} - {}", keelCtx(ctx).appName, olympic::appVersion)} {
#endif
}

void AboutPopup::draw(Context&) noexcept {
	if (ImGui::IsKeyPressed(ImGuiKey_Escape)) {
		close();
		return;
	}
	switch (m_stage) {
		case Stage::Closed:
			break;
		case Stage::Opening:
			ImGui::OpenPopup("About");
			m_stage = Stage::Open;
			[[fallthrough]];
		case Stage::Open: {
			constexpr auto modalFlags =
					ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize;
			ig::centerNextWindow();
			auto open = true;
			if (ImGui::BeginPopupModal("About", &open, modalFlags)) {
				ImGui::Text("%s\n\nBuild date: %s", m_text.c_str(), __DATE__);
				ImGui::NewLine();
				ig::Dummy({148.0f, 0.0f});
				ImGui::SameLine();
				if (ig::PushButton("Close")) {
					ImGui::CloseCurrentPopup();
					open = false;
				}
				ImGui::EndPopup();
			}
			if (!open) {
				m_stage = Stage::Closed;
			}
			break;
		}
	}
}

}
