#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;

class ModuleHandler{
    class Internal{
    public:
        unordered_map<string, Script*> MODULES;

        Internal(){
            MODULES = {
                {"simple_return", Script::Create(u"return 500")},
                {"simple_variables", Script::Create(u"foo = 50\nbar = \'test\'")}
            };
        }

        ~Internal(){
            for (const auto& v: MODULES){
                delete v.second;
            }
            MODULES.clear();
        }
    };
    static Internal _internal;

    inline static bool DoesModuleExist(const string& moduleName){
        return _internal.MODULES.find(moduleName) != _internal.MODULES.end();
    }

    inline static Script* ResolveModule(const string& moduleName){
        return _internal.MODULES[moduleName];
    }

public:
    static void Initialize(){
        ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(DoesModuleExist);
        ScriptOptions::GetDefaultScriptOptions()->SetResolveModuleFunc(ResolveModule);
    }
};

ModuleHandler::Internal ModuleHandler::_internal;

TEST_CASE( "Require simple return script", "[integration]" ) {
    ModuleHandler::Initialize();
    auto script = Script::Create(uR"(
return require("simple_return")
)");
    REQUIRE(!script->Diagnostics -> HasErrors());
    auto var = script->Evaluate();
    REQUIRE(var->EvaluateInteger() == 500);
    delete script;
}

TEST_CASE( "Require simple variables script", "[integration]" ) {
    ModuleHandler::Initialize();
    auto script = Script::Create(uR"(
require("simple_variables")
)");
    REQUIRE(!script->Diagnostics -> HasErrors());
    script->Evaluate();
    REQUIRE(script->HasVariable(u"foo"));
    REQUIRE(script->HasVariable(u"bar"));
    auto foo = script->GetVariable(u"foo");
    auto bar = script->GetVariable(u"bar");
    CHECK(foo->EvaluateInteger() == 50);
    CHECK(bar->EvaluateString() == u"test");

    delete foo;
    delete bar;
    delete script;
}

#endif