#pragma once

#include "ButtonData.h"
#include "Widget.h"
#include "Color.h"

#include "MediaResources.h"

#include <functional>
#include <string>

class PaintEvent;
class MouseEvent;
class TextNode;
class IconNode;

class Button : public Widget
{
public:
    using clickFunc = std::function<void(Widget* self)>;

    Button(ButtonData::Component component = ButtonData::Component::Elevated);

    ~Button();

    static std::unique_ptr<Button> Create(ButtonData::Component component = ButtonData::Component::Elevated);

    void setSvgIcon(Resource::Icon::Svg icon);

    void setLabel(const std::string& text);

    void setEnabled(bool isEnabled);

    void setOnClickFunction(clickFunc func);

protected:
    void onMyMouseEvent(const MouseEvent* event) override;

    bool isDirty() const override;
    void doPaint(const PaintEvent* event) override;

    void setLabelTextColor(Theme::Sys::Color color);
    void setLabelTextOpacity(float opacity);
    void setLabelTextTypescale(Theme::Sys::Typescale typescale);

    void setIconColor(Theme::Sys::Color color);
    void setIconOpacity(float opacity);

    void updateLabel(const PaintEvent* event);
    void updateIcon(const PaintEvent* event);

    void setState(ButtonData::State state);
    void resetState();
    void updateState() override;

private:
    ButtonData mStyle;

    std::string mLabel;
    Theme::Sys::Color mLabelTextColor;
    Theme::Sys::Typescale mLabelTextTypescale;
    float mLabelOpacity{ 1.0 };
    std::unique_ptr<TextNode> mTextNode;

    Resource::Icon::Svg mIcon{ Resource::Icon::Svg::NONE};
    Theme::Sys::Color mIconColor;
    float mIconOpacity{ 1.0 };
    std::unique_ptr<IconNode> mIconNode;

    bool mContentDirty{ true };

    clickFunc mClickFunc;
    bool mEnabled{ true };
};

using ButtonUPtr = std::unique_ptr<Button>;
