#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"

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, 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)> on_layer_add;
    std::function<void(Node* target, int old_idx, int new_idx)> on_layer_order;
    NodeLayer* m_current_layer = nullptr;
    std::vector<NodeLayer*> m_layers;
    NodeBorder* m_layers_container;
    NodeSliderH* m_opacity;
    NodeCheckBox* m_alpha_lock;
    NodeComboBox* m_blend_mode;
    virtual Node* clone_instantiate() const override;
    virtual void init() override;
    void add_layer();
    NodeLayer* add_layer(const char* name);
    NodeLayer* get_layer_at(int index);
    void remove_layer(NodeLayer* layer);
    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 clear();
    void update_attributes();
};
