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

namespace {

void cloud_upload_is_unavailable_without_canvas(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_cloud_upload(false, false, false);
    PP_EXPECT(harness, plan.action == pp::app::CloudUploadAction::unavailable_no_canvas);
    PP_EXPECT(harness, !plan.save_before_upload);
}

void cloud_upload_warns_for_new_documents(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_cloud_upload(true, true, true);
    PP_EXPECT(harness, plan.action == pp::app::CloudUploadAction::show_save_required_warning);
    PP_EXPECT(harness, !plan.save_before_upload);
}

void cloud_upload_prompts_for_clean_existing_documents(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_cloud_upload(true, false, false);
    PP_EXPECT(harness, plan.action == pp::app::CloudUploadAction::prompt_publish);
    PP_EXPECT(harness, !plan.save_before_upload);
}

void cloud_upload_records_save_before_upload_for_dirty_existing_documents(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_cloud_upload(true, false, true);
    PP_EXPECT(harness, plan.action == pp::app::CloudUploadAction::prompt_publish);
    PP_EXPECT(harness, plan.save_before_upload);
}

void cloud_browse_is_unavailable_without_canvas(pp::tests::Harness& harness)
{
    PP_EXPECT(harness, pp::app::plan_cloud_browse(false) == pp::app::CloudBrowseAction::unavailable_no_canvas);
}

void cloud_browse_shows_browser_with_canvas(pp::tests::Harness& harness)
{
    PP_EXPECT(harness, pp::app::plan_cloud_browse(true) == pp::app::CloudBrowseAction::show_browser);
}

void cloud_download_selection_waits_for_empty_file(pp::tests::Harness& harness)
{
    PP_EXPECT(
        harness,
        pp::app::plan_cloud_download_selection("") == pp::app::CloudDownloadSelectionAction::wait_for_selection);
}

void cloud_download_selection_starts_for_selected_file(pp::tests::Harness& harness)
{
    PP_EXPECT(
        harness,
        pp::app::plan_cloud_download_selection("demo.ppi") == pp::app::CloudDownloadSelectionAction::start_download);
}

void cloud_bulk_upload_shows_progress_when_ui_available(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_cloud_bulk_upload(3, true);
    PP_EXPECT(harness, plan.file_count == 3);
    PP_EXPECT(harness, plan.progress_total == 3);
    PP_EXPECT(harness, plan.show_progress);
}

void cloud_bulk_upload_runs_without_progress_when_ui_unavailable(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_cloud_bulk_upload(3, false);
    PP_EXPECT(harness, plan.file_count == 3);
    PP_EXPECT(harness, plan.progress_total == 3);
    PP_EXPECT(harness, !plan.show_progress);
}

void cloud_bulk_upload_keeps_zero_file_progress_explicit(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_cloud_bulk_upload(0, true);
    PP_EXPECT(harness, plan.file_count == 0);
    PP_EXPECT(harness, plan.progress_total == 0);
    PP_EXPECT(harness, plan.show_progress);
}

void cloud_bulk_upload_clamps_progress_total(pp::tests::Harness& harness)
{
    const auto too_many_files = static_cast<std::size_t>(std::numeric_limits<int>::max()) + 1U;
    const auto plan = pp::app::plan_cloud_bulk_upload(too_many_files, true);
    PP_EXPECT(harness, plan.file_count == too_many_files);
    PP_EXPECT(harness, plan.progress_total == std::numeric_limits<int>::max());
    PP_EXPECT(harness, plan.show_progress);
}

}

int main()
{
    pp::tests::Harness harness;
    harness.run("cloud upload is unavailable without canvas", cloud_upload_is_unavailable_without_canvas);
    harness.run("cloud upload warns for new documents", cloud_upload_warns_for_new_documents);
    harness.run("cloud upload prompts for clean existing documents", cloud_upload_prompts_for_clean_existing_documents);
    harness.run("cloud upload records save before upload for dirty existing documents", cloud_upload_records_save_before_upload_for_dirty_existing_documents);
    harness.run("cloud browse is unavailable without canvas", cloud_browse_is_unavailable_without_canvas);
    harness.run("cloud browse shows browser with canvas", cloud_browse_shows_browser_with_canvas);
    harness.run("cloud download selection waits for empty file", cloud_download_selection_waits_for_empty_file);
    harness.run("cloud download selection starts for selected file", cloud_download_selection_starts_for_selected_file);
    harness.run("cloud bulk upload shows progress when ui available", cloud_bulk_upload_shows_progress_when_ui_available);
    harness.run("cloud bulk upload runs without progress when ui unavailable", cloud_bulk_upload_runs_without_progress_when_ui_unavailable);
    harness.run("cloud bulk upload keeps zero file progress explicit", cloud_bulk_upload_keeps_zero_file_progress_explicit);
    harness.run("cloud bulk upload clamps progress total", cloud_bulk_upload_clamps_progress_total);
    return harness.finish();
}
