#include "pch.h"

#include "legacy_brush_package_import_services.h"

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

#include <condition_variable>
#include <deque>
#include <functional>
#include <string>
#include <mutex>
#include <stop_token>
#include <thread>

namespace pp::panopainter {
namespace {

class LegacyBrushPackageWorker final {
public:
    LegacyBrushPackageWorker()
        : worker_([this](std::stop_token stop_token) {
            run(stop_token);
        })
    {
    }

    ~LegacyBrushPackageWorker()
    {
        shutdown();
    }

    void post(std::function<void()> task)
    {
        {
            std::lock_guard<std::mutex> lock(mutex_);
            if (stopping_)
                return;
            tasks_.push_back(std::move(task));
        }
        cv_.notify_one();
    }

private:
    void shutdown()
    {
        {
            std::lock_guard<std::mutex> lock(mutex_);
            stopping_ = true;
        }
        cv_.notify_all();
    }

    void run(std::stop_token stop_token)
    {
        for (;;) {
            std::function<void()> task;
            {
                std::unique_lock<std::mutex> lock(mutex_);
                cv_.wait(lock, [&] {
                    return stopping_ || stop_token.stop_requested() || !tasks_.empty();
                });
                if ((stopping_ || stop_token.stop_requested()) && tasks_.empty())
                    break;
                task = std::move(tasks_.front());
                tasks_.pop_front();
            }

            if (task) {
                try {
                    task();
                } catch (...) {
                    LOG("brush package import worker task failed");
                }
            }
        }
    }

    std::mutex mutex_;
    std::condition_variable cv_;
    std::deque<std::function<void()>> tasks_;
    bool stopping_ = false;
    std::jthread worker_;
};

LegacyBrushPackageWorker& brush_package_worker()
{
    static LegacyBrushPackageWorker worker;
    return worker;
}

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);
        brush_package_worker().post([presets, kind, path_string] {
            BT_SetTerminate();
            if (!presets) {
                return;
            }
            if (kind == pp::app::BrushPackageImportKind::abr) {
                presets->import_abr(path_string);
                return;
            }

            presets->import_ppbr(path_string);
        });
    }

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
