#include "CameraEditor.hpp"
#include <imgui.h>
#include <OgreTextureManager.h>
#include <OgreRenderTexture.h>
#include <OgreViewport.h>
#include <OgreLogManager.h>
#include <OgreRoot.h>
#include <OgreRenderSystem.h>

CameraEditor::CameraEditor(Ogre::SceneManager* sceneMgr, Ogre::RenderWindow* renderWindow)
	: m_sceneMgr(sceneMgr)
	, m_renderWindow(renderWindow)
{
}

bool CameraEditor::renderComponent(flecs::entity entity, CameraComponent &camera)
{
	bool modified = false;

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

		// Projection type
		bool ortho = camera.orthographic;
		if (ImGui::Checkbox("Orthographic", &camera.orthographic)) {
			camera.markDirty();
			modified = true;
		}

		if (camera.orthographic) {
			// Orthographic settings
			if (ImGui::DragFloat("Ortho Width", &camera.orthoWidth, 0.1f, 0.1f, 1000.0f)) {
				camera.markDirty();
				modified = true;
			}
			if (ImGui::DragFloat("Ortho Height", &camera.orthoHeight, 0.1f, 0.1f, 1000.0f)) {
				camera.markDirty();
				modified = true;
			}
		} else {
			// Perspective settings
			if (ImGui::SliderFloat("FOV Y", &camera.fovY, 10.0f, 120.0f, "%.1f deg")) {
				camera.markDirty();
				modified = true;
			}
		}

		// Clip distances
		ImGui::Separator();
		if (ImGui::DragFloat("Near Clip", &camera.nearClip, 0.01f, 0.001f, 10.0f)) {
			camera.markDirty();
			modified = true;
		}
		if (ImGui::DragFloat("Far Clip", &camera.farClip, 1.0f, 10.0f, 10000.0f)) {
			camera.markDirty();
			modified = true;
		}

		// Aspect ratio (auto or manual)
		ImGui::Separator();
		static bool autoAspect = true;
		ImGui::Checkbox("Auto Aspect Ratio", &autoAspect);
		if (!autoAspect) {
			if (ImGui::DragFloat("Aspect Ratio", &camera.aspectRatio, 0.01f, 0.1f, 10.0f)) {
				camera.markDirty();
				modified = true;
			}
		}

		// Preview section - allows switching viewport to this camera
		ImGui::Separator();
		if (camera.camera) {
			ImGui::Text("Camera Preview:");
			
			// Check if this camera is currently active in viewport
			bool isActive = isCameraActiveInViewport(&camera);
			
			if (isActive) {
				if (ImGui::Button("Stop Preview (Restore Editor Camera)")) {
					restoreEditorCamera();
				}
				ImGui::TextColored(ImVec4(0, 1, 0, 1), "Preview Active - main viewport is using this camera");
			} else {
				if (ImGui::Button("Preview This Camera")) {
					activateCamera(&camera);
				}
				ImGui::Text("Click to temporarily switch main viewport to this camera");
			}
		} else {
			ImGui::TextDisabled("Camera not yet created - save/load scene to initialize");
		}

		ImGui::Unindent();
	}

	return modified;
}

bool CameraEditor::isCameraActiveInViewport(CameraComponent* camera)
{
	if (!camera || !camera->camera || !m_renderWindow) return false;
	
	// Get the main viewport's camera from the stored render window
	if (m_renderWindow->getNumViewports() == 0) return false;
	
	Ogre::Viewport* viewport = m_renderWindow->getViewport(0);
	return viewport->getCamera() == camera->camera;
}

void CameraEditor::activateCamera(CameraComponent* camera)
{
	if (!camera || !camera->camera || !m_renderWindow) return;
	
	if (m_renderWindow->getNumViewports() == 0) return;
	
	Ogre::Viewport* viewport = m_renderWindow->getViewport(0);
	
	// Store original camera if not already stored
	if (!m_originalCamera) {
		m_originalCamera = viewport->getCamera();
	}
	
	// Set the new camera
	viewport->setCamera(camera->camera);
	
	Ogre::LogManager::getSingleton().logMessage(
		"CameraEditor: Switched viewport to camera '" + 
		camera->camera->getName() + "'");
}

void CameraEditor::restoreEditorCamera()
{
	if (!m_originalCamera || !m_renderWindow) return;
	
	if (m_renderWindow->getNumViewports() == 0) return;
	
	Ogre::Viewport* viewport = m_renderWindow->getViewport(0);
	viewport->setCamera(m_originalCamera);
	
	Ogre::LogManager::getSingleton().logMessage(
		"CameraEditor: Restored original camera '" + 
		m_originalCamera->getName() + "'");
	
	// Don't clear m_originalCamera - we might want to toggle back and forth
}
