#include "CharacterIdentityEditor.hpp"
#include "ComponentRegistration.hpp"
#include "../systems/CharacterRegistry.hpp"
#include <imgui.h>

bool CharacterIdentityEditor::renderComponent(flecs::entity entity,
					      CharacterIdentityComponent &identity)
{
	(void)entity;
	bool modified = false;
	ImGui::PushID("CharacterIdentity");

	ImGui::Text("Registry ID: %llu", (unsigned long long)identity.registryId);

	static uint64_t selectedId = 0;
	static bool showPicker = false;

	if (identity.registryId != 0) {
		if (ImGui::Button("Clear")) {
			identity.registryId = 0;
			modified = true;
		}
		ImGui::SameLine();
	}
	if (ImGui::Button("Select from Registry")) {
		showPicker = !showPicker;
		selectedId = identity.registryId;
	}

	if (showPicker) {
		ImGui::BeginChild("RegistryPicker", ImVec2(0, 150), true);
		/* We don't have direct access to the registry here; the picker
		 * is a convenience that would need to be wired up if we want
		 * a live list.  For now, allow typing the ID directly. */
		static char idBuf[32] = "";
		if (ImGui::InputText("Registry ID", idBuf, sizeof(idBuf),
				     ImGuiInputTextFlags_CharsDecimal)) {
			identity.registryId = strtoull(idBuf, nullptr, 10);
			modified = true;
		}
		ImGui::EndChild();
	}

	ImGui::PopID();
	return modified;
}

