#include "CharacterClassOverrideEditor.hpp"
#include "../components/CharacterClassDatabase.hpp"
#include <imgui.h>

bool CharacterClassOverrideEditor::renderComponent(
	flecs::entity entity, CharacterClassOverrideComponent &comp)
{
	(void)entity;
	auto &db = CharacterClassDatabase::getSingleton();

	// --- Stat Offsets ---
	ImGui::Text("Stat Offsets");
	if (!comp.statOffsets.empty()) {
		for (auto it = comp.statOffsets.begin();
		     it != comp.statOffsets.end();) {
			ImGui::PushID(it->first.c_str());
			int val = it->second;
			ImGui::InputInt(it->first.c_str(), &val, 1, 5);
			it->second = val;
			ImGui::SameLine();
			if (ImGui::Button("Remove"))
				it = comp.statOffsets.erase(it);
			else
				++it;
			ImGui::PopID();
		}
	} else {
		ImGui::TextDisabled("No stat offsets.");
	}

	// Add stat offset
	{
		const auto &names = db.getStatNames();
		if (!names.empty()) {
			static int selected = 0;
			static int valBuf = 0;
			ImGui::PushID("add_stat");
			if (selected >= (int)names.size())
				selected = 0;
			ImGui::Combo("Stat", &selected,
				     [](void *data, int idx) -> const char * {
					     const auto *n = static_cast<const std::vector<
						     Ogre::String> *>(data);
					     if (idx < 0 ||
						 idx >= (int)n->size())
						     return nullptr;
					     return (*n)[idx].c_str();
				     },
				     (void *)&names, (int)names.size());
			ImGui::SameLine();
			ImGui::InputInt("Offset", &valBuf, 1, 5);
			ImGui::SameLine();
			if (ImGui::Button("Add")) {
				comp.statOffsets[names[selected]] = valBuf;
				valBuf = 0;
			}
			ImGui::PopID();
		}
	}

	ImGui::Separator();

	// --- Skill Offsets ---
	ImGui::Text("Skill Offsets");
	if (!comp.skillOffsets.empty()) {
		for (auto it = comp.skillOffsets.begin();
		     it != comp.skillOffsets.end();) {
			ImGui::PushID(it->first.c_str());
			int val = it->second;
			ImGui::InputInt(it->first.c_str(), &val, 1, 5);
			it->second = val;
			ImGui::SameLine();
			if (ImGui::Button("Remove"))
				it = comp.skillOffsets.erase(it);
			else
				++it;
			ImGui::PopID();
		}
	} else {
		ImGui::TextDisabled("No skill offsets.");
	}

	// Add skill offset
	{
		const auto &names = db.getSkillNames();
		if (!names.empty()) {
			static int selected = 0;
			static int valBuf = 0;
			ImGui::PushID("add_skill");
			if (selected >= (int)names.size())
				selected = 0;
			ImGui::Combo("Skill", &selected,
				     [](void *data, int idx) -> const char * {
					     const auto *n = static_cast<const std::vector<
						     Ogre::String> *>(data);
					     if (idx < 0 ||
						 idx >= (int)n->size())
						     return nullptr;
					     return (*n)[idx].c_str();
				     },
				     (void *)&names, (int)names.size());
			ImGui::SameLine();
			ImGui::InputInt("Offset", &valBuf, 1, 5);
			ImGui::SameLine();
			if (ImGui::Button("Add")) {
				comp.skillOffsets[names[selected]] = valBuf;
				valBuf = 0;
			}
			ImGui::PopID();
		}
	}

	ImGui::Separator();

	// --- Need Offsets ---
	ImGui::Text("Need Offsets");
	if (!comp.needOffsets.empty()) {
		for (auto it = comp.needOffsets.begin();
		     it != comp.needOffsets.end();) {
			ImGui::PushID(it->first.c_str());
			int val = it->second;
			ImGui::InputInt(it->first.c_str(), &val, 1, 5);
			it->second = val;
			ImGui::SameLine();
			if (ImGui::Button("Remove"))
				it = comp.needOffsets.erase(it);
			else
				++it;
			ImGui::PopID();
		}
	} else {
		ImGui::TextDisabled("No need offsets.");
	}

	// Add need offset
	{
		const auto &names = db.getNeedNames();
		if (!names.empty()) {
			static int selected = 0;
			static int valBuf = 0;
			ImGui::PushID("add_need");
			if (selected >= (int)names.size())
				selected = 0;
			ImGui::Combo("Need", &selected,
				     [](void *data, int idx) -> const char * {
					     const auto *n = static_cast<const std::vector<
						     Ogre::String> *>(data);
					     if (idx < 0 ||
						 idx >= (int)n->size())
						     return nullptr;
					     return (*n)[idx].c_str();
				     },
				     (void *)&names, (int)names.size());
			ImGui::SameLine();
			ImGui::InputInt("Offset", &valBuf, 1, 5);
			ImGui::SameLine();
			if (ImGui::Button("Add")) {
				comp.needOffsets[names[selected]] = valBuf;
				valBuf = 0;
			}
			ImGui::PopID();
		}
	}

	return false;
}
