#ifndef SCROLL_PANEL_HPP_
#define SCROLL_PANEL_HPP_

#include <Color/Color.hpp>
#include <Widget/Layout/Layout.hpp>

/**
 * @brief A vertically scrollable panel containing a widget.
 *
 * A scroll panel has a specific "natural height" and a child widget. Its minimum height
 * will be set to no more than the natural height, or the minimum height of its child. The
 * scroll panel will accept more height if given it. If the child widget does not fit within
 * the allocated space, a scrollbar will be shown.
 */
class ScrollPanel final : public Layout
{
private:
    int naturalHeight;
    int &scrollPos;
    int scrollPosInternal = 0;
    
    std::optional<int> mousePivotY;
    int scrollPivot;
    glm::vec4 backgroundColor = Color::scrollPanelBackground;

    static constexpr int margin = 0;
    static constexpr int scrollbarWidth = 4;
    static constexpr int scrollSpeed = 12;

public:
    /**
     * @brief Construct a scroll panel.
     * 
     * @param naturalHeight The natural height (see class description).
     * @param child The child widget.
     */
    ScrollPanel(int naturalHeight, Widget *child);

    /**
     * @brief Construct a scroll panel.
     * 
     * @param scrollPos Reference to the variable holding the scroll position.
     * @param naturalHeight The natural height (see class description).
     * @param child The child widget.
     */
    ScrollPanel(int &scrollPos, int naturalHeight, Widget *child);

    /**
     * @brief Construct a scroll panel.
     * 
     * @param stateUrl URL of the runtime state to be used by this scroll panel.
     * @param naturalHeight The natural height (see class description).
     * @param child The child widget.
     */
    ScrollPanel(std::string stateUrl, int naturalHeight, Widget *child);

    /**
     * @brief Set the background color and return the scroll panel itself.
     */
    ScrollPanel* withBackgroundColor(glm::vec4 color);

    virtual void layout() override;
    virtual glm::ivec2 getMinSize() override;
    virtual void draw(GraphicsContext &context) override;
    virtual void onScroll(int delta) override;
    virtual void onMouseMoved(glm::ivec2 pos) override;
    virtual void onMouseDown(Uint8 button) override;
    virtual void onMouseUp(Uint8 button) override;
};

#endif