/******************************************************************************
*	File		-	coreAPI.cpp
*	Author		-	Joey Pollack
*	Date		-	2021/09/23 (y/m/d)
*	Mod Date	-	2021/09/23 (y/m/d)
*	Description	-	class with all static methods that expose core functionality to LUA
*                   Need an init function that takes a reference to a LUA state so that it
*                   can expose the static functions to LUA.
******************************************************************************/

#include "coreAPI.h"
#include "script_manager.h"
#include <core/core.h>
#include <utils/logger.h>

namespace lunarium
{
    
    CoreAPI* CoreAPI::mpInstance = nullptr;
    CoreAPI& CoreAPI::GetInstance()
    {
        if (mpInstance == nullptr)
        {
            mpInstance = new CoreAPI;
        }

        return *mpInstance;
    }

    void CoreAPI::FreeInstance()
    {
        delete mpInstance;
        mpInstance = nullptr;
    }

    OpRes CoreAPI::Initialize(ScriptManager& sman)
    {
        mCat = sman.mCat;

        // Register methods
        sman.mState["SetWindowSize"] = &CoreAPI::SetWindowSize;
        sman.mState["Log"] = &CoreAPI::Log;

       // return OpRes::Fail("CoreAPI::Initialize not implemented yet!");
       return OpRes::OK();
    }

    ////////////////////////////////////////////////////////////
    //				API
    ////////////////////////////////////////////////////////////

    void CoreAPI::SetWindowSize(int w, int h)
    {
        Core::MainWindow().Resize(w, h);
    }

    void CoreAPI::Log(int level, const char* msg)
    {
        switch (level)
        {
            case LogLevel::FATAL_ERROR: { Logger::Fatal(mpInstance->mCat, msg); break; }
            case LogLevel::ERROR:       { Logger::Error(mpInstance->mCat, msg); break; }
            case LogLevel::WARNING:     { Logger::Warn(mpInstance->mCat, msg); break; }
            case LogLevel::INFO:        { Logger::Info(mpInstance->mCat, msg); break; }
            case LogLevel::DEBUG:       { Logger::Debug(mpInstance->mCat, msg); break; }
            default:                    { Logger::Trace(mpInstance->mCat, msg); break; }
        }
    }
}