#pragma once

#include "IFontEngine.h"

#include <iostream>

#include <ft2build.h>
#include FT_FREETYPE_H

class FreeTypeFontEngine : public IFontEngine
{
public:
    FreeTypeFontEngine() = default;

    void run()
    {
        FT_Library  library;
        auto error = FT_Init_FreeType( &library );
        if ( error )
        {
            std::cout << "Error initializing ft" << std::endl;
        }

        FT_Face face;
        error = FT_New_Face( library,
                             "/usr/share/fonts/truetype/tlwg/TlwgTypo-Bold.ttf",
                             0,
                             &face );
        if ( error == FT_Err_Unknown_File_Format )
        {
            std::cout << "Found file but format not supported" << std::endl;
        }
        else if ( error )
        {
            std::cout << "Failed to find or open file" << std::endl;
        }

        auto charcode = 65;
        auto glyph_index = FT_Get_Char_Index( face, charcode );
        if (glyph_index == 0)
        {
            std::cout << "Got the null glyph" << std::endl;
        }

        error = FT_Load_Glyph(
                  face,          /* handle to face object */
                  glyph_index,   /* glyph index           */
                  FT_LOAD_DEFAULT );

        if (face->glyph->format == FT_GLYPH_FORMAT_BITMAP)
        {
            std::cout << "It is bitmap format" << std::endl;
        }
        else
        {
            error = FT_Render_Glyph( face->glyph,   /* glyph slot  */
                    FT_RENDER_MODE_NORMAL );
        }

        auto rows = face->glyph->bitmap.rows;
        auto columns = face->glyph->bitmap.width;
        std::cout << "We have a bitmap with rows " << rows << " and columns " << columns << std::endl;
    }
};
