//
// Created by m on 12/3/21.
//

#ifndef RLA_IIPP_SDLTEXTUREMANAGER_HPP
#define RLA_IIPP_SDLTEXTUREMANAGER_HPP
#include <SDL.h>
#include <string>
#include <unordered_map>
#include "../../engine.hpp"
#include "../../resources/resourcemanager.hpp"
#include <SDL_image.h>
#include <algorithm>
#include <SDL_ttf.h>

struct TextureManagerFetchArgs {
    std::string path;
    std::optional<Color> color_key;

};

class SdlTextureManager : public ResourceManager<std::shared_ptr<SDL_Texture>, std::string, TextureManagerFetchArgs> {
protected:
    std::shared_ptr<SDL_Texture> load_resource(TextureManagerFetchArgs args) override;
    std::string get_key(TextureManagerFetchArgs args) override;

public:
    explicit SdlTextureManager(std::shared_ptr<SDL_Renderer> renderer) : renderer(renderer) {}
    std::shared_ptr<SDL_Texture> load_text_as_texture(const std::string& text, const std::shared_ptr<TTF_Font>& font, Color color);


private:
    std::shared_ptr<SDL_Renderer> renderer = nullptr;
    static SDL_Surface* load_surface(const std::string& path);
};

class MissingTextureException: public std::exception {
private:
    std::string msg;
public:
    [[nodiscard]] const char *what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override {
        return msg.c_str();
    }

    MissingTextureException(const std::string& path, const std::string& error) {
        msg = fmt::format("Failed ot load texture at path [{}] because: {}", path, error);
    }
};


#endif //RLA_IIPP_SDLTEXTUREMANAGER_HPP
