#pragma once

#include <Magnum/Math/Matrix4.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/Math/Vector3.h>

namespace osim_viewer {

class OrbitCameraController {
public:
    void setViewport(const Magnum::Vector2i& windowSize, const Magnum::Vector2i& framebufferSize);
    void setSceneBounds(const Magnum::Vector3& center, float radius);
    void reset();

    void orbit(const Magnum::Vector2& deltaPixels);
    void pan(const Magnum::Vector2& deltaPixels);
    void zoom(float wheelDelta);

    [[nodiscard]] Magnum::Matrix4 cameraTransform() const;
    [[nodiscard]] Magnum::Vector3 position() const;
    [[nodiscard]] Magnum::Vector3 pivot() const { return _pivot; }

private:
    [[nodiscard]] float panScale() const;
    [[nodiscard]] Magnum::Vector3 forward() const;
    [[nodiscard]] Magnum::Vector3 right() const;
    [[nodiscard]] Magnum::Vector3 up() const;

    Magnum::Vector2i _windowSize{1280, 720};
    Magnum::Vector2i _framebufferSize{1280, 720};
    Magnum::Vector3 _pivot{0.0f};
    float _distance = 5.0f;
    float _yaw = 0.0f;
    float _pitch = 0.35f;
    float _sceneRadius = 1.0f;
};

}

