#include "GUI.h"
#include "gl3w.h"
#include <iostream>

#define WIDTH   500
#define HEIGHT  500

namespace jes
{
    bool GUI::load()
    {
        if (SDL_Init(SDL_INIT_VIDEO))
        {
            std::cerr << "Failed to initialize SDL!" << std::endl;
            return false;
        }

        atexit(SDL_Quit);

        m_window = SDL_CreateWindow("jes",
                                    SDL_WINDOWPOS_UNDEFINED,
                                    SDL_WINDOWPOS_UNDEFINED,
                                    WIDTH,
                                    HEIGHT,
                                    SDL_WINDOW_OPENGL);
        if (!m_window)
        {
            std::cerr << "Failed to create window!" << std::endl;
            SDL_Quit();
            return false;
        }

        (void)SDL_GL_CreateContext(m_window);

        if (gl3wInit() != 0)
        {
            std::cerr << "Failed to gl3wInit()" << std::endl;
            SDL_Quit();
            return false;
        }

        if (!gl3wIsSupported(3, 0))
        {
            std::cerr << "OpenGL 3.0 is not supported!" << std::endl;
            SDL_Quit();
            return false;
        }

        glClearColor (0.0, 0.0, 0.0, 0.0);

        m_font_manager = new FontManager();

        if (!m_font_manager->load())
            return false;

        return true;
    }

    int GUI::run()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        SDL_GL_SwapWindow(m_window);

        SDL_Event event;
        while (SDL_WaitEvent(&event))
        {
            if (event.type == SDL_QUIT)
                break;
            else if (event.type == SDL_KEYDOWN)
            {
                if (event.key.keysym.sym == SDLK_ESCAPE)
                    break;
            }
        }

        return 0;
    }
}
