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

#include <string>
#include <string_view>

namespace {

class FakeCommandConvertServices final : public pp::app::CommandConvertServices {
public:
    void apply_renderer_state() override
    {
        call_order += "state;";
    }

    void create_canvas(int canvas_resolution) override
    {
        created_canvas_resolution = canvas_resolution;
        call_order += "canvas;";
    }

    void open_project(std::string_view project_path) override
    {
        opened_project = std::string(project_path);
        call_order += "open;";
    }

    void export_equirectangular(std::string_view output_path) override
    {
        exported_output = std::string(output_path);
        call_order += "export;";
    }

    int created_canvas_resolution = 0;
    std::string opened_project;
    std::string exported_output;
    std::string call_order;
};

void convert_plan_preserves_legacy_step_order(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_command_convert("D:/Paint/demo.ppi", "D:/Paint/demo.png", 2048);

    PP_EXPECT(harness, plan);
    if (!plan) {
        return;
    }

    PP_EXPECT(harness, plan.value().project_path == "D:/Paint/demo.ppi");
    PP_EXPECT(harness, plan.value().output_path == "D:/Paint/demo.png");
    PP_EXPECT(harness, plan.value().canvas_resolution == 2048);
    PP_EXPECT(harness, plan.value().steps.size() == 4);
    PP_EXPECT(harness, plan.value().steps[0] == pp::app::CommandConvertStep::apply_renderer_state);
    PP_EXPECT(harness, plan.value().steps[1] == pp::app::CommandConvertStep::create_canvas);
    PP_EXPECT(harness, plan.value().steps[2] == pp::app::CommandConvertStep::open_project);
    PP_EXPECT(harness, plan.value().steps[3] == pp::app::CommandConvertStep::export_equirectangular);
}

void convert_plan_rejects_missing_inputs(pp::tests::Harness& harness)
{
    const auto missing_project = pp::app::plan_command_convert("", "D:/Paint/demo.png", 2048);
    const auto missing_output = pp::app::plan_command_convert("D:/Paint/demo.ppi", "", 2048);
    const auto bad_resolution = pp::app::plan_command_convert("D:/Paint/demo.ppi", "D:/Paint/demo.png", 0);

    PP_EXPECT(harness, !missing_project);
    PP_EXPECT(harness, missing_project.status().code == pp::foundation::StatusCode::invalid_argument);
    PP_EXPECT(harness, !missing_output);
    PP_EXPECT(harness, missing_output.status().code == pp::foundation::StatusCode::invalid_argument);
    PP_EXPECT(harness, !bad_resolution);
    PP_EXPECT(harness, bad_resolution.status().code == pp::foundation::StatusCode::invalid_argument);
}

void convert_executor_dispatches_valid_plan(pp::tests::Harness& harness)
{
    const auto plan = pp::app::plan_command_convert("D:/Paint/demo.ppi", "D:/Paint/demo.png", 1024);
    FakeCommandConvertServices services;

    PP_EXPECT(harness, plan);
    PP_EXPECT(harness, pp::app::execute_command_convert_plan(plan.value(), services).ok());
    PP_EXPECT(harness, services.created_canvas_resolution == 1024);
    PP_EXPECT(harness, services.opened_project == "D:/Paint/demo.ppi");
    PP_EXPECT(harness, services.exported_output == "D:/Paint/demo.png");
    PP_EXPECT(harness, services.call_order == "state;canvas;open;export;");
}

void convert_executor_rejects_malformed_plan(pp::tests::Harness& harness)
{
    FakeCommandConvertServices services;
    PP_EXPECT(harness, !pp::app::execute_command_convert_plan(pp::app::CommandConvertPlan {}, services).ok());
    PP_EXPECT(harness, services.call_order.empty());
}

} // namespace

int main()
{
    pp::tests::Harness harness;
    harness.run("convert plan preserves legacy step order", convert_plan_preserves_legacy_step_order);
    harness.run("convert plan rejects missing inputs", convert_plan_rejects_missing_inputs);
    harness.run("convert executor dispatches valid plan", convert_executor_dispatches_valid_plan);
    harness.run("convert executor rejects malformed plan", convert_executor_rejects_malformed_plan);
    return harness.finish();
}
