#ifndef LIST_VIEW_HPP_
#define LIST_VIEW_HPP_

#include <Widget/Layout/Column/Column.hpp>
#include <Widget/Layout/CompoundWidget/CompoundWidget.hpp>
#include <Widget/Layout/Frame/Frame.hpp>

/**
 * @brief A widget for viewing a list of static elements, with visually distinct rows.
 */
class ListView : public CompoundWidget
{
private:
    void appendRow(std::vector<Widget*> &widgets, int index, std::nullptr_t) {}
    void appendRow(std::vector<Widget*> &widgets, int index) {}

    template<typename... TArgs>
    void appendRow(std::vector<Widget*> &widgets, int index, Widget *widget, TArgs... args)
    {
        if (widget != nullptr)
        {
            widgets.push_back(
                new Frame(
                    (index & 1) ? Color::tableRowOdd : Color::tableRowEven,
                    widget
                )
            );
        }

        appendRow(widgets, !index, args...);
    }

    template<typename... TArgs>
    void appendRow(std::vector<Widget*> &widgets, int index, std::vector<Widget*> children, TArgs... args)
    {
        for (const auto &widget : children)
        {
            appendRow(widgets, index, widget);
            index = !index;
        }

        appendRow(widgets, index, args...);
    }

    template<typename... TArgs>
    Widget* makeColumn(TArgs... args)
    {
        std::vector<Widget*> children;

        appendRow(children, 0, args...);

        return new Column(children);
    }

public:
    template<typename... TArgs>
    ListView(TArgs... args) : CompoundWidget(
        makeColumn(args...)
    )
    {}
};

#endif