#include "client/render/WorldRenderer.h"

#include "client/Client.h"
#include "client/render/loader/GLLoader.h"
#include "client/render/shader/EntityShader.h"
#include <GL/glew.h>

#define VERTEX_SIZE 3

namespace blitz {
namespace render {

WorldRenderer::WorldRenderer(Client* client) : m_Client(client) {

	std::vector<float> positions = {
		-0.5, -0.5, -0.1,
		0.5, -0.5, -0.1,
		-0.5, 0.5, -0.1,

		0.5, -0.5, -0.1,
		-0.5, 0.5, -0.1,
		0.5, 0.5, -0.1
	};

	GL::VertexBuffer positionVBO(positions, VERTEX_SIZE);
	positionVBO.AddVertexAttribPointer(0, VERTEX_SIZE, 0);

	m_Vao = std::make_unique<GL::VertexArray>(positions.size() / VERTEX_SIZE);	// each pos = 1 color
	m_Vao->Bind();
	m_Vao->BindVertexBuffer(positionVBO);
	m_Vao->Unbind();

	m_Shader = std::make_unique<shader::EntityShader>();
	m_Shader->LoadShader();
}

WorldRenderer::~WorldRenderer() {}

void WorldRenderer::Render(const GL::VertexArray& vao) {
	m_Shader->Start();
	vao.Bind();
	glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vao.GetVertexCount()));
	vao.Unbind();
}

void WorldRenderer::Render() {
	if (!m_Client->IsConnected())
		return;

	Render(*m_Vao.get());
}

}  // namespace render
}  // namespace blitz
