#include "FontsManager.h"

#ifdef _WIN32
    #include "DirectWriteFontEngine.h"
    #include "DirectWriteHelpers.h"
#else
    #ifdef HAS_FREETYPE
    #include "FreeTypeFontEngine.h"
    #else
    #include "BasicFontEngine.h"
    #endif
#endif

#include "FontGlyph.h"

FontsManager::FontsManager()
{
#ifdef _WIN32
    mFontEngine = std::make_unique<DirectWriteFontEngine>();
#else
    #ifdef HAS_FREETYPE
        mFontEngine = std::make_unique<FreeTypeFontEngine>();
        mUsesGlyphs = true;
    #else
        mFontEngine = std::make_unique<BasicFontEngine>();
    #endif
#endif
    mFontEngine->initialize();
}

std::unique_ptr<FontsManager> FontsManager::Create()
{
    return std::make_unique<FontsManager>();
}

bool FontsManager::usesGlyphs() const
{
    return mUsesGlyphs;
}

IFontEngine* FontsManager::getFontEngine() const
{
    return mFontEngine.get();
}

FontGlyph* FontsManager::getGlyph(const std::string&, float size, uint32_t c)
{
    auto path = std::filesystem::path("truetype/liberation/LiberationSans-Regular.ttf");

    mFontEngine->loadFontFace(path, size);

    auto iter = mGlyphs.find(c);
    if(iter != mGlyphs.end())
    {
        return iter->second.get();
    }
    else
    {
        auto glyph = mFontEngine->loadGlyph(c);
        auto glyph_ptr = glyph.get();
        mGlyphs[c] = std::move(glyph);
        return glyph_ptr;
    }
}


