/*******************************************************************
** This code is part of Breakout.
**
** Breakout is free software: you can redistribute it and/or modify
** it under the terms of the CC BY 4.0 license as published by
** Creative Commons, either version 4 of the License, or (at your
** option) any later version.
******************************************************************/
#ifndef RESOURCE_MANAGER_H
#define RESOURCE_MANAGER_H

#include <map>
#include <string>

#include <glad/glad.h>

#include "graphics/texture2d.hpp"
#include "graphics/shader.hpp"
#include "graphics/sprite_renderer.hpp"
#include "graphics/text_renderer.hpp"

namespace bookr {

// A static singleton ResourceManager class that hosts several
// functions to load Textures and Shaders. Each loaded texture
// and/or shader is also stored for future reference by string
// handles. All functions and resources are static and no 
// public constructor is defined.
class ResourceManager
{
public:
    // Resource storage
    static std::map<std::string, Shader>    Shaders;
    static std::map<std::string, Texture2D> Textures;
    static SpriteRenderer *sprite_renderer;
    static TextRenderer  *ui_text_renderer;
    // Loads (and generates) a shader program from file loading vertex, fragment (and geometry) shader's source code. If gShaderFile is not nullptr, it also loads a geometry shader
    static Shader   LoadShader(const GLchar *vShaderFile, const GLchar *fShaderFile, const GLchar *gShaderFile, std::string name,
        bool fromFile = true, const int vertLength = NULL, const int fragLength = NULL);
    // Retrieves a stored sader
    static Shader   GetShader(std::string name);
    // Loads (and generates) a texture from file
    static Texture2D LoadTexture(const GLchar *file, GLboolean alpha, std::string name, bool fromFile = true, int size = NULL);
    // Retrieves a stored texture
    static Texture2D GetTexture(std::string name);
    // Properly de-allocates all loaded resources
    static void      Clear();

    static SpriteRenderer* getSpriteRenderer();
    static SpriteRenderer* CreateSpriteRenderer(Shader shader);

    static TextRenderer* getTextRenderer();
    static bool setTextRenderer(TextRenderer* default_text_renderer);
    static TextRenderer* CreateTextRenderer(int width, int height);
private:
    // Private constructor, that is we do not want any actual resource manager objects. Its members and functions should be publicly available (static).
    ResourceManager() { }
    // Loads and generates a shader from file
    static Shader    loadShaderFromFile(const GLchar *vShaderFile, const GLchar *fShaderFile, const GLchar *gShaderFile = nullptr);
    static Shader    loadShaderFromBuffer(const GLchar *vertShaderBuffer, const GLchar *fragShaderBuffer, const GLchar *geoShaderBuffer = nullptr,
        const int vertLength = NULL, const int fragLength = NULL);
    // Loads a single texture from file
    static Texture2D loadTextureFromFile(const GLchar *file, GLboolean alpha);
    static Texture2D loadTextureFromBuffer(const GLchar *file, GLboolean alpha, int size = NULL);
};

}

#endif
