#include "pch.h"

#include "legacy_grid_ui_services.h"

#include "app.h"
#include "canvas.h"
#include "image.h"
#include "node_panel_grid.h"

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

namespace pp::panopainter {
namespace {

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

    ~LegacyGridWorker()
    {
        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("grid worker task failed");
                }
            }
        }
    }

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

LegacyGridWorker& grid_worker()
{
    static LegacyGridWorker worker;
    return worker;
}

class LegacyGridUiServices final : public pp::app::GridUiServices {
public:
    explicit LegacyGridUiServices(NodePanelGrid& panel) noexcept
        : panel_(panel)
    {
    }

    void request_heightmap_pick() override
    {
        auto* panel = &panel_;
        App::I->pick_image([panel](std::string path) {
            panel->load_heightmap_file(path, true);
        });
    }

    pp::foundation::Status load_heightmap(std::string_view path, bool raise_ground_opacity) override
    {
        Image img;
        if (!img.load_file(std::string(path)))
            return pp::foundation::Status::invalid_argument("heightmap image could not be loaded");

        panel_.m_file_path = std::string(path);
        panel_.m_hm_image = img.resize(128, 128);
        panel_.m_hm_preview->tex = std::make_shared<Texture2D>();
        panel_.m_hm_preview->tex->create(panel_.m_hm_image);
        panel_.m_hm_preview->tex->create_mipmaps();
        auto sz = panel_.m_hm_preview->tex->size();
        panel_.m_hm_preview->SetAspectRatio(sz.x / sz.y);
        panel_.m_hm_plane.create(1, 1, panel_.m_hm_image, panel_.get_resolution(), panel_.get_height());
        panel_.m_hm_preview->SetHeight(100);
        if (raise_ground_opacity && panel_.m_groud_opacity->get_value() == 0.f)
            panel_.m_groud_opacity->set_value(1.f);
        panel_.m_rt_dirty = true;
        return pp::foundation::Status::success();
    }

    void clear_heightmap(bool updates_preview) override
    {
        panel_.m_hm_plane.create(1, 1, 100 * panel_.get_resolution());
        panel_.m_hm_image.destroy();
        panel_.m_hm_preview->tex.reset();
        panel_.m_hm_preview->SetHeight(0);
    }

    void render_lightmap(bool shows_unsupported_message, bool renders_lightmap) override
    {
        if (shows_unsupported_message)
        {
            App::I->message_box("Rendering failed",
                "Your hardware does not support lightmap rendering.");
            return;
        }

        if (!renders_lightmap)
            return;

        auto panel = std::static_pointer_cast<NodePanelGrid>(panel_.shared_from_this());
        grid_worker().post([panel] {
            BT_SetTerminate();
            panel->bake_uvs();
            if (App::I) {
                App::I->ui_task([panel] {
                    panel->m_hm_shading->set_index(3);
                    panel->m_shade_mode = NodePanelGrid::ShadeMode::Textured;
                });
            }
        });
    }

    void commit_heightmap(bool updates_ground_opacity) override
    {
        Canvas::I->draw_objects([this](const glm::mat4& camera, const glm::mat4& proj, int i) {
            panel_.draw_heightmap(proj, camera, true);
        }, Canvas::I->layer().m_frame_index, true);
        if (updates_ground_opacity)
            panel_.m_groud_opacity->set_value(0);
    }

private:
    NodePanelGrid& panel_;
};

} // namespace

pp::foundation::Status execute_legacy_grid_ui_plan(
    NodePanelGrid& panel,
    const pp::app::GridUiPlan& plan)
{
    LegacyGridUiServices services(panel);
    return pp::app::execute_grid_ui_plan(plan, services);
}

} // namespace pp::panopainter
