#include "Renderer/MeshRender.h"

#include "RHI/OpenglDrv/GLHandleMapper.h"

MeshRender::MeshRender(Mesh* mesh)
    : mesh_(mesh) {
    RHI* rhi = RHI::Get();
    rhiApi_ = rhi->GetRHIAPI();
}

MeshRender::~MeshRender() {
    if (0 == vaoHandle_) {
        return;
    }
    auto task = rhiApi_->DeleteVAOCommand(vaoHandle_, vboHandle_, eboHandle_);
    RHI::Get()->Push(task);
}

void MeshRender::Render() {
    if (nullptr == mesh_) {
        return;
    }

    if (0 == vaoHandle_) {
        vaoHandle_ = GLHandleMapper::GeneratorVAOHandle();
        vboHandle_ = GLHandleMapper::GeneratorVBOHandle();
        eboHandle_ = GLHandleMapper::GeneratorEBOHandle();
        auto task = rhiApi_->CreateVAOCommand(mesh_, vaoHandle_, vboHandle_, eboHandle_);
        RHI::Get()->Push(task);
    } else {
        auto task = rhiApi_->DrawElementsCommand(vaoHandle_, eboHandle_, mesh_->GetIndexCount());
        RHI::Get()->Push(task);
    }


}