#pragma once
#include "node_border.h"
#include "node_text.h"
#include "node_checkbox.h"
#include "node_slider.h"
#include "node_button_custom.h"
#include "node_combobox.h"
#include "node_scroll.h"
#include "action.h"
#include "canvas_layer.h"

class NodeLayer : public NodeBorder
{
public:
    std::function<void(NodeLayer* target)> on_selected;
    std::function<void(NodeLayer* target, bool visible)> on_visibility_changed;
    std::function<void(NodeLayer* target, bool highlight)> on_highlight;
    bool m_selected = false;
    glm::vec4 m_color_normal = glm::vec4(.4, .4, .4, 1);
    glm::vec4 m_color_selected = glm::vec4(.3, .3, .3, 1);
    glm::vec4 m_color_hover = glm::vec4(.5, .5, .5, 1);
    std::string m_label_text;
    NodeText* m_label;
    NodeCheckBox* m_visibility;
    virtual Node* clone_instantiate() const override;
    virtual void clone_children(Node* dest) const override;
    virtual void clone_copy(Node* dest) const override;
    virtual void init() override;
    virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) override;
    virtual void loaded() override;
    virtual kEventResult handle_event(Event* e) override;
    virtual void draw() override;
    void set_name(const char* s);
};

class NodePanelLayer : public Node
{
    NodeButtonCustom* btn_add;
    NodeButtonCustom* btn_remove;
    NodeButtonCustom* btn_up;
    NodeButtonCustom* btn_down;
    NodeButtonCustom* btn_duplicate;
    int id_counter = 0;
public:
    std::function<void(Node* target)> on_popup_close;
    std::function<void(Node* target, int old_idx, int new_idx)> on_layer_change;
    std::function<void(Node* target, int idx, float value)> on_layer_opacity_changed;
    std::function<void(Node* target, int idx, bool visible)> on_layer_visibility_changed;
    std::function<void(Node* target, int idx, bool locked)> on_layer_alpha_lock_changed;
    std::function<void(Node* target, int idx, bool highlight)> on_layer_highlight_changed;
    std::function<void(Node* target, int idx, int mode)> on_layer_blend_mode_changed;
    std::function<void(Node* target, int index)> on_layer_delete;
    std::function<void(Node* target, int index)> on_layer_duplicate;
    std::function<void(Node* target, std::shared_ptr<class Layer> layer, int index)> on_layer_add;
    std::function<void(Node* target, int old_idx, int new_idx)> on_layer_order;
    std::shared_ptr<NodeLayer> m_current_layer;
    std::vector<std::shared_ptr<NodeLayer>> m_layers;
    NodeScroll* m_layers_container;
    NodeSliderH* m_opacity;
    NodeCheckBox* m_alpha_lock;
    NodeComboBox* m_blend_mode;
    virtual Node* clone_instantiate() const override;
    virtual void init() override;
    virtual kEventResult handle_event(Event* e) override;
    void merge(int src_index, int dst_index, bool create_history);
    void add_layer(bool add_history = true, bool create_events = false, int index = -1);
    NodeLayer* add_layer(const char* name, bool add_history = true, bool create_events = false,
        std::shared_ptr<Layer> layer = nullptr, std::shared_ptr<NodeLayer> layer_node = nullptr, int index = -1);
    NodeLayer* get_layer_at(int index);
    void remove_layer(NodeLayer* layer, bool add_history = true);
    void handle_layer_opacity(NodeLayer* target, float value);
    void handle_layer_visibility(NodeLayer* target, bool visible);
    void handle_layer_alpha_lock(NodeLayer* target, bool locked);
    void handle_layer_highlight(NodeLayer* target, bool highlight);
    void handle_layer_blend_mode(NodeLayer* target, int mode);
    void handle_layer_selected(NodeLayer* target);
    void save_history();
    void clear();
    void update_attributes();
};

struct ActionLayerChange : public Action
{
    struct LayerState
    {
        bool visible;
        float alpha;
        bool lock;
        int blend_mode;
    };
    NodePanelLayer* m_panel;
    std::vector<LayerState> m_layers;
    virtual void run() override { }
    virtual Action* get_redo() override;
    virtual void undo() override;
    virtual size_t memory() override { return 0; }
};

class ActionLayerAdd : public Action
{
public:
    NodePanelLayer* m_panel;
    std::shared_ptr<Node> m_layer_node;
    int m_layer_order;
    uint32_t m_layer_id;
    virtual void run() override { }
    virtual Action* get_redo() override;
    virtual void undo() override;
    virtual size_t memory() override { return 0; }
};

class ActionLayerRemove : public Action
{
public:
    NodePanelLayer* m_panel;
    std::shared_ptr<Node> m_layer_node;
    std::shared_ptr<Layer> m_layer;
    int m_layer_order;
    virtual void run() override { }
    virtual Action* get_redo() override;
    virtual void undo() override;
    virtual size_t memory() override { return 0; }
};

class ActionLayerMove : public Action
{
public:
    NodePanelLayer* m_panel;
    std::shared_ptr<Node> m_layer_node;
    int m_offset;
    virtual void run() override { }
    virtual Action* get_redo() override;
    virtual void undo() override;
    virtual size_t memory() override { return 0; }
};

struct ActionLayerMerge : public Action
{
    std::shared_ptr<LayerFrame::Snapshot> m_snap;
    std::shared_ptr<Layer> m_layer;
    std::shared_ptr<NodeLayer> m_layer_node;
    std::shared_ptr<NodePanelLayer> m_panel;
    int m_src_index = 0; // removed layer
    int m_dst_index = 0;
    std::array<glm::vec4, 6> m_dirty_box;
    std::array<bool, 6> m_dirty_face;
    virtual void run() override { }
    virtual size_t memory() override { return m_snap->memsize(); }
    virtual void undo() override;
    virtual Action* get_redo() override;
};
