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

namespace {

void resolution_maps_supported_indices_and_labels(pp::tests::Harness& harness)
{
    const auto resolution = pp::app::display_resolution_from_index(3);
    PP_EXPECT(harness, resolution);
    if (resolution) {
        PP_EXPECT(harness, resolution.value() == 2048);
    }

    const auto index = pp::app::document_resolution_to_index(4096);
    PP_EXPECT(harness, index);
    if (index) {
        PP_EXPECT(harness, index.value() == 4U);
    }

    const auto label = pp::app::document_resolution_label(8192);
    PP_EXPECT(harness, label);
    if (label) {
        PP_EXPECT(harness, label.value() == "32K");
    }
}

void resolution_mapping_rejects_out_of_range_values(pp::tests::Harness& harness)
{
    PP_EXPECT(harness, !pp::app::display_resolution_from_index(-1));
    PP_EXPECT(harness, !pp::app::display_resolution_from_index(6));
    PP_EXPECT(harness, !pp::app::document_resolution_to_index(1234));
    PP_EXPECT(harness, !pp::app::document_resolution_label(1234));
}

void document_title_marks_unsaved_documents(pp::tests::Harness& harness)
{
    PP_EXPECT(
        harness,
        pp::app::make_document_title("demo", false, 2048) == "Panodoc: demo (8K)");
    PP_EXPECT(
        harness,
        pp::app::make_document_title("demo", true, 2048) == "Panodoc: demo* (8K)");
}

void document_title_survives_unknown_resolution(pp::tests::Harness& harness)
{
    PP_EXPECT(
        harness,
        pp::app::make_document_title("demo", true, 1234) == "Panodoc: demo* (unknown)");
}

void status_labels_match_legacy_text(pp::tests::Harness& harness)
{
    PP_EXPECT(harness, pp::app::make_dpi_label(1.25F) == "1.2x-dpi");
    PP_EXPECT(harness, pp::app::make_dpi_label(1.26F) == "1.3x-dpi");
    PP_EXPECT(harness, pp::app::make_history_memory_label(1024U * 1024U * 3U / 2U) == "History memory: 1.50 Mb");
}

void recording_label_only_shows_when_recording_with_encoder(pp::tests::Harness& harness)
{
    const auto inactive = pp::app::make_recording_frame_label(false, true, 12);
    PP_EXPECT(harness, !inactive.visible);
    PP_EXPECT(harness, inactive.text.empty());

    const auto missing_encoder = pp::app::make_recording_frame_label(true, false, 12);
    PP_EXPECT(harness, !missing_encoder.visible);
    PP_EXPECT(harness, missing_encoder.text.empty());

    const auto active = pp::app::make_recording_frame_label(true, true, 12);
    PP_EXPECT(harness, active.visible);
    PP_EXPECT(harness, active.text == "Recorded 12 frames");
}

void renderer_diagnostics_report_framebuffer_fetch(pp::tests::Harness& harness)
{
    const auto unsupported = pp::app::plan_renderer_diagnostics({});
    PP_EXPECT(harness, !unsupported.framebuffer_fetch.supported);
    PP_EXPECT(harness, unsupported.framebuffer_fetch.label == "FBF");

    const auto supported = pp::app::plan_renderer_diagnostics({
        .framebuffer_fetch = true,
    });
    PP_EXPECT(harness, supported.framebuffer_fetch.supported);
    PP_EXPECT(harness, supported.framebuffer_fetch.label == "FBF");
}

void renderer_diagnostics_prefer_highest_float_capability_label(pp::tests::Harness& harness)
{
    const auto none = pp::app::plan_renderer_diagnostics({});
    PP_EXPECT(harness, !none.floating_point_targets.supported);
    PP_EXPECT(harness, none.floating_point_targets.label.empty());

    const auto float16 = pp::app::plan_renderer_diagnostics({
        .float16_render_targets = true,
    });
    PP_EXPECT(harness, float16.floating_point_targets.supported);
    PP_EXPECT(harness, float16.floating_point_targets.label == "F16");

    const auto float32 = pp::app::plan_renderer_diagnostics({
        .float32_render_targets = true,
        .float16_render_targets = true,
    });
    PP_EXPECT(harness, float32.floating_point_targets.supported);
    PP_EXPECT(harness, float32.floating_point_targets.label == "F32");

    const auto float32_linear = pp::app::plan_renderer_diagnostics({
        .float32_render_targets = true,
        .float32_linear_filtering = true,
        .float16_render_targets = true,
    });
    PP_EXPECT(harness, float32_linear.floating_point_targets.supported);
    PP_EXPECT(harness, float32_linear.floating_point_targets.label == "F32L");
}

}

int main()
{
    pp::tests::Harness harness;
    harness.run("resolution maps supported indices and labels", resolution_maps_supported_indices_and_labels);
    harness.run("resolution mapping rejects out of range values", resolution_mapping_rejects_out_of_range_values);
    harness.run("document title marks unsaved documents", document_title_marks_unsaved_documents);
    harness.run("document title survives unknown resolution", document_title_survives_unknown_resolution);
    harness.run("status labels match legacy text", status_labels_match_legacy_text);
    harness.run("recording label only shows when recording with encoder", recording_label_only_shows_when_recording_with_encoder);
    harness.run("renderer diagnostics report framebuffer fetch", renderer_diagnostics_report_framebuffer_fetch);
    harness.run("renderer diagnostics prefer highest float capability label", renderer_diagnostics_prefer_highest_float_capability_label);
    return harness.finish();
}
