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

#include <cmath>

namespace {

void initial_surface_matches_legacy_default(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_app_initial_surface();

    PP_EXPECT(harness, plan.width == 960.0F);
    PP_EXPECT(harness, plan.height == 540.0F);
}

void update_plan_skips_idle_frames(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_app_frame_update(false, false);

    PP_EXPECT(harness, !plan.update_frame);
    PP_EXPECT(harness, !plan.update_layouts);
    PP_EXPECT(harness, !plan.refresh_canvas_toolbar);
}

void update_plan_refreshes_redraw_or_animated_frames(pp::tests::Harness& harness)
{
    const auto redraw = pp::app::plan_app_frame_update(true, false);
    const auto animated = pp::app::plan_app_frame_update(false, true);

    PP_EXPECT(harness, redraw.update_frame);
    PP_EXPECT(harness, redraw.update_layouts);
    PP_EXPECT(harness, redraw.refresh_canvas_toolbar);
    PP_EXPECT(harness, animated.update_frame);
    PP_EXPECT(harness, animated.update_layouts);
    PP_EXPECT(harness, animated.refresh_canvas_toolbar);
}

void draw_plan_selects_canvas_and_ui_passes(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_app_frame_draw(true, true, true, true, false);

    PP_EXPECT(harness, plan.draw_canvas_stroke);
    PP_EXPECT(harness, plan.draw_vr_ui);
    PP_EXPECT(harness, plan.draw_main_ui);
    PP_EXPECT(harness, plan.reset_redraw);
}

void draw_plan_skips_missing_canvas_document(pp::tests::Harness& harness)
{
    const auto missing_node = pp::app::plan_app_frame_draw(false, true, false, true, false);
    const auto missing_document = pp::app::plan_app_frame_draw(true, false, false, true, false);

    PP_EXPECT(harness, !missing_node.draw_canvas_stroke);
    PP_EXPECT(harness, !missing_document.draw_canvas_stroke);
}

void draw_plan_respects_vr_visibility_modes(pp::tests::Harness& harness)
{
    const auto hidden_vr_ui = pp::app::plan_app_frame_draw(true, true, true, false, false);
    const auto vr_only = pp::app::plan_app_frame_draw(true, true, false, true, true);

    PP_EXPECT(harness, !hidden_vr_ui.draw_vr_ui);
    PP_EXPECT(harness, hidden_vr_ui.draw_main_ui);
    PP_EXPECT(harness, !vr_only.draw_vr_ui);
    PP_EXPECT(harness, !vr_only.draw_main_ui);
}

void tick_plan_selects_available_layouts(pp::tests::Harness& harness)
{
    const auto both = pp::app::plan_app_frame_tick(true, true);
    const auto main_only = pp::app::plan_app_frame_tick(false, true);
    const auto none = pp::app::plan_app_frame_tick(false, false);

    PP_EXPECT(harness, both.tick_designer_layout);
    PP_EXPECT(harness, both.tick_main_layout);
    PP_EXPECT(harness, !main_only.tick_designer_layout);
    PP_EXPECT(harness, main_only.tick_main_layout);
    PP_EXPECT(harness, !none.tick_designer_layout);
    PP_EXPECT(harness, !none.tick_main_layout);
}

void resize_plan_projects_render_target_and_redraw(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_app_resize(1280.9F, 720.1F);

    PP_EXPECT(harness, plan);
    if (plan) {
        PP_EXPECT(harness, plan.value().width == 1280.9F);
        PP_EXPECT(harness, plan.value().height == 720.1F);
        PP_EXPECT(harness, plan.value().render_target_width == 1280);
        PP_EXPECT(harness, plan.value().render_target_height == 720);
        PP_EXPECT(harness, plan.value().recreate_ui_render_target);
        PP_EXPECT(harness, plan.value().request_redraw);
    }
}

void resize_plan_rejects_invalid_dimensions(pp::tests::Harness& harness)
{
    PP_EXPECT(harness, !pp::app::plan_app_resize(0.0F, 720.0F));
    PP_EXPECT(harness, !pp::app::plan_app_resize(1280.0F, -1.0F));
    PP_EXPECT(harness, !pp::app::plan_app_resize(std::nanf(""), 720.0F));
}

void ui_observer_plan_projects_visible_clip_to_scissor(pp::tests::Harness& harness)
{
    const pp::app::AppUiObserverParentClip parents[] {
        {
            .clip = {
                .x = 0.0F,
                .y = 0.0F,
                .width = 80.0F,
                .height = 60.0F,
            },
            .padding_top = 5.0F,
            .padding_right = 5.0F,
            .padding_bottom = 5.0F,
            .padding_left = 5.0F,
        },
    };

    const auto plan = pp::app::plan_app_ui_observer(
        true,
        true,
        false,
        {
            .x = 10.0F,
            .y = 20.0F,
            .width = 100.0F,
            .height = 50.0F,
        },
        parents,
        540.0F,
        2.0F,
        10.0F,
        5.0F);

    PP_EXPECT(harness, plan);
    if (plan) {
        PP_EXPECT(harness, plan.value().draw_node);
        PP_EXPECT(harness, plan.value().notify_enter_screen);
        PP_EXPECT(harness, !plan.value().notify_leave_screen);
        PP_EXPECT(harness, plan.value().next_on_screen);
        PP_EXPECT(harness, plan.value().visible_clip.width == 65.0F);
        PP_EXPECT(harness, plan.value().visible_clip.height == 35.0F);
        PP_EXPECT(harness, plan.value().scissor_x == 28);
        PP_EXPECT(harness, plan.value().scissor_y == 433);
        PP_EXPECT(harness, plan.value().scissor_width == 134);
        PP_EXPECT(harness, plan.value().scissor_height == 74);
    }
}

void ui_observer_plan_notifies_leave_for_clipped_visible_node(pp::tests::Harness& harness)
{
    const pp::app::AppUiObserverParentClip parents[] {
        {
            .clip = {
                .x = 0.0F,
                .y = 0.0F,
                .width = 10.0F,
                .height = 10.0F,
            },
        },
    };

    const auto plan = pp::app::plan_app_ui_observer(
        true,
        true,
        true,
        {
            .x = 100.0F,
            .y = 100.0F,
            .width = 10.0F,
            .height = 10.0F,
        },
        parents,
        540.0F,
        1.0F,
        0.0F,
        0.0F);

    PP_EXPECT(harness, plan);
    if (plan) {
        PP_EXPECT(harness, !plan.value().draw_node);
        PP_EXPECT(harness, plan.value().notify_leave_screen);
        PP_EXPECT(harness, !plan.value().next_on_screen);
    }
}

void ui_observer_plan_rejects_bad_geometry(pp::tests::Harness& harness)
{
    PP_EXPECT(
        harness,
        !pp::app::plan_app_ui_observer(
            true,
            true,
            false,
            {
                .x = 0.0F,
                .y = 0.0F,
                .width = 10.0F,
                .height = 10.0F,
            },
            {},
            540.0F,
            0.0F,
            0.0F,
            0.0F));
    PP_EXPECT(
        harness,
        !pp::app::plan_app_ui_observer(
            true,
            true,
            false,
            {
                .x = std::nanf(""),
                .y = 0.0F,
                .width = 10.0F,
                .height = 10.0F,
            },
            {},
            540.0F,
            1.0F,
            0.0F,
            0.0F));
}

} // namespace

