#include "pch.h"

#include "legacy_brush_package_import_services.h"

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

#include <string>
#include <thread>

namespace pp::panopainter {
namespace {

class LegacyBrushPackageImportServices final : public pp::app::BrushPackageImportServices {
public:
    explicit LegacyBrushPackageImportServices(App& app) noexcept
        : app_(app)
    {
    }

    void import_brush_package(pp::app::BrushPackageImportKind kind, std::string_view path) override
    {
        auto presets = app_.presets;
        const auto path_string = std::string(path);
        if (kind == pp::app::BrushPackageImportKind::abr) {
            std::thread(&NodePanelBrushPreset::import_abr, presets, path_string).detach();
            return;
        }

        std::thread(&NodePanelBrushPreset::import_ppbr, presets, path_string).detach();
    }

private:
    App& app_;
};

} // namespace

pp::foundation::Status execute_legacy_brush_package_import(
    App& app,
    pp::app::BrushPackageImportKind kind,
    std::string_view path)
{
    LegacyBrushPackageImportServices services(app);
    return pp::app::execute_brush_package_import(kind, path, services);
}

} // namespace pp::panopainter
