#include "TransformEditor.hpp"
#include <imgui.h>
#include <cmath>

bool TransformEditor::renderComponent(flecs::entity entity,
				      TransformComponent &transform)
{
	bool modified = false;

	if (ImGui::CollapsingHeader("Transform",
				    ImGuiTreeNodeFlags_DefaultOpen)) {
		ImGui::Indent();

		// Position
		float pos[3] = { transform.position.x, transform.position.y,
			       transform.position.z };
		if (ImGui::DragFloat3("Position", pos, 0.1f)) {
			transform.position =
				Ogre::Vector3(pos[0], pos[1], pos[2]);
			modified = true;
		}

		// Rotation (using Ogre's getYaw/Pitch/Roll)
		float yaw = Ogre::Radian(transform.rotation.getYaw()).valueDegrees();
		float pitch = Ogre::Radian(transform.rotation.getPitch()).valueDegrees();
		float roll = Ogre::Radian(transform.rotation.getRoll()).valueDegrees();
		float rot[3] = { yaw, pitch, roll };
		if (ImGui::DragFloat3("Rotation (Y/P/R)", rot, 0.5f)) {
			Ogre::Quaternion q1(Ogre::Degree(rot[0]), Ogre::Vector3::UNIT_Y);
			Ogre::Quaternion q2(Ogre::Degree(rot[1]), Ogre::Vector3::UNIT_X);
			Ogre::Quaternion q3(Ogre::Degree(rot[2]), Ogre::Vector3::UNIT_Z);
			transform.rotation = q1 * q2 * q3;
			modified = true;
		}

		// Scale
		float scale[3] = { transform.scale.x, transform.scale.y,
				   transform.scale.z };
		if (ImGui::DragFloat3("Scale", scale, 0.01f)) {
			transform.scale =
				Ogre::Vector3(scale[0], scale[1], scale[2]);
			modified = true;
		}

		// Rotation buttons (Y axis)
		ImGui::Text("Rotate Y:");
		ImGui::SameLine();
		if (ImGui::Button("-90##rot")) {
			Ogre::Quaternion q(Ogre::Degree(-90.0f), Ogre::Vector3::UNIT_Y);
			transform.rotation = q * transform.rotation;
			modified = true;
		}
		ImGui::SameLine();
		if (ImGui::Button("-45##rot")) {
			Ogre::Quaternion q(Ogre::Degree(-45.0f), Ogre::Vector3::UNIT_Y);
			transform.rotation = q * transform.rotation;
			modified = true;
		}
		ImGui::SameLine();
		if (ImGui::Button("+45##rot")) {
			Ogre::Quaternion q(Ogre::Degree(45.0f), Ogre::Vector3::UNIT_Y);
			transform.rotation = q * transform.rotation;
			modified = true;
		}
		ImGui::SameLine();
		if (ImGui::Button("+90##rot")) {
			Ogre::Quaternion q(Ogre::Degree(90.0f), Ogre::Vector3::UNIT_Y);
			transform.rotation = q * transform.rotation;
			modified = true;
		}

		// Grid snap buttons
		ImGui::Text("Snap to Grid:");
		ImGui::SameLine();
		if (ImGui::Button("0.25")) {
			transform.position.x = std::round(transform.position.x / 0.25f) * 0.25f;
			transform.position.y = std::round(transform.position.y / 0.25f) * 0.25f;
			transform.position.z = std::round(transform.position.z / 0.25f) * 0.25f;
			modified = true;
		}
		ImGui::SameLine();
		if (ImGui::Button("0.5")) {
			transform.position.x = std::round(transform.position.x / 0.5f) * 0.5f;
			transform.position.y = std::round(transform.position.y / 0.5f) * 0.5f;
			transform.position.z = std::round(transform.position.z / 0.5f) * 0.5f;
			modified = true;
		}
		ImGui::SameLine();
		if (ImGui::Button("1.0")) {
			transform.position.x = std::round(transform.position.x);
			transform.position.y = std::round(transform.position.y);
			transform.position.z = std::round(transform.position.z);
			modified = true;
		}
		ImGui::SameLine();
		if (ImGui::Button("2.0")) {
			transform.position.x = std::round(transform.position.x / 2.0f) * 2.0f;
			transform.position.y = std::round(transform.position.y / 2.0f) * 2.0f;
			transform.position.z = std::round(transform.position.z / 2.0f) * 2.0f;
			modified = true;
		}
		ImGui::SameLine();
		if (ImGui::Button("4.0")) {
			transform.position.x = std::round(transform.position.x / 4.0f) * 4.0f;
			transform.position.y = std::round(transform.position.y / 4.0f) * 4.0f;
			transform.position.z = std::round(transform.position.z / 4.0f) * 4.0f;
			modified = true;
		}

		// Reset buttons
		if (ImGui::Button("Reset Position")) {
			transform.position = Ogre::Vector3::ZERO;
			modified = true;
		}
		ImGui::SameLine();
		if (ImGui::Button("Reset Rotation")) {
			transform.rotation = Ogre::Quaternion::IDENTITY;
			modified = true;
		}
		ImGui::SameLine();
		if (ImGui::Button("Reset Scale")) {
			transform.scale = Ogre::Vector3::UNIT_SCALE;
			modified = true;
		}

		// Apply changes to scene node
		if (modified && transform.node) {
			transform.applyToNode();
			transform.markChanged();
		}

		ImGui::Unindent();
	}

	return modified;
}
