#include "pch.h"

#include "legacy_brush_panel_item_ui.h"

#include "app.h"
#include "texture.h"

Node* NodeButtonBrush::clone_instantiate() const
{
    return pp::panopainter::clone_legacy_brush_button(*this);
}

void NodeButtonBrush::init()
{
    pp::panopainter::init_legacy_brush_button(*this);
}

void NodeButtonBrush::set_icon(const char* path)
{
    pp::panopainter::set_legacy_brush_button_icon(*this, path);
}

void NodeButtonBrush::draw()
{
    pp::panopainter::draw_legacy_brush_button(*this);
}

bool NodeButtonBrush::read(BinaryStreamReader& r)
{
    return pp::panopainter::read_legacy_brush_button(*this, r);
}

void NodeButtonBrush::write(BinaryStreamWriter& w) const
{
    pp::panopainter::write_legacy_brush_button(*this, w);
}

Node* NodeBrushPresetItem::clone_instantiate() const
{
    return pp::panopainter::clone_legacy_brush_preset_item(*this);
}

void NodeBrushPresetItem::init()
{
    pp::panopainter::init_legacy_brush_preset_item(*this);
}

void NodeBrushPresetItem::draw()
{
    pp::panopainter::draw_legacy_brush_preset_item(*this);
}

namespace pp::panopainter {

Node* clone_legacy_brush_button(const NodeButtonBrush&)
{
    return new NodeButtonBrush();
}

void init_legacy_brush_button(NodeButtonBrush& owner)
{
    owner.init_template_file("data/dialogs/panel-brushes.xml", "tpl-brush-icon");
    owner.color_hover = glm::vec4(.7, .7, .7, 1);
    owner.color_normal = glm::vec4(.3, .3, .3, 1);
    owner.m_color = owner.color_normal;
    owner.img = (NodeImage*)owner.m_children[0].get();
}

void set_legacy_brush_button_icon(NodeButtonBrush& owner, const char* path)
{
    owner.img->m_path = path;
    owner.img->m_tex_id = const_hash(owner.img->m_path.c_str());
    owner.img->m_use_mipmaps = true;
    owner.img->create();
}

void draw_legacy_brush_button(NodeButtonBrush& owner)
{
    owner.m_color = owner.m_mouse_inside ? owner.color_hover : owner.color_normal;
    owner.m_color = owner.m_selected ? glm::vec4(.9, 0, 0, 1) : owner.m_color;
    owner.NodeButtonCustom::draw();
}

bool read_legacy_brush_button(NodeButtonBrush& owner, BinaryStreamReader& r)
{
    Serializer::Descriptor d;
    r >> d;
    d.value<Serializer::CString>("brush_name", owner.brush_name);
    d.value<Serializer::CString>("high_path", owner.high_path);
    d.value<Serializer::CString>("thumb_path", owner.thumb_path);
    d.value<Serializer::Boolean>("m_user_brush", owner.m_user_brush);
    owner.high_path = str_replace(owner.high_path, "{data_path}", App::I->data_path);
    owner.thumb_path = str_replace(owner.thumb_path, "{data_path}", App::I->data_path);
    return true;
}

void write_legacy_brush_button(const NodeButtonBrush& owner, BinaryStreamWriter& w)
{
    Serializer::Descriptor d;
    d.class_id = "brush";
    d.name = L"Brush class";
    d.props["brush_name"] = std::make_shared<Serializer::CString>(owner.brush_name);
    d.props["high_path"] = std::make_shared<Serializer::CString>(
        str_replace(owner.high_path, App::I->data_path, "{data_path}"));
    d.props["thumb_path"] = std::make_shared<Serializer::CString>(
        str_replace(owner.thumb_path, App::I->data_path, "{data_path}"));
    d.props["m_user_brush"] = std::make_shared<Serializer::Boolean>(owner.m_user_brush);
    w << d;
}

Node* clone_legacy_brush_preset_item(const NodeBrushPresetItem&)
{
    return new NodeBrushPresetItem();
}

void init_legacy_brush_preset_item(NodeBrushPresetItem& owner)
{
    owner.init_template_file("data/dialogs/panel-brushes.xml", "tpl-brush-preset");
    owner.color_hover = glm::vec4(.7, .7, .7, 1);
    owner.color_normal = glm::vec4(.3, .3, .3, 1);
    owner.m_color = owner.color_normal;
    owner.m_thumb = owner.find<NodeImage>("thumb");
    owner.m_caption_size = owner.find<NodeText>("caption-size");
    owner.m_preview = owner.find<NodeStrokePreview>("canvas");
    owner.m_preview->m_min_flow = 1.f;
}

void draw_legacy_brush_preset_item(NodeBrushPresetItem& owner)
{
    owner.m_color = owner.m_mouse_inside ? owner.color_hover : owner.color_normal;
    owner.m_color = owner.m_selected ? glm::vec4(.9, 0, 0, 1) : owner.m_color;
    owner.NodeButtonCustom::draw();
}

} // namespace pp::panopainter
