#include "ItemEditor.hpp"
#include "../systems/ItemRegistry.hpp"
#include <imgui.h>

bool ItemEditor::renderComponent(flecs::entity entity, ItemComponent &item)
{
	(void)entity;
	bool modified = false;
	ImGui::PushID("Item");

	ImGui::Text("Item Reference");
	ImGui::Separator();

	// Item ID
	char idBuf[256];
	strncpy(idBuf, item.itemId.c_str(), sizeof(idBuf));
	idBuf[sizeof(idBuf) - 1] = '\0';
	if (ImGui::InputText("Item ID", idBuf, sizeof(idBuf))) {
		item.itemId = idBuf;
		modified = true;
	}

	// Pick from registry dropdown
	ItemRegistry *registry = ItemRegistry::getSingletonPtr();
	if (registry && !registry->getDefinitions().empty()) {
		static int selectedRegItem = -1;
		std::vector<const char *> names;
		std::vector<std::string> ids;
		int currentIdx = -1;
		int idx = 0;
		for (const auto &pair : registry->getDefinitions()) {
			names.push_back(pair.second.itemName.c_str());
			ids.push_back(pair.second.itemId);
			if (pair.second.itemId == item.itemId)
				currentIdx = idx;
			idx++;
		}
		if (currentIdx >= 0)
			selectedRegItem = currentIdx;
		if (selectedRegItem >= (int)names.size())
			selectedRegItem = 0;
		ImGui::SameLine();
		if (ImGui::Combo("##itemIdSelect", &selectedRegItem,
				 names.data(), (int)names.size())) {
			item.itemId = ids[selectedRegItem];
			modified = true;
		}
	}

	// Registry info display
	if (!item.itemId.empty() && registry) {
		auto *def = registry->findDefinition(item.itemId);
		if (def) {
			ImGui::Text("Name: %s", def->itemName.c_str());
			ImGui::Text("Type: %s", def->itemType.c_str());
			ImGui::Text("Max Stack: %d", def->maxStackSize);
			ImGui::Text("Weight: %.2f", def->weight);
			ImGui::Text("Value: %d", def->value);
			if (!def->useActionName.empty())
				ImGui::Text("Use Action: %s", def->useActionName.c_str());
			if (def->unique)
				ImGui::TextColored(ImVec4(1, 0.5f, 0, 1), "UNIQUE ITEM");
		} else {
			ImGui::TextColored(ImVec4(1, 0, 0, 1),
					   "Unknown item ID: %s", item.itemId.c_str());
		}
	}

	ImGui::Separator();

	// Stack size
	int stackSize = item.stackSize;
	if (ImGui::DragInt("Stack Size", &stackSize, 1, 1, 999)) {
		if (stackSize < 1)
			stackSize = 1;
		item.stackSize = stackSize;
		modified = true;
	}

	// Action (GOAP action name executed on interact)
	char actionBuf[256];
	strncpy(actionBuf, item.action.c_str(), sizeof(actionBuf));
	actionBuf[sizeof(actionBuf) - 1] = '\0';
	if (ImGui::InputText("Action", actionBuf, sizeof(actionBuf))) {
		item.action = actionBuf;
		modified = true;
	}
	ImGui::SameLine();
	ImGui::TextDisabled("(?)");
	if (ImGui::IsItemHovered()) {
		ImGui::SetTooltip(
			"Optional GOAP action name.\n"
			"If set, pressing E executes this action instead of picking up.");
	}

	// Instance ID (for global state tracking)
	char instanceBuf[256];
	strncpy(instanceBuf, item.instanceId.c_str(), sizeof(instanceBuf));
	instanceBuf[sizeof(instanceBuf) - 1] = '\0';
	if (ImGui::InputText("Instance ID", instanceBuf, sizeof(instanceBuf))) {
		item.instanceId = instanceBuf;
		modified = true;
	}
	ImGui::SameLine();
	ImGui::TextDisabled("(?)");
	if (ImGui::IsItemHovered()) {
		ImGui::SetTooltip(
			"Unique ID for save/load tracking.\n"
			"If set, disabled state persists across sessions.");
	}

	// Disabled state
	if (ImGui::Checkbox("Disabled", &item.disabled)) {
		modified = true;
	}
	if (ImGui::IsItemHovered()) {
		ImGui::SetTooltip(
			"Item has been picked up / consumed.\n"
			"Disabled items are hidden in game mode.");
	}

	ImGui::PopID();
	return modified;
}
