#include "pch.h"

#include "legacy_document_open_services.h"

#include "app.h"
#include "legacy_brush_package_import_services.h"
#include "legacy_history_services.h"
#include "log.h"
#include "node_panel_brush.h"
#include "node_panel_layer.h"

namespace pp::panopainter {
namespace {

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;
    app.canvas->reset_camera();
    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.");
        }
    });
    pp::panopainter::clear_legacy_history();
}

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);
            target->destroy();
        };
    }

    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);
            target->destroy();
        };
    }

    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);
            target->destroy();
        };
    }

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
