#include "pch.h"
#include "log.h"
#include "node_stroke_preview.h"
#include "texture.h"
#include "canvas.h"
#include "app.h"
#include "legacy_canvas_stroke_shader_services.h"
#include "legacy_node_stroke_preview_execution_services.h"
#include "legacy_node_stroke_preview_runtime_services.h"
#include "legacy_node_stroke_preview_sample_services.h"
#include "util.h"
#include <array>

Node* NodeStrokePreview::clone_instantiate() const
{
    return new NodeStrokePreview();
}

void NodeStrokePreview::clone_copy(Node* dest) const
{
    NodeBorder::clone_copy(dest);
}

void NodeStrokePreview::clone_children(Node* dest) const
{
    // stop children cloning
}

void NodeStrokePreview::clone_finalize(Node* dest) const
{
    pp::panopainter::initialize_legacy_node_stroke_preview_clone(dest);
}

void NodeStrokePreview::init_controls()
{
//     TextureManager::load("data/thumbs/Round-Hard.png");
//     Canvas::I->m_current_brush.m_tex_id = const_hash("data/thumbs/Round-Hard.png");
}

void NodeStrokePreview::stroke_draw_mix(const glm::vec2& bb_min, const glm::vec2& bb_sz)
{
    const bool mix_ok = pp::panopainter::execute_legacy_node_stroke_preview_mix_pass(
        *m_brush,
        m_size,
        m_rtt_mixer,
        bb_min,
        bb_sz,
        m_sampler_linear,
        m_tex_background,
        m_tex,
        m_tex_dual,
        [this] {
            m_plane.draw_fill();
        });
    assert(mix_ok);
}

glm::vec4 NodeStrokePreview::stroke_draw_samples(
    std::array<vertex_t, 4>& P,
    Texture2D& blend_tex,
    bool copy_stroke_destination)
{
    return pp::panopainter::execute_legacy_node_stroke_preview_sample_pass(
        m_rtt,
        P,
        m_brush_shape,
        blend_tex,
        copy_stroke_destination);
}

std::vector<NodeStrokePreview::StrokeFrame> NodeStrokePreview::stroke_draw_compute(const Stroke& stroke, float zoom) const
{
    return pp::panopainter::plan_legacy_node_stroke_preview_stroke_frames(
        stroke,
        zoom,
        glm::vec2(m_rtt.getWidth(), m_rtt.getHeight()));
}

void NodeStrokePreview::draw_stroke_immediate()
{
    if (m_size.x == 0 || m_size.y == 0)
        return;
    const bool sequence_ok = pp::panopainter::execute_legacy_node_stroke_preview_immediate_draw(*this);
    assert(sequence_ok);
}
