#include "RenderSystem.h"
#include "components/Renderable.h"
#include "components/Position.h"

#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>

void RenderSystem::update(entityx::EntityManager &es,
                          entityx::EventManager &events,
                          entityx::TimeDelta dt)
{
    for (entityx::Entity entity : es.entities_with_components<Renderable, Position>())
    {
        entityx::ComponentHandle<Renderable> meshContainer = entity.component<Renderable>();
        entityx::ComponentHandle<Position> pos = entity.component<Position>();

        glm::mat4 model(1);
        float x = pos.get()->x;
        float y = pos.get()->y;
        model = glm::translate(model, glm::vec3(x, y, 0.f));

        glm::mat4 combined;
        combined = _camera->getProjectionMatrix() * _camera->getViewMatrix() * model;

        //Drawing
        Shader defaultShader = _assets->getShader(meshContainer.get()->_shaderName);
        defaultShader.SetMatrix4("MVP", combined, true);
        //Draw Mesh
        _renderer->draw(*(meshContainer.get()->_mesh), defaultShader);
    }
}