#include "ResourceManager.hpp"
#include <iterator>
#include <iostream>
#include <unordered_map>

#if defined(LINUX) || defined(MINGW)
    #include <SDL2/SDL.h>
#else // This works for Mac
    #include <SDL.h>
#endif

ResourceManager::ResourceManager(){}
ResourceManager::ResourceManager(ResourceManager const&){}
void ResourceManager::operator=(ResourceManager const& rhs){}

ResourceManager &ResourceManager::GetInstance()
{
    static ResourceManager* instance = new ResourceManager();
    return *instance;
}

SDL_Surface* ResourceManager::LoadResource(std::string image_filename){
    auto search = surfaces.find(image_filename);
    if(search != surfaces.end()){
        return GetResource(image_filename);
    } else {
        SDL_Surface* surface = SDL_LoadBMP(image_filename.c_str());
        surfaces.insert(std::make_pair(image_filename, surface));
        return surfaces[image_filename];
    }
    return nullptr;
}

// TODO SPRINT 1 (April 5): Create LoadResource overload for audio files

SDL_Surface* ResourceManager::GetResource(std::string key){
    return surfaces[key];
}

int ResourceManager::ShutDown() {
    for(auto surface : surfaces) {
        SDL_FreeSurface(surface.second);
    }
    return 0;
}