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

namespace {

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);
}

} // 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);
    return harness.finish();
}
