#ifndef EDITSCENE_GIZMO_HPP
#define EDITSCENE_GIZMO_HPP
#pragma once

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

/**
 * 3D Gizmo for editing entity transforms
 * Shows X/Y/Z axes and allows mouse interaction for translation
 */
class Gizmo {
public:
	enum class Mode {
		Translate,
		Rotate,
		Scale
	};

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

	Gizmo(Ogre::SceneManager *sceneMgr);
	~Gizmo();
	
	/**
	 * Shutdown and cleanup - must be called before SceneManager is destroyed
	 */
	void shutdown();

	/**
	 * Attach to an entity's transform
	 */
	void attachTo(flecs::entity entity);

	/**
	 * Detach from current entity
	 */
	void detach();

	/**
	 * Update gizmo position/rotation to match attached entity
	 */
	void update();

	/**
	 * Check if gizmo is attached to an entity
	 */
	bool isAttached() const { return m_attachedEntity.is_alive(); }

	/**
	 * Get attached entity
	 */
	flecs::entity getAttachedEntity() const { return m_attachedEntity; }

	/**
	 * Set gizmo mode (translate/rotate/scale)
	 */
	void setMode(Mode mode);
	Mode getMode() const { return m_mode; }

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

	/**
	 * Show/hide gizmo
	 */
	void setVisible(bool visible);
	bool isVisible() const;

	/**
	 * Set gizmo size/scale
	 */
	void setSize(float size) { m_size = size; createGizmoGeometry(); }
	float getSize() const { return m_size; }

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

	Ogre::SceneManager *m_sceneMgr;
	Ogre::SceneNode *m_gizmoNode;
	Ogre::ManualObject *m_axisX;
	Ogre::ManualObject *m_axisY;
	Ogre::ManualObject *m_axisZ;

	flecs::entity m_attachedEntity;
	Mode m_mode;
	Axis m_selectedAxis;
	Axis m_hoveredAxis;

	float m_size;
	float m_axisLength;
	bool m_isDragging;

	// Drag state - stored at drag start and reused during drag
	Ogre::Vector3 m_dragStartPosition;
	Ogre::Vector3 m_dragAxisDir;
	float m_dragStartT;
};

#endif // EDITSCENE_GIZMO_HPP
