#include <cmath>
#include <cfloat>

#include "client/rendering/Engine.h"
#include "client/rendering/wrapper/GLFWWrapper.h"
#include "client/rendering/wrapper/GLWrapper.h"
#include "client/rendering/Renderer.h"
#include "client/math/Plane.h"

Engine::Engine(Shaders& shaders, Framebuffers& fb, const WindowSize& size, RenderSettings& renderSettings) :
shaders(shaders), fb(fb), size(size), renderSettings(renderSettings), frustum(60.0f, 0.1f, 80.0f) {
    rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
    rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
    rectangle.add( {-1, 1, 0, 0, 1, 0, 0, 0});
    rectangle.add( {-1, -1, 0, 0, 0, 0, 0, 0});
    rectangle.add( {1, -1, 0, 1, 0, 0, 0, 0});
    rectangle.add( {1, 1, 0, 1, 1, 0, 0, 0});
    rectangle.build();
}

void Engine::renderTick(float lag, const Game& game) {
    updateWorldProjection();
    updateWorldView();

    if(renderSettings.shadows) {
        renderShadow(lag, game);
    }
    renderWorld(lag, game);
    if(renderSettings.ssao) {
        renderSSAO();
    }
    renderPostWorld();
    renderTextOverlay(lag, game);
}

void Engine::renderShadow(float lag, const Game& game) {
    fb.shadow.bind();
    GLWrapper::enableDepthTesting();
    shaders.shadow.use();
    worldShadowProjView.set(worldShadowProj).mul(worldShadowView);
    shaders.shadow.setMatrix("projView", worldShadowProjView.getValues());
    model.clear();
    shaders.world.setMatrix("model", model.get().getValues());
    Renderer renderer(shaders.shadow, model, worldView);
    game.renderWorld(lag, renderer);
}

void Engine::renderWorld(float lag, const Game& game) {
    fb.world.bind();
    GLWrapper::enableDepthTesting();
    shaders.world.use();

    Matrix rWorldShadowProjView;
    rWorldShadowProjView.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldShadowProjView);

    shaders.world.setMatrix("projViewShadow", rWorldShadowProjView.getValues());
    shaders.world.setMatrix("proj", worldProj.getValues());
    shaders.world.setMatrix("view", worldView.setToIdentity().getValues());
    model.clear();
    shaders.world.setMatrix("model", model.get().getValues());
    fb.shadow.bindDepthTexture(1);
    shaders.world.setInt("shadows", renderSettings.shadows);
    shaders.world.setFloat("radius", renderSettings.testRadius);
    shaders.world.setFloat("zbias", renderSettings.testBias);
    Renderer renderer(shaders.world, model, worldView);
    game.renderWorld(lag, renderer);
}

void Engine::renderSSAO() {
    shaders.ssao.use();

    Matrix rProj;
    rProj.translate(0.5f, 0.5f, 0.5f).scale(0.5f).mul(worldProj);

    shaders.ssao.setMatrix("proj", rProj.getValues());
    shaders.ssao.setInt("width", size.width);
    shaders.ssao.setInt("height", size.height);
    fb.world.bindPositionTexture(0);
    fb.world.bindNormalTexture(1);
    fb.world.bindColorTexture(2);
    fb.world.bindDepthTexture(3);
    ssaoNoise.bind(4);
    fb.ssao.bind();
    rectangle.draw();

    shaders.ssaoBlur.use();
    fb.ssao.bindRedTexture(0);
    fb.ssaoBlur.bind();
    rectangle.draw();
}

void Engine::renderPostWorld() {
    GLWrapper::prepareMainFramebuffer();
    shaders.postWorld.use();
    fb.world.bindColorTexture(0);
    fb.ssaoBlur.bindRedTexture(1);
    fb.world.bindRedTexture(2);
    fb.world.bindNormalTexture(3);
    shaders.postWorld.setInt("ssao", renderSettings.ssao);
    shaders.postWorld.setInt("shadows", renderSettings.shadows);
    rectangle.draw();
}

void Engine::renderTextOverlay(float lag, const Game& game) {
    GLWrapper::disableDepthTesting();
    shaders.text.use();

    Matrix m;
    shaders.text.setMatrix("proj", m.getValues());
    m.translate(-1.0f, 1.0f, 0.0f).scale(2.0f / size.width, -2.0f / size.height, 1.0f);
    shaders.text.setMatrix("view", m.getValues());
    model.clear();
    shaders.text.setMatrix("model", model.get().getValues());

    GLWrapper::enableBlending();
    Renderer renderer(shaders.text, model, m);
    game.renderTextOverlay(lag, renderer, fontRenderer);
    GLWrapper::disableBlending();
}

void Engine::updateWorldProjection() {
    frustum.setProjection(worldProj, size.width, size.height);
    
    if(!renderSettings.shadows) {
        return;
    }
    worldShadowProj.setToIdentity();
    worldShadowProj.set(0, 2.0f / 40.0f);
    worldShadowProj.set(5, 2.0f / 30.0f);
    worldShadowProj.set(10, -2.0f / frustum.getClipDifference());
}

void Engine::updateWorldView() {
    if(!renderSettings.shadows) {
        return;
    }

    Vector right(0.939693f, 0.0f, -0.34202f);
    Vector back(0.280166f, 0.573576f, 0.769751f);
    Vector up(-0.196175f, 0.819152f, -0.538986f);
    Vector center(16.0f, 24.0f, 24.0f);

    worldShadowView.set(0, right.getX());
    worldShadowView.set(1, up.getX());
    worldShadowView.set(2, back.getX());
    worldShadowView.set(4, right.getY());
    worldShadowView.set(5, up.getY());
    worldShadowView.set(6, back.getY());
    worldShadowView.set(8, right.getZ());
    worldShadowView.set(9, up.getZ());
    worldShadowView.set(10, back.getZ());
    worldShadowView.set(12, right.dotInverse(center));
    worldShadowView.set(13, up.dotInverse(center));
    worldShadowView.set(14, back.dotInverse(center));
}