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

#include <array>
#include <string>

namespace {

class FakeAppPreferenceServices final : public pp::app::AppPreferenceServices {
public:
    void apply_ui_scale(const pp::app::ScaleApplicationPlan& plan) override
    {
        ui_scale = plan.scale;
        ui_font_scale = plan.font_scale;
        call_order += "ui-scale;";
    }

    void apply_viewport_scale(const pp::app::ScaleApplicationPlan& plan) override
    {
        viewport_scale = plan.scale;
        viewport_font_scale = plan.font_scale;
        call_order += "viewport-scale;";
    }

    void apply_interface_direction(const pp::app::InterfaceDirectionPlan& plan) override
    {
        interface_direction = plan.direction;
        call_order += "direction;";
    }

    bool apply_vr_mode_preference(const pp::app::StoredBooleanPreferencePlan& plan) override
    {
        vr_mode = plan.value;
        call_order += "vr-mode;";
        return vr_mode_result;
    }

    void apply_vr_controllers_preference(const pp::app::StoredBooleanPreferencePlan& plan) override
    {
        vr_controllers = plan.value;
        call_order += "vr-controllers;";
    }

    void apply_timelapse_preference(const pp::app::TimelapsePreferencePlan& plan) override
    {
        timelapse_enabled = plan.enabled;
        timelapse_action = plan.recording_action;
        call_order += "timelapse;";
    }

    void apply_canvas_cursor_mode(const pp::app::StoredIntegerPreferencePlan& plan) override
    {
        cursor_mode = plan.value;
        call_order += "cursor;";
    }

