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

#include <limits>

namespace {

void recording_start_only_starts_when_not_running(pp::tests::Harness& harness)
{
    PP_EXPECT(
        harness,
        pp::app::plan_recording_start(false) == pp::app::RecordingStartAction::start_thread);
    PP_EXPECT(
        harness,
        pp::app::plan_recording_start(true) == pp::app::RecordingStartAction::no_op_already_running);
}

void recording_stop_only_stops_when_running(pp::tests::Harness& harness)
{
    PP_EXPECT(
        harness,
        pp::app::plan_recording_stop(true) == pp::app::RecordingStopAction::stop_thread);
    PP_EXPECT(
        harness,
        pp::app::plan_recording_stop(false) == pp::app::RecordingStopAction::no_op_not_running);
}

void recording_clear_resets_frames_and_preserves_platform_delete_flag(pp::tests::Harness& harness)
{
    const auto running_desktop = pp::app::plan_recording_clear(true, false);
    PP_EXPECT(harness, running_desktop.stop_running_recording);
    PP_EXPECT(harness, !running_desktop.delete_recorded_files);
    PP_EXPECT(harness, running_desktop.frame_count_after_clear == 0);

    const auto stopped_apple = pp::app::plan_recording_clear(false, true);
    PP_EXPECT(harness, !stopped_apple.stop_running_recording);
    PP_EXPECT(harness, stopped_apple.delete_recorded_files);
    PP_EXPECT(harness, stopped_apple.frame_count_after_clear == 0);
}

void recording_export_tracks_frame_count(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_recording_export(120);
    PP_EXPECT(harness, plan.frame_count == 120);
    PP_EXPECT(harness, plan.progress_total == 120);
}

void recording_export_clamps_progress_total(pp::tests::Harness& harness)
{
    const auto too_many_frames = static_cast<std::size_t>(std::numeric_limits<int>::max()) + 1U;
    const auto plan = pp::app::plan_recording_export(too_many_frames);
    PP_EXPECT(harness, plan.frame_count == too_many_frames);
    PP_EXPECT(harness, plan.progress_total == std::numeric_limits<int>::max());
}

}

int main()
{
    pp::tests::Harness harness;
    harness.run("recording start only starts when not running", recording_start_only_starts_when_not_running);
    harness.run("recording stop only stops when running", recording_stop_only_stops_when_running);
    harness.run(
        "recording clear resets frames and preserves platform delete flag",
        recording_clear_resets_frames_and_preserves_platform_delete_flag);
    harness.run("recording export tracks frame count", recording_export_tracks_frame_count);
    harness.run("recording export clamps progress total", recording_export_clamps_progress_total);
    return harness.finish();
}
