#pragma once
#include "node_border.h"
#include "rtt.h"
#include "brush.h"
#include "texture.h"

class NodeStrokePreview : public NodeBorder
{
    struct StrokeFrame
    {
        glm::vec4 col;
        float flow;
        float opacity;
        std::array<vertex_t, 4> shapes;
        glm::vec4 m_mixer_rect;
    };

    RTT m_rtt;
    RTT m_rtt_mixer;
    Texture2D m_tex; // blending tmp texture
    Texture2D m_tex_dual;
    Texture2D m_tex_background;
    Sampler m_sampler_linear;
    Sampler m_sampler_linear_repeat;
    Sampler m_sampler_mipmap;
    DynamicShape m_brush_shape;
public:
    static std::atomic_int s_instances;
    static std::atomic_bool s_running;
    static std::thread s_renderer;
    static BlockingQueue<NodeStrokePreview*> s_queue;
    static void terminate_renderer();
    std::shared_ptr<Brush> m_brush;
    std::shared_ptr<Brush> m_dual_brush;
    Stroke m_stroke;
    Stroke m_dual_stroke;
    std::vector<glm::vec2> m_bez_points;
    float m_min_flow = 0.f;
    virtual Node* clone_instantiate() const override;
    virtual void clone_copy(Node* dest) const override;
    virtual void clone_children(Node* dest) const override;
    virtual void clone_finalize(Node* dest) const override;
    void init_controls();
    virtual void restore_context() override;
    virtual void clear_context() override;
    void stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz);
    // return rect {origin, size}
    glm::vec4 stroke_draw_samples(std::array<vertex_t, 4>& P, Texture2D& blend_tex);
    std::vector<StrokeFrame> stroke_draw_compute(Stroke& stroke) const;
    void draw_stroke();
    void draw_stroke_immediate();
    virtual void draw() override;
    virtual void handle_resize(glm::vec2 old_size, glm::vec2 new_size) override;
    virtual void destroy_immediate() override;
};
