#include "pch.h"

#include "legacy_document_open_services.h"

#include "action.h"
#include "app.h"
#include "legacy_brush_package_import_services.h"
#include "legacy_canvas_view_services.h"
#include "legacy_ui_overlay_services.h"
#include "log.h"
#include "node_panel_brush.h"
#include "node_panel_layer.h"

namespace pp::panopainter {
namespace {

class LegacyDocumentHistoryServices final : public pp::app::HistoryUiServices {
public:
    void invoke_undo() override
    {
        ActionManager::undo();
    }

    void invoke_redo() override
    {
        ActionManager::redo();
    }

    void clear_history() override
    {
        ActionManager::clear();
    }
};

pp::foundation::Status apply_document_history(const pp::app::HistoryUiPlan& plan)
{
    LegacyDocumentHistoryServices services;
    return pp::app::execute_history_ui_plan(plan, services);
}

void open_legacy_project(App& app, const pp::app::DocumentOpenRoute& route)
{
    app.doc_name = route.name;
    app.doc_dir = route.directory;
    app.doc_path = route.path;
    const auto reset_status = execute_legacy_canvas_camera_reset(app);
    if (!reset_status.ok())
        LOG("Project open camera reset failed: %s", reset_status.message);
    app.layers->clear();
    app.canvas->m_canvas->project_open(route.path, [&app](bool success) {
        if (success)
        {
            app.title_update();
            for (std::size_t layer_index = 0; layer_index < app.canvas->m_canvas->m_layers.size(); ++layer_index)
            {
                auto layer = app.layers->add_layer(app.canvas->m_canvas->m_layers[layer_index]->m_name.c_str(), false);
                layer->m_visibility->set_value(app.canvas->m_canvas->m_layers[layer_index]->m_visible);
            }
        }
        else
        {
            app.message_box(
                "Open Document Error",
                "There was an error opening the document.\n"
                "It may be inaccessible or corrupted.");
        }
    });
    const auto history_status = apply_document_history(pp::app::plan_document_open_history(route));
    if (!history_status.ok()) {
        LOG("Project open history effect failed: %s", history_status.message);
    }
}

class LegacyDocumentOpenServices final : public pp::app::DocumentOpenServices {
public:
    explicit LegacyDocumentOpenServices(App& app) noexcept
        : app_(app)
    {
    }

    void prompt_import_abr(const pp::app::DocumentOpenRoute& route) override
    {
        auto* app = &app_;
        auto mb = app_.message_box("Import ABR", "Would you like to import the brushes?", true);
        mb->on_submit = [app, path = route.path](Node* target) {
            const auto status = pp::panopainter::execute_legacy_brush_package_import(
                *app,
                pp::app::BrushPackageImportKind::abr,
                path);
            if (!status.ok())
                LOG("ABR import failed: %s", status.message);
            pp::panopainter::close_legacy_dialog_node(*target);
        };
    }

    void prompt_import_ppbr(const pp::app::DocumentOpenRoute& route) override
    {
        auto* app = &app_;
        auto mb = app_.message_box("Import PPBR", "Would you like to import the brushes?", true);
        mb->on_submit = [app, path = route.path](Node* target) {
            const auto status = pp::panopainter::execute_legacy_brush_package_import(
                *app,
                pp::app::BrushPackageImportKind::ppbr,
                path);
            if (!status.ok())
                LOG("PPBR import failed: %s", status.message);
            pp::panopainter::close_legacy_dialog_node(*target);
        };
    }

    void open_project_now(const pp::app::DocumentOpenRoute& route) override
    {
        open_legacy_project(app_, route);
    }

    void prompt_discard_unsaved_project(const pp::app::DocumentOpenRoute& route) override
    {
        auto* app = &app_;
        auto mb = app_.message_box(
            "Unsaved document",
            "Do you want to close the unsaved document before opening the file?",
            true);
        mb->on_submit = [app, route](Node* target) {
            open_legacy_project(*app, route);
            pp::panopainter::close_legacy_dialog_node(*target);
        };
    }

private:
    App& app_;
};

} // namespace

pp::foundation::Status execute_legacy_document_open_plan(
    App& app,
    pp::app::DocumentOpenPlanAction action,
    const pp::app::DocumentOpenRoute& route)
{
    LegacyDocumentOpenServices services(app);
    return pp::app::execute_document_open_plan(action, route, services);
}

} // namespace pp::panopainter
