#ifndef EDITSCENE_CURSOR3D_HPP
#define EDITSCENE_CURSOR3D_HPP
#pragma once

#include <Ogre.h>
#include <OgreManualObject.h>

// Forward declarations
struct TransformComponent;

/**
 * 3D Cursor - a visual marker for prefab placement and transform reference.
 * Supports axis-based translation and rotation interaction like the gizmo.
 */
class Cursor3D {
public:
	enum class Mode {
		Translate,
		Rotate
	};

	enum class Axis {
		None,
		X,
		Y,
		Z
	};

	Cursor3D(Ogre::SceneManager *sceneMgr);
	~Cursor3D();

	void shutdown();

	void setPosition(const Ogre::Vector3 &pos);
	Ogre::Vector3 getPosition() const { return m_position; }

	void setOrientation(const Ogre::Quaternion &rot);
	Ogre::Quaternion getOrientation() const { return m_orientation; }

	void setVisible(bool visible);
	bool isVisible() const;

	void setSize(float size);
	float getSize() const { return m_size; }

	void setMode(Mode mode) { m_mode = mode; }
	Mode getMode() const { return m_mode; }

	void snapToTransform(const TransformComponent &transform);
	void applyToTransform(TransformComponent &transform) const;

	/**
	 * Handle mouse input for cursor interaction.
	 * Returns true if cursor handled the input.
	 */
	bool onMousePressed(const Ogre::Ray &mouseRay,
			    const Ogre::Camera *camera);
	bool onMouseMoved(const Ogre::Ray &mouseRay,
			  const Ogre::Vector2 &mouseDelta);
	bool onMouseReleased();

	bool isDragging() const { return m_isDragging; }
	Axis getSelectedAxis() const { return m_selectedAxis; }

private:
	void createGeometry();
	void updateNodeTransform();
	Axis hitTest(const Ogre::Ray &mouseRay);

	Ogre::SceneManager *m_sceneMgr;
	Ogre::SceneNode *m_cursorNode;
	Ogre::ManualObject *m_axisX;
	Ogre::ManualObject *m_axisY;
	Ogre::ManualObject *m_axisZ;
	Ogre::ManualObject *m_centerMarker;

	Ogre::Vector3 m_position;
	Ogre::Quaternion m_orientation;
	float m_size;
	bool m_visible;

	Mode m_mode;
	Axis m_selectedAxis;
	Axis m_hoveredAxis;
	bool m_isDragging;

	// Drag state
	Ogre::Vector3 m_dragStartPosition;
	Ogre::Quaternion m_dragStartRotation;
	Ogre::Vector3 m_dragAxisDir;
	float m_dragStartT;
	float m_dragStartAngle;
	Ogre::Vector2 m_dragScreenAxis;
};

#endif // EDITSCENE_CURSOR3D_HPP
