#include "pch.h"
#include "app.h"
#include "app_core/brush_ui.h"

void App::brush_update(bool update_color, bool update_brush)
{
//     brushes->select_brush(canvas->m_brush->id);
//     stroke->set_params(canvas->m_brush);
    render_task_async([this, update_color, update_brush]
    {
        pp::app::BrushUiRefreshInput input;
        input.update_color = update_color;
        input.update_brush = update_brush;
        auto current_brush = Canvas::I ? Canvas::I->m_current_brush : nullptr;
        input.has_current_brush = current_brush != nullptr;
        input.has_floating_picker = floating_picker != nullptr;
        input.has_floating_color_panel = floating_color != nullptr;
        if (input.has_current_brush)
        {
            input.tip_flow = current_brush->m_tip_flow;
            input.tip_size = current_brush->m_tip_size;
            input.r = current_brush->m_tip_color.r;
            input.g = current_brush->m_tip_color.g;
            input.b = current_brush->m_tip_color.b;
            input.a = current_brush->m_tip_color.a;
        }

        const auto view = pp::app::plan_brush_ui_refresh(input);
        if (!view) {
            LOG("Brush UI refresh failed: %s", view.status().message);
            return;
        }

        if (view.value().updates_stroke_controls)
        {
            stroke->update_controls();
        }
        if (view.value().updates_quick_flow)
        {
            quick->m_slider_flow->set_value(stroke->m_tip_flow->get_value());
        }
        if (view.value().updates_quick_size)
        {
            quick->m_slider_size->set_value(stroke->m_tip_size->get_value());
        }
        if (view.value().updates_quick_brush_preview && current_brush)
        {
            *quick->m_button_brush_current_preview->m_brush = *current_brush;
            quick->m_button_brush_current_preview->draw_stroke();
        }
        if (view.value().updates_quick_color)
        {
            const glm::vec4 color(view.value().r, view.value().g, view.value().b, view.value().a);
            quick->m_button_color_current_inner->m_color = color;
            if (view.value().updates_floating_picker)
                floating_picker->set_color(color);
            if (view.value().updates_floating_color_panel)
                floating_color->set_color(color);
        }
    }, true);
}