    float ui_scale = 0.0F;
    float ui_font_scale = 0.0F;
    float viewport_scale = 0.0F;
    float viewport_font_scale = 0.0F;
    pp::app::InterfaceDirection interface_direction = pp::app::InterfaceDirection::left_to_right;
    bool vr_mode = false;
    bool vr_mode_result = true;
    bool vr_controllers = false;
    bool timelapse_enabled = false;
    pp::app::TimelapseRecordingAction timelapse_action = pp::app::TimelapseRecordingAction::no_op;
    int cursor_mode = -1;
    std::string call_order;
};

void ui_scale_computes_font_scale_from_display_density(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_ui_scale(1.5F, 2.0F);
    PP_EXPECT(harness, plan.scale == 1.5F);
    PP_EXPECT(harness, plan.display_density == 2.0F);
    PP_EXPECT(harness, plan.font_scale == 3.0F);
}

void scale_option_selection_uses_last_option_not_above_current(pp::tests::Harness& harness)
{
    constexpr std::array<float, 4> options { 0.75F, 1.0F, 1.5F, 2.0F };
    const auto plan = pp::app::plan_scale_option_selection(1.6F, options);
    PP_EXPECT(harness, plan.has_selection);
    PP_EXPECT(harness, plan.index == 2U);
}

void scale_option_selection_handles_empty_or_too_large_options(pp::tests::Harness& harness)
{
    constexpr std::array<float, 0> empty {};
    const auto empty_plan = pp::app::plan_scale_option_selection(1.0F, empty);
    PP_EXPECT(harness, !empty_plan.has_selection);
    PP_EXPECT(harness, empty_plan.index == 0U);

    constexpr std::array<float, 2> options { 2.0F, 3.0F };
    const auto low_plan = pp::app::plan_scale_option_selection(1.0F, options);
    PP_EXPECT(harness, !low_plan.has_selection);
    PP_EXPECT(harness, low_plan.index == 0U);
}

void interface_direction_tracks_requested_layout_direction(pp::tests::Harness& harness)
{
    PP_EXPECT(
        harness,
        pp::app::plan_interface_direction(false).direction == pp::app::InterfaceDirection::left_to_right);
    PP_EXPECT(
        harness,
        pp::app::plan_interface_direction(true).direction == pp::app::InterfaceDirection::right_to_left);
}

void timelapse_preference_starts_and_stops_only_on_state_change(pp::tests::Harness& harness)
{
    PP_EXPECT(
        harness,
        pp::app::plan_timelapse_preference(true, false).recording_action
            == pp::app::TimelapseRecordingAction::start_recording);
    PP_EXPECT(
        harness,
        pp::app::plan_timelapse_preference(false, true).recording_action
            == pp::app::TimelapseRecordingAction::stop_recording);
    PP_EXPECT(
        harness,
        pp::app::plan_timelapse_preference(true, true).recording_action
            == pp::app::TimelapseRecordingAction::no_op);
    PP_EXPECT(
        harness,
        pp::app::plan_timelapse_preference(false, false).recording_action
            == pp::app::TimelapseRecordingAction::no_op);
}

void simple_preferences_preserve_values_for_storage(pp::tests::Harness& harness)
{
    PP_EXPECT(harness, pp::app::plan_vr_mode_preference(true).value);
    PP_EXPECT(harness, !pp::app::plan_vr_mode_preference(false).value);
    PP_EXPECT(harness, pp::app::plan_vr_controllers_preference(true).value);
    PP_EXPECT(harness, !pp::app::plan_vr_controllers_preference(false).value);
    PP_EXPECT(harness, pp::app::plan_canvas_cursor_mode(2).value == 2);
}

void preference_executor_dispatches_side_effect_plans(pp::tests::Harness& harness)
{
    FakeAppPreferenceServices services;

    PP_EXPECT(harness, pp::app::execute_ui_scale_preference(1.5F, 2.0F, services).ok());
    PP_EXPECT(harness, pp::app::execute_viewport_scale_preference(1.25F, 1.0F, services).ok());
    PP_EXPECT(harness, pp::app::execute_interface_direction_preference(true, services).ok());
    PP_EXPECT(harness, pp::app::execute_vr_mode_preference(true, services).ok());
    PP_EXPECT(harness, pp::app::execute_vr_controllers_preference(true, services).ok());
    PP_EXPECT(harness, pp::app::execute_timelapse_preference(true, false, services).ok());
    PP_EXPECT(harness, pp::app::execute_canvas_cursor_mode_preference(2, services).ok());

    PP_EXPECT(harness, services.ui_scale == 1.5F);
    PP_EXPECT(harness, services.ui_font_scale == 3.0F);
    PP_EXPECT(harness, services.viewport_scale == 1.25F);
    PP_EXPECT(harness, services.viewport_font_scale == 1.25F);
    PP_EXPECT(harness, services.interface_direction == pp::app::InterfaceDirection::right_to_left);
    PP_EXPECT(harness, services.vr_mode);
    PP_EXPECT(harness, services.vr_controllers);
    PP_EXPECT(harness, services.timelapse_enabled);
    PP_EXPECT(harness, services.timelapse_action == pp::app::TimelapseRecordingAction::start_recording);
    PP_EXPECT(harness, services.cursor_mode == 2);
    PP_EXPECT(
        harness,
        services.call_order == "ui-scale;viewport-scale;direction;vr-mode;vr-controllers;timelapse;cursor;");
}

void preference_executor_preserves_timelapse_stop_action(pp::tests::Harness& harness)
{
    FakeAppPreferenceServices services;

    PP_EXPECT(harness, pp::app::execute_timelapse_preference(false, true, services).ok());
    PP_EXPECT(harness, !services.timelapse_enabled);
    PP_EXPECT(harness, services.timelapse_action == pp::app::TimelapseRecordingAction::stop_recording);
}

void preference_executor_reports_failed_vr_start(pp::tests::Harness& harness)
{
    FakeAppPreferenceServices services;
    services.vr_mode_result = false;

    const auto status = pp::app::execute_vr_mode_preference(true, services);
    PP_EXPECT(harness, !status.ok());
    PP_EXPECT(harness, status.code == pp::foundation::StatusCode::invalid_argument);
    PP_EXPECT(harness, services.vr_mode);
}

}

int main()
{
    pp::tests::Harness harness;
    harness.run("ui scale computes font scale from display density", ui_scale_computes_font_scale_from_display_density);
    harness.run(
        "scale option selection uses last option not above current",
        scale_option_selection_uses_last_option_not_above_current);
    harness.run(
        "scale option selection handles empty or too large options",
        scale_option_selection_handles_empty_or_too_large_options);
    harness.run("interface direction tracks requested layout direction", interface_direction_tracks_requested_layout_direction);
    harness.run(
        "timelapse preference starts and stops only on state change",
        timelapse_preference_starts_and_stops_only_on_state_change);
    harness.run("simple preferences preserve values for storage", simple_preferences_preserve_values_for_storage);
    harness.run("preference executor dispatches side effect plans", preference_executor_dispatches_side_effect_plans);
    harness.run("preference executor preserves timelapse stop action", preference_executor_preserves_timelapse_stop_action);
    harness.run("preference executor reports failed VR start", preference_executor_reports_failed_vr_start);
    return harness.finish();
}