int main()
{
    pp::tests::Harness harness;
    harness.run("initial surface matches legacy default", initial_surface_matches_legacy_default);
    harness.run("update plan skips idle frames", update_plan_skips_idle_frames);
    harness.run("update plan refreshes redraw or animated frames", update_plan_refreshes_redraw_or_animated_frames);
    harness.run("draw plan selects canvas and UI passes", draw_plan_selects_canvas_and_ui_passes);
    harness.run("draw plan skips missing canvas document", draw_plan_skips_missing_canvas_document);
    harness.run("draw plan respects VR visibility modes", draw_plan_respects_vr_visibility_modes);
    harness.run("tick plan selects available layouts", tick_plan_selects_available_layouts);
    harness.run("resize plan projects render target and redraw", resize_plan_projects_render_target_and_redraw);
    harness.run("resize plan rejects invalid dimensions", resize_plan_rejects_invalid_dimensions);
    harness.run("ui observer plan projects visible clip to scissor", ui_observer_plan_projects_visible_clip_to_scissor);
    harness.run("ui observer plan notifies leave for clipped visible node", ui_observer_plan_notifies_leave_for_clipped_visible_node);
    harness.run("ui observer plan rejects bad geometry", ui_observer_plan_rejects_bad_geometry);
    return harness.finish();
}
