#include "RHI/OpenglDrv/GLHandleMapper.h"

#include <unordered_map>

static uint32 s_vaoHandle = 1;
static std::unordered_map<uint32, uint32> s_vaoHandleMapper;

static uint32 s_vboHandle = 1;
static std::unordered_map<uint32, uint32> s_vboHandleMapper;

static uint32 s_eboHandle = 1;
static std::unordered_map<uint32, uint32> s_eboHandleMapper;

static uint32 s_shaderHandle = 1;
static std::unordered_map<uint32, uint32> s_shaderHandleMapper;

static uint32 s_programHandle = 1;
static std::unordered_map<uint32, uint32> s_programHandleMapper;

uint32 GLHandleMapper::GeneratorVAOHandle() {
    return s_vaoHandle++;
}

uint32 GLHandleMapper::GeneratorVBOHandle() {
    return s_vboHandle++;
}

uint32 GLHandleMapper::GeneratorEBOHandle() {
    return s_eboHandle++;
}

uint32 GLHandleMapper::GeneratorShaderHandle() {
    return s_shaderHandle++;
}

uint32 GLHandleMapper::GeneratorProgramHandle() {
    return s_programHandle++;
}


void GLHandleMapper::MapVAOHandle(uint32 vaoHandle, uint32 vao) {
    s_vaoHandleMapper.insert(std::make_pair(vaoHandle, vao));
}

uint32 GLHandleMapper::GetVAO(uint32 vaoHandle) {
    if (s_vaoHandleMapper.find(vaoHandle) != s_vaoHandleMapper.end()) {
        return s_vaoHandleMapper[vaoHandle];
    }

    return 0;
}

void GLHandleMapper::UnmapVAOHandle(uint32 vaoHandle) {
    s_vaoHandleMapper.erase(vaoHandle);
}

void GLHandleMapper::MapVBOHandle(uint32 vboHandle, uint32 vbo) {
    s_vboHandleMapper.insert(std::make_pair(vboHandle, vbo));
}

uint32 GLHandleMapper::GetVBO(uint32 vboHandle) {
    if (s_vboHandleMapper.find(vboHandle) != s_vboHandleMapper.end()) {
        return s_vboHandleMapper[vboHandle];
    }

    return 0;
}

void GLHandleMapper::UnmapVBOHandle(uint32 vboHandle) {
    s_vboHandleMapper.erase(vboHandle);
}

void GLHandleMapper::MapEBOHandle(uint32 eboHandle, uint32 ebo) {
    s_eboHandleMapper.insert(std::make_pair(eboHandle, ebo));
}
uint32 GLHandleMapper::GetEBO(uint32 eboHandle) {
    if (s_eboHandleMapper.find(eboHandle) != s_eboHandleMapper.end()) {
        return s_eboHandleMapper[eboHandle];
    }

    return 0;
}
void GLHandleMapper::UnmapEBOHandle(uint32 eboHandle) {
    s_eboHandleMapper.erase(eboHandle);
}


void GLHandleMapper::MapShaderHandle(uint32 shaderHandle, uint32 shader) {
    s_shaderHandleMapper.insert(std::make_pair(shaderHandle, shader));
}
uint32 GLHandleMapper::GetShader(uint32 shaderHandle) {
    if (s_shaderHandleMapper.find(shaderHandle) != s_shaderHandleMapper.end()) {
        return s_shaderHandleMapper[shaderHandle];
    }

    return 0;
}
void GLHandleMapper::UnmapShaderHandle(uint32 shaderHandle) {
    s_shaderHandleMapper.erase(shaderHandle);
}

void GLHandleMapper::MapProgramHandle(uint32 programHandle, uint32 program) {
    s_programHandleMapper.insert(std::make_pair(programHandle, program));
}
uint32 GLHandleMapper::GetProgram(uint32 programHandle) {
    if (s_programHandleMapper.find(programHandle) != s_programHandleMapper.end()) {
        return s_programHandleMapper[programHandle];
    }

    return 0;
}
void GLHandleMapper::UnmapProgramHandle(uint32 programHandle) {
    s_programHandleMapper.erase(programHandle);
}
