#include "pch.h"

#include "legacy_document_canvas_services.h"

#include "app.h"
#include "legacy_history_services.h"

namespace pp::panopainter {
namespace {

class LegacyDocumentCanvasClearServices final : public pp::app::DocumentCanvasClearServices {
public:
    explicit LegacyDocumentCanvasClearServices(App& app) noexcept
        : app_(app)
    {
    }

    void clear_current_canvas(float r, float g, float b, float a) override
    {
        if (!legacy_document_canvas_available(app_)) {
            return;
        }

        app_.canvas->m_canvas->clear({ r, g, b, a });
    }

private:
    App& app_;
};

class LegacyDocumentResizeServices final : public pp::app::DocumentResizeServices {
public:
    explicit LegacyDocumentResizeServices(App& app) noexcept
        : app_(app)
    {
    }

    void resize_document(int width, int height) override
    {
        if (!legacy_document_canvas_available(app_)) {
            return;
        }

        app_.canvas->m_canvas->resize(width, height);
    }

    void update_title() override
    {
        app_.title_update();
    }

    void clear_history() override
    {
        clear_legacy_history();
    }

private:
    App& app_;
};

} // namespace

bool legacy_document_canvas_available(const App& app) noexcept
{
    return app.canvas != nullptr && app.canvas->m_canvas != nullptr;
}

pp::foundation::Status execute_legacy_document_canvas_clear_plan(
    App& app,
    const pp::app::DocumentCanvasClearPlan& plan)
{
    LegacyDocumentCanvasClearServices services(app);
    return pp::app::execute_document_canvas_clear_plan(plan, services);
}

pp::foundation::Status execute_legacy_document_resize_plan(
    App& app,
    const pp::app::DocumentResizePlan& plan)
{
    LegacyDocumentResizeServices services(app);
    return pp::app::execute_document_resize_plan(plan, services);
}

} // namespace pp::panopainter
