/*
 * Renderer.cpp
 *
 *  Created on: 4 nov. 2020
 *      Author: simon
 */

#ifndef ANDROID
#include <glbinding/Binding.h>
#endif
#include "render/Renderer.h"
#include "render/GL.h"
#include <stdio.h>
#include "misc/Time.h"
#include "misc/Easing.h"

namespace td {
namespace render {

Renderer::Renderer() : m_BackgroundColor(0, 0, 0) {

}

Renderer::~Renderer() {

}

void Renderer::UpdateIsometricView() {
    float isometricEased = utils::EaseInOutExpo(m_IsometricShade);
    m_WorldShader->Start();
    m_WorldShader->SetIsometricView(isometricEased);
    m_EntityShader->Start();
    m_EntityShader->SetIsometricView(isometricEased);
}

void Renderer::InitShaders() {
    m_WorldShader = std::make_unique<shader::WorldShader>();
    m_WorldShader->LoadShader();
    m_EntityShader = std::make_unique<shader::EntityShader>();
    m_EntityShader->LoadShader();
    SetIsometricView(true);
    UpdateIsometricView();
}

bool Renderer::Init() {
#ifndef ANDROID
    glbinding::Binding::initialize();
#endif
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    InitShaders();
    return true;
}

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

void Renderer::RenderModel(const Model& model) {
    m_EntityShader->Start();
    m_EntityShader->SetModelPos(model.positon);
    model.vao->Bind();
    glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(model.vao->GetVertexCount()));
    model.vao->Unbind();
}

void Renderer::UpdateIsometricFade() {
    static std::uint64_t lastTime = utils::GetTime();
    if (m_IsometricShade != static_cast<float>(m_IsometricView)) {
        float step = static_cast<float>(utils::GetTime() - lastTime) / 1000.0f * m_AnimationSpeed;
        if (m_IsometricShade < m_IsometricView) {
            m_IsometricShade += step;
        } else {
            m_IsometricShade -= step;
        }
        m_IsometricShade = std::min(m_IsometricShade, 1.0f);
        m_IsometricShade = std::max(m_IsometricShade, 0.0f);
        UpdateIsometricView();
    }
    lastTime = utils::GetTime();
}

void Renderer::Prepare() {
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(m_BackgroundColor.r, m_BackgroundColor.g, m_BackgroundColor.b, 0);
    UpdateIsometricFade();
}

void Renderer::Resize(int width, int height) {
    m_WorldShader->Start();
    m_WorldShader->SetAspectRatio(static_cast<float>(width) / height);
    m_EntityShader->Start();
    m_EntityShader->SetAspectRatio(static_cast<float>(width) / height);
    glViewport(0, 0, width, height);
}

void Renderer::SetZoom(float zoom) {
    m_WorldShader->Start();
    m_WorldShader->SetZoom(zoom);
    m_EntityShader->Start();
    m_EntityShader->SetZoom(zoom);
}

void Renderer::SetCamMovement(const glm::vec2& mov) {
    m_CamPos.x += mov.x * (1 - m_IsometricView) + (0.5 * mov.x - mov.y) * m_IsometricView;
    m_CamPos.y += -mov.y * (1 - m_IsometricView) + (-0.5 * mov.x - mov.y) * m_IsometricView;
    SetCamPos(m_CamPos);
}

void Renderer::SetCamPos(const glm::vec2& newPos) {
    m_CamPos = newPos;
    m_WorldShader->Start();
    m_WorldShader->SetCamPos(newPos);
    m_EntityShader->Start();
    m_EntityShader->SetCamPos(newPos);
}

void Renderer::SetIsometricView(bool isometric) {
    m_IsometricView = isometric;
}

glm::vec2 Renderer::GetCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight) {
    float isometricEased = utils::EaseInOutExpo(m_IsometricShade);

    float relativeX = (cursorPos.x / windowWidth * 2) - 1;
    float relativeY = (cursorPos.y / windowHeight * 2) - 1;

    float deltaX = relativeX * aspectRatio / zoom;
    float deltaY = relativeY / zoom;

    float worldX = m_CamPos.x + deltaX * (1 - isometricEased) + (0.5 * deltaX + deltaY) * isometricEased;
    float worldY = m_CamPos.y + deltaY * (1 - isometricEased) + (-0.5 * deltaX + deltaY) * isometricEased;

    return { worldX, worldY };
}


} // namespace render
} // namespace td
