#include "app_core/grid_ui.h"
#include "test_harness.h"

#include <string>

namespace {

class FakeGridUiServices final : public pp::app::GridUiServices {
public:
    void request_heightmap_pick() override
    {
        picks += 1;
        call_order += "pick;";
    }

    pp::foundation::Status load_heightmap(std::string_view path, bool raise_ground_opacity) override
    {
        loads += 1;
        last_path = std::string(path);
        last_raise_ground_opacity = raise_ground_opacity;
        call_order += "load;";
        if (fail_load) {
            return pp::foundation::Status::invalid_argument("fake load failed");
        }
        return pp::foundation::Status::success();
    }

    void clear_heightmap(bool updates_preview) override
    {
        clears += 1;
        last_updates_preview = updates_preview;
        call_order += "clear;";
    }

    void render_lightmap(bool shows_unsupported_message, bool renders_lightmap) override
    {
        renders += 1;
        last_shows_unsupported_message = shows_unsupported_message;
        last_renders_lightmap = renders_lightmap;
        call_order += "render;";
    }

    void commit_heightmap(bool updates_ground_opacity) override
    {
        commits += 1;
        last_updates_ground_opacity = updates_ground_opacity;
        call_order += "commit;";
    }

    int picks = 0;
    int loads = 0;
    int clears = 0;
    int renders = 0;
    int commits = 0;
    bool fail_load = false;
    bool last_raise_ground_opacity = false;
    bool last_updates_preview = false;
    bool last_shows_unsupported_message = false;
    bool last_renders_lightmap = false;
    bool last_updates_ground_opacity = false;
    std::string last_path;
    std::string call_order;
};

void heightmap_load_reload_and_clear_plan_state(pp::tests::Harness& harness)
{
    const auto pick = pp::app::plan_grid_heightmap_pick();
    PP_EXPECT(harness, pick.operation == pp::app::GridUiOperation::request_heightmap_pick);
    PP_EXPECT(harness, pick.opens_picker);
    PP_EXPECT(harness, !pick.mutates_grid_state);

    const auto load = pp::app::plan_grid_heightmap_load("D:/Paint/height.png");
    PP_EXPECT(harness, load);
    if (load) {
        PP_EXPECT(harness, load.value().operation == pp::app::GridUiOperation::load_heightmap);
        PP_EXPECT(harness, load.value().path == "D:/Paint/height.png");
        PP_EXPECT(harness, load.value().loads_heightmap);
        PP_EXPECT(harness, load.value().updates_preview);
        PP_EXPECT(harness, load.value().updates_ground_opacity);
    }

    const auto reload = pp::app::plan_grid_heightmap_reload("D:/Paint/height.png");
    PP_EXPECT(harness, reload);
    if (reload) {
        PP_EXPECT(harness, reload.value().operation == pp::app::GridUiOperation::reload_heightmap);
        PP_EXPECT(harness, reload.value().loads_heightmap);
        PP_EXPECT(harness, !reload.value().updates_ground_opacity);
    }

    const auto clear_existing = pp::app::plan_grid_heightmap_clear(true);
    PP_EXPECT(harness, clear_existing.clears_heightmap);
    PP_EXPECT(harness, clear_existing.updates_preview);
    PP_EXPECT(harness, clear_existing.mutates_grid_state);

    const auto clear_empty = pp::app::plan_grid_heightmap_clear(false);
    PP_EXPECT(harness, clear_empty.clears_heightmap);
    PP_EXPECT(harness, !clear_empty.updates_preview);
    PP_EXPECT(harness, !clear_empty.mutates_grid_state);

    PP_EXPECT(harness, !pp::app::plan_grid_heightmap_load(""));
    PP_EXPECT(harness, !pp::app::plan_grid_heightmap_reload(""));
}

void lightmap_render_validates_capabilities_and_limits(pp::tests::Harness& harness)
{
    const auto supported = pp::app::plan_grid_lightmap_render(true, true, false, 1024, 32);
    PP_EXPECT(harness, supported);
    if (supported) {
        PP_EXPECT(harness, supported.value().operation == pp::app::GridUiOperation::render_lightmap);
        PP_EXPECT(harness, supported.value().renders_lightmap);
        PP_EXPECT(harness, supported.value().shows_progress);
        PP_EXPECT(harness, supported.value().updates_shading_mode);
        PP_EXPECT(harness, supported.value().texture_resolution == 1024);
        PP_EXPECT(harness, supported.value().sample_count == 32);
    }

    const auto missing_heightmap = pp::app::plan_grid_lightmap_render(false, true, false, 1024, 32);
    PP_EXPECT(harness, missing_heightmap);
    if (missing_heightmap) {
        PP_EXPECT(harness, !missing_heightmap.value().renders_lightmap);
        PP_EXPECT(harness, !missing_heightmap.value().shows_unsupported_message);
    }

    const auto unsupported = pp::app::plan_grid_lightmap_render(true, false, false, 1024, 32);
    PP_EXPECT(harness, unsupported);
    if (unsupported) {
        PP_EXPECT(harness, unsupported.value().shows_unsupported_message);
        PP_EXPECT(harness, !unsupported.value().renders_lightmap);
    }

    PP_EXPECT(harness, !pp::app::plan_grid_lightmap_render(true, true, false, 0, 32));
    PP_EXPECT(harness, !pp::app::plan_grid_lightmap_render(true, true, false, 1024, 0));
    PP_EXPECT(harness, !pp::app::plan_grid_lightmap_render(true, true, false, 20000, 32));
    PP_EXPECT(harness, !pp::app::plan_grid_lightmap_render(true, true, false, 1024, 4097));
}

void commit_plan_requires_canvas(pp::tests::Harness& harness)
{
    const auto live = pp::app::plan_grid_heightmap_commit(true);
    PP_EXPECT(harness, live.operation == pp::app::GridUiOperation::commit_heightmap);
    PP_EXPECT(harness, live.commits_heightmap);
    PP_EXPECT(harness, live.updates_ground_opacity);

    const auto headless = pp::app::plan_grid_heightmap_commit(false);
    PP_EXPECT(harness, !headless.commits_heightmap);
    PP_EXPECT(harness, !headless.mutates_grid_state);
}

void executor_dispatches_grid_operations(pp::tests::Harness& harness)
{
    FakeGridUiServices services;

    PP_EXPECT(harness, pp::app::execute_grid_ui_plan(pp::app::plan_grid_heightmap_pick(), services).ok());

    const auto load = pp::app::plan_grid_heightmap_load("D:/Paint/height.png");
    PP_EXPECT(harness, load);
    if (load) {
        PP_EXPECT(harness, pp::app::execute_grid_ui_plan(load.value(), services).ok());
    }

    const auto clear = pp::app::plan_grid_heightmap_clear(true);
    PP_EXPECT(harness, pp::app::execute_grid_ui_plan(clear, services).ok());

    const auto render = pp::app::plan_grid_lightmap_render(true, true, false, 1024, 32);
    PP_EXPECT(harness, render);
    if (render) {
        PP_EXPECT(harness, pp::app::execute_grid_ui_plan(render.value(), services).ok());
    }

    const auto commit = pp::app::plan_grid_heightmap_commit(true);
    PP_EXPECT(harness, pp::app::execute_grid_ui_plan(commit, services).ok());

    PP_EXPECT(harness, services.picks == 1);
    PP_EXPECT(harness, services.loads == 1);
    PP_EXPECT(harness, services.clears == 1);
    PP_EXPECT(harness, services.renders == 1);
    PP_EXPECT(harness, services.commits == 1);
    PP_EXPECT(harness, services.last_path == "D:/Paint/height.png");
    PP_EXPECT(harness, services.last_raise_ground_opacity);
    PP_EXPECT(harness, services.last_updates_preview);
    PP_EXPECT(harness, services.last_renders_lightmap);
    PP_EXPECT(harness, services.last_updates_ground_opacity);
    PP_EXPECT(harness, services.call_order == "pick;load;clear;render;commit;");
}

void executor_rejects_malformed_grid_plans(pp::tests::Harness& harness)
{
    FakeGridUiServices services;

    pp::app::GridUiPlan pick;
    pick.operation = pp::app::GridUiOperation::request_heightmap_pick;
    PP_EXPECT(harness, !pp::app::execute_grid_ui_plan(pick, services).ok());

    pp::app::GridUiPlan load;
    load.operation = pp::app::GridUiOperation::load_heightmap;
    load.loads_heightmap = true;
    PP_EXPECT(harness, !pp::app::execute_grid_ui_plan(load, services).ok());

    pp::app::GridUiPlan render;
    render.operation = pp::app::GridUiOperation::render_lightmap;
    render.renders_lightmap = true;
    render.texture_resolution = 0;
    render.sample_count = 32;
    PP_EXPECT(harness, !pp::app::execute_grid_ui_plan(render, services).ok());

    const auto failing_load = pp::app::plan_grid_heightmap_load("D:/Paint/missing.png");
    PP_EXPECT(harness, failing_load);
    if (failing_load) {
        services.fail_load = true;
        PP_EXPECT(harness, !pp::app::execute_grid_ui_plan(failing_load.value(), services).ok());
    }

    PP_EXPECT(harness, services.picks == 0);
    PP_EXPECT(harness, services.renders == 0);
    PP_EXPECT(harness, services.loads == 1);
}

} // namespace

int main()
{
    pp::tests::Harness harness;
    harness.run("heightmap load reload and clear plan state", heightmap_load_reload_and_clear_plan_state);
    harness.run("lightmap render validates capabilities and limits", lightmap_render_validates_capabilities_and_limits);
    harness.run("commit plan requires canvas", commit_plan_requires_canvas);
    harness.run("executor dispatches grid operations", executor_dispatches_grid_operations);
    harness.run("executor rejects malformed grid plans", executor_rejects_malformed_grid_plans);
    return harness.finish();
}
