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

#include <string>
#include <string_view>

namespace {

class FakeBrushPackageImportServices final : public pp::app::BrushPackageImportServices {
public:
    void import_brush_package(pp::app::BrushPackageImportKind kind, std::string_view path) override
    {
        imports += 1;
        last_kind = kind;
        last_path = std::string(path);
    }

    int imports = 0;
    pp::app::BrushPackageImportKind last_kind = pp::app::BrushPackageImportKind::abr;
    std::string last_path;
};

void names_import_kinds(pp::tests::Harness& harness)
{
    PP_EXPECT(harness, std::string_view(pp::app::brush_package_import_kind_name(pp::app::BrushPackageImportKind::abr)) == "abr");
    PP_EXPECT(harness, std::string_view(pp::app::brush_package_import_kind_name(pp::app::BrushPackageImportKind::ppbr)) == "ppbr");
}

void executor_dispatches_abr_and_ppbr(pp::tests::Harness& harness)
{
    FakeBrushPackageImportServices services;

    PP_EXPECT(
        harness,
        pp::app::execute_brush_package_import(
            pp::app::BrushPackageImportKind::abr,
            "D:/Paint/Brushes/clouds.abr",
            services).ok());
    PP_EXPECT(harness, services.imports == 1);
    PP_EXPECT(harness, services.last_kind == pp::app::BrushPackageImportKind::abr);
    PP_EXPECT(harness, services.last_path == "D:/Paint/Brushes/clouds.abr");

    PP_EXPECT(
        harness,
        pp::app::execute_brush_package_import(
            pp::app::BrushPackageImportKind::ppbr,
            "D:/Paint/Brushes/clouds.ppbr",
            services).ok());
    PP_EXPECT(harness, services.imports == 2);
    PP_EXPECT(harness, services.last_kind == pp::app::BrushPackageImportKind::ppbr);
    PP_EXPECT(harness, services.last_path == "D:/Paint/Brushes/clouds.ppbr");
}

void executor_rejects_empty_paths_before_dispatch(pp::tests::Harness& harness)
{
    FakeBrushPackageImportServices services;

    PP_EXPECT(
        harness,
        !pp::app::execute_brush_package_import(pp::app::BrushPackageImportKind::abr, "", services).ok());
    PP_EXPECT(harness, services.imports == 0);
}

} // namespace

int main()
{
    pp::tests::Harness harness;
    harness.run("names import kinds", names_import_kinds);
    harness.run("executor dispatches abr and ppbr", executor_dispatches_abr_and_ppbr);
    harness.run("executor rejects empty paths before dispatch", executor_rejects_empty_paths_before_dispatch);
    return harness.finish();
}
