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

#include <limits>
#include <string>

namespace {

class FakeDocumentCanvasClearServices final : public pp::app::DocumentCanvasClearServices {
public:
    void clear_current_canvas(float r, float g, float b, float a) override
    {
        clear_calls += 1;
        last_r = r;
        last_g = g;
        last_b = b;
        last_a = a;
        call_order += "clear;";
    }

    int clear_calls = 0;
    float last_r = 0.0F;
    float last_g = 0.0F;
    float last_b = 0.0F;
    float last_a = 0.0F;
    std::string call_order;
};

void clear_plan_records_legacy_canvas_effects(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_document_canvas_clear(true, 0.0F, 0.1F, 0.2F, 0.3F);
    PP_EXPECT(harness, plan);
    if (plan) {
        PP_EXPECT(harness, plan.value().clears_canvas);
        PP_EXPECT(harness, plan.value().records_undo);
        PP_EXPECT(harness, plan.value().marks_unsaved);
        PP_EXPECT(harness, !plan.value().no_op);
        PP_EXPECT(harness, plan.value().r == 0.0F);
        PP_EXPECT(harness, plan.value().g == 0.1F);
        PP_EXPECT(harness, plan.value().b == 0.2F);
        PP_EXPECT(harness, plan.value().a == 0.3F);
    }
}

void clear_plan_noops_without_canvas(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_document_canvas_clear(false);
    PP_EXPECT(harness, plan);
    if (plan) {
        PP_EXPECT(harness, !plan.value().clears_canvas);
        PP_EXPECT(harness, !plan.value().records_undo);
        PP_EXPECT(harness, !plan.value().marks_unsaved);
        PP_EXPECT(harness, plan.value().no_op);
    }
}

void clear_plan_rejects_bad_color_channels(pp::tests::Harness& harness)
{
    PP_EXPECT(harness, !pp::app::plan_document_canvas_clear(true, -0.01F, 0.0F, 0.0F, 0.0F));
    PP_EXPECT(harness, !pp::app::plan_document_canvas_clear(true, 0.0F, 1.01F, 0.0F, 0.0F));
    PP_EXPECT(harness, !pp::app::plan_document_canvas_clear(
        true,
        0.0F,
        0.0F,
        std::numeric_limits<float>::infinity(),
        0.0F));
}

void clear_executor_dispatches_color_to_service(pp::tests::Harness& harness)
{
    FakeDocumentCanvasClearServices services;
    const auto plan = pp::app::plan_document_canvas_clear(true, 0.25F, 0.5F, 0.75F, 1.0F);
    PP_EXPECT(harness, plan);
    if (plan) {
        PP_EXPECT(harness, pp::app::execute_document_canvas_clear_plan(plan.value(), services).ok());
    }

    PP_EXPECT(harness, services.clear_calls == 1);
    PP_EXPECT(harness, services.last_r == 0.25F);
    PP_EXPECT(harness, services.last_g == 0.5F);
    PP_EXPECT(harness, services.last_b == 0.75F);
    PP_EXPECT(harness, services.last_a == 1.0F);
    PP_EXPECT(harness, services.call_order == "clear;");
}

void clear_executor_preserves_noop_without_canvas(pp::tests::Harness& harness)
{
    FakeDocumentCanvasClearServices services;
    const auto plan = pp::app::plan_document_canvas_clear(false);
    PP_EXPECT(harness, plan);
    if (plan) {
        PP_EXPECT(harness, pp::app::execute_document_canvas_clear_plan(plan.value(), services).ok());
    }

    PP_EXPECT(harness, services.clear_calls == 0);
}

void clear_executor_rejects_invalid_color(pp::tests::Harness& harness)
{
    FakeDocumentCanvasClearServices services;
    pp::app::DocumentCanvasClearPlan plan;
    plan.clears_canvas = true;
    plan.records_undo = true;
    plan.marks_unsaved = true;
    plan.no_op = false;
    plan.r = 0.0F;
    plan.g = 0.0F;
    plan.b = 1.5F;
    plan.a = 0.0F;

    const auto status = pp::app::execute_document_canvas_clear_plan(plan, services);
    PP_EXPECT(harness, !status.ok());
    PP_EXPECT(harness, status.code == pp::foundation::StatusCode::out_of_range);
    PP_EXPECT(harness, services.clear_calls == 0);
}

} // namespace

int main()
{
    pp::tests::Harness harness;
    harness.run("clear plan records legacy canvas effects", clear_plan_records_legacy_canvas_effects);
    harness.run("clear plan noops without canvas", clear_plan_noops_without_canvas);
    harness.run("clear plan rejects bad color channels", clear_plan_rejects_bad_color_channels);
    harness.run("clear executor dispatches color to service", clear_executor_dispatches_color_to_service);
    harness.run("clear executor preserves noop without canvas", clear_executor_preserves_noop_without_canvas);
    harness.run("clear executor rejects invalid color", clear_executor_rejects_invalid_color);
    return harness.finish();
}
