#ifndef EDITSCENE_EDITORCAMERA_HPP
#define EDITSCENE_EDITORCAMERA_HPP
#pragma once
#include <memory>
#include <Ogre.h>
#include <OgreCamera.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreInput.h>
#include <OgreCameraMan.h>

/**
 * Editor camera controller using OgreBites::CameraMan
 * Supports orbit (default) and FPS modes
 */
class EditorCamera {
public:
	EditorCamera(Ogre::SceneManager *sceneMgr, Ogre::RenderWindow *window);
	~EditorCamera();

	/**
	 * Update camera (called each frame)
	 */
	void update(float deltaTime);

	/**
	 * Handle input events
	 */
	void handleMouseMove(const OgreBites::MouseMotionEvent &evt);
	void handleMousePress(const OgreBites::MouseButtonEvent &evt);
	void handleMouseRelease(const OgreBites::MouseButtonEvent &evt);
	void handleKeyboard(const OgreBites::KeyboardEvent &evt);

	/**
	 * Get the camera
	 */
	Ogre::Camera *getCamera() const
	{
		return m_camera;
	}

	/**
	 * Focus camera on a point
	 */
	void focusOn(const Ogre::Vector3 &point);

	/**
	 * Set camera position
	 */
	void setPosition(const Ogre::Vector3 &pos);

	/**
	 * Get camera position
	 */
	Ogre::Vector3 getPosition() const
	{
		return m_position;
	}

	/**
	 * Get ray from mouse position
	 */
	Ogre::Ray getMouseRay(float screenX, float screenY) const;

	/**
	 * Check if in FPS mode
	 */
	bool isFPSMode() const
	{
		return m_fpsMode;
	}

private:
	void updateCameraPosition();
	void updateFPSMovement(float deltaTime);

	Ogre::SceneManager *m_sceneMgr;
	Ogre::Camera *m_camera;
	Ogre::SceneNode *m_cameraNode;
	Ogre::SceneNode *m_targetNode;

	// Use OgreBites::CameraMan for proper camera control
	std::unique_ptr<OgreBites::CameraMan> m_cameraMan;

	// Camera state
	Ogre::Vector3 m_position;
	Ogre::Vector3 m_target;
	float m_distance;
	float m_yaw;
	float m_pitch;

	// Input state
	bool m_rotating;
	bool m_panning;
	bool m_fpsMode;
	int m_lastMouseX;
	int m_lastMouseY;

	// Keyboard state for FPS movement
	bool m_keyW;
	bool m_keyS;
	bool m_keyA;
	bool m_keyD;
	bool m_keyQ;
	bool m_keyE;

	// Movement speeds
	static constexpr float ROTATION_SPEED = 0.3f;
	static constexpr float PAN_SPEED = 0.01f;
	static constexpr float FPS_SPEED = 10.0f;
};

#endif // EDITSCENE_EDITORCAMERA_HPP
