#include "pch.h"

#include "legacy_brush_package_export_services.h"

#include "app.h"
#include "node_dialog_export_ppbr.h"
#include "node_panel_brush.h"

#include <string>
#include <thread>

namespace pp::panopainter {
namespace {

NodePanelBrushPreset::PPBRInfo to_legacy_ppbr_info(
    const pp::app::BrushPackageExportRequest& request,
    const NodeDialogExportPPBR& dialog)
{
    NodePanelBrushPreset::PPBRInfo info;
    info.author = request.author;
    info.email = request.email;
    info.url = request.url;
    info.descr = request.description;
    info.header_image = dialog.m_header_image;
    info.export_data = request.export_data;
    info.dest_path = request.destination_path;
    return info;
}

class LegacyBrushPackageExportServices final : public pp::app::BrushPackageExportServices {
public:
    LegacyBrushPackageExportServices(
        App& app,
        NodeDialogExportPPBR& dialog,
        LegacyBrushPackageExportMode mode) noexcept
        : app_(app)
        , dialog_(dialog)
        , mode_(mode)
    {
    }

    void export_brush_package(std::string_view path, const pp::app::BrushPackageExportRequest& request) override
    {
        const auto path_string = std::string(path);
        const auto info = to_legacy_ppbr_info(request, dialog_);
        if (mode_ == LegacyBrushPackageExportMode::desktop_async_close_and_message) {
            auto* app = &app_;
            auto* dialog = &dialog_;
            std::thread([app, dialog, path_string, info] {
                BT_SetTerminate();
                app->presets->export_ppbr(path_string, info);
                dialog->destroy();
                app->message_box("Export PPBR", "Brushes exported to:\n" + path_string);
            }).detach();
            return;
        }

        app_.presets->export_ppbr(path_string, info);
    }

private:
    App& app_;
    NodeDialogExportPPBR& dialog_;
    LegacyBrushPackageExportMode mode_ = LegacyBrushPackageExportMode::inline_export_only;
};

} // namespace

pp::app::BrushPackageExportRequest make_legacy_brush_package_export_request(const NodeDialogExportPPBR& dialog)
{
    pp::app::BrushPackageExportRequest request;
    request.author = dialog.txt_author ? dialog.txt_author->m_text : std::string();
    request.email = dialog.txt_email ? dialog.txt_email->m_text : std::string();
    request.url = dialog.txt_url ? dialog.txt_url->m_text : std::string();
    request.description = dialog.txt_descr ? dialog.txt_descr->m_text : std::string();
    request.destination_path = dialog.m_dest_path;
    request.export_data = dialog.export_check && dialog.export_check->checked;
    request.has_header_image = static_cast<bool>(dialog.m_header_image);
    return request;
}

pp::foundation::Status execute_legacy_brush_package_export(
    App& app,
    NodeDialogExportPPBR& dialog,
    const pp::app::BrushPackageExportRequest& request,
    std::string_view path,
    LegacyBrushPackageExportMode mode)
{
    LegacyBrushPackageExportServices services(app, dialog, mode);
    return pp::app::execute_brush_package_export(path, request, services);
}

void complete_legacy_brush_package_export(NodeDialogExportPPBR& dialog, bool saved)
{
    if (saved) {
        dialog.destroy();
    }
}

} // namespace pp::panopainter
