#include "DeerStudio/EditorEngine.h"
#include "DeerStudio/EditorEngine/ErrorHandle.h"

#include "Deer/Path.h"
#include "Deer/DataStore.h"

#include "angelscript.h"
#include "scriptbuilder.h"

#include <iostream>
#include <filesystem>
#include <vector>
#include <string>

namespace fs = std::filesystem;

namespace Deer {
    void EditorEngine::loadScripts() {
        Path path = DataStore::rootPath / DEER_EDITOR_PATH;
        CScriptBuilder builder;

        AS_RET_CHECK(builder.StartNewModule(scriptEngine, "DeerModule"));

        DEER_CORE_TRACE("Extracting UI Engine Scripts ");
        for (const auto& entry : fs::recursive_directory_iterator(path)) {
            if (entry.is_regular_file() && entry.path().extension() == ".as") {
                //DEER_UI_ENGINE_TRACE("\t{0}", entry.path().stem().string().c_str());
                // We add aditional info to check who caused the error
                AS_CHECK_ADDITIONAL_INFO(
                    builder.AddSectionFromFile(entry.path().string().c_str());,
                    entry.path().string().c_str()
                );
            }
        }

        AS_RET_CHECK(builder.BuildModule());
    }
}