#ifndef TAB_LIST_HPP_
#define TAB_LIST_HPP_

#include <Page/Page.hpp>
#include <vector>
#include <Widget/Widget.hpp>
#include <Material/Material.hpp>

/**
 * @brief A widget which allows the user to switch between pages by clicking on tabs.
 */
class TabList : public Widget
{
public:
    using Page = ::Page;

private:
    static constexpr int tabWidth = 16;
    static constexpr int tabHeight = 16;
    static constexpr int bodyMargin = 2;
    static constexpr int tabSpacing = 2;

    struct State
    {
        unsigned int currentPageIndex = 0;
    };

    std::vector<Page*> m_pages;
    std::shared_ptr<State> state;

    inline void addPages() {}

    template<typename... TPages>
    void addPages(Page *page, TPages... rest)
    {
        if (page != nullptr)
        {
            m_pages.push_back(page);
        }
        
        addPages(rest...);
    }

    void initState(std::string url);
    Page* getCurrentPage();

public:
    /**
     * @brief Construct a new tab list with the specified pages.
     * 
     * Every argument must be of type `TabList::Page*`, and at least one must be
     * provided.
     */
    template<typename... TPages>
    TabList(Page *firstPage, TPages... pages)
    {
        initState("");
        addPages(firstPage, pages...);
    }

    /**
     * @brief Construct a new tab list with the specified pages and state URL.
     * 
     * Every argument besides the URL must be of type `TabList::Page*`, and at least
     * one must be provided.
     */
    template<typename... TPages>
    TabList(std::string stateUrl, Page *firstPage, TPages... pages)
    {
        initState(stateUrl);
        addPages(firstPage, pages...);
    }

    virtual ~TabList();

    virtual void draw(GraphicsContext &context) override;
    virtual glm::ivec2 getMinSize() override;
    virtual void onResize(glm::ivec2 newSize) override;
    virtual void onMouseMoved(glm::ivec2 pos) override;
    virtual void onMouseDown(Uint8 button) override;
    virtual void onMouseUp(Uint8 button) override;
    virtual void onScroll(int delta) override;
    virtual void getInternalTabOrder(std::vector<Widget*> &tabOrder) override;
};

#endif