#include "SkyBox.h"
#include "Shader.h"

#include <glad/glad.h>
#include <stb_image.h>
#include <glm/gtc/type_ptr.hpp>

using namespace std;

SkyBox::SkyBox() :
		textureId(GL_INVALID_INDEX), programId(GL_INVALID_INDEX), textureLoc(-1) {

}

SkyBox::~SkyBox() {
	glDeleteTextures(1, &textureId);
}

bool SkyBox::Load(const std::string basename) {
	glGenTextures(1, &textureId);

	float extent = 10;
	mesh.positions.clear();
	mesh.positions.push_back(glm::vec3(-extent, -extent, -extent));
	mesh.positions.push_back(glm::vec3(-extent, -extent, extent));
	mesh.positions.push_back(glm::vec3(extent, -extent, extent));
	mesh.positions.push_back(glm::vec3(extent, -extent, -extent));

	mesh.positions.push_back(glm::vec3(-extent, extent, -extent));
	mesh.positions.push_back(glm::vec3(-extent, extent, extent));
	mesh.positions.push_back(glm::vec3(extent, extent, extent));
	mesh.positions.push_back(glm::vec3(extent, extent, -extent));

	mesh.indices.clear();
	// bottom
	mesh.indices.push_back(glm::uvec3(1, 0, 2));
	mesh.indices.push_back(glm::uvec3(2, 0, 3));
	// back
	mesh.indices.push_back(glm::uvec3(5, 2, 6));
	mesh.indices.push_back(glm::uvec3(1, 2, 5));
	// front
	mesh.indices.push_back(glm::uvec3(3, 4, 7));
	mesh.indices.push_back(glm::uvec3(3, 0, 4));
	// left
	mesh.indices.push_back(glm::uvec3(6, 3, 7));
	mesh.indices.push_back(glm::uvec3(2, 3, 6));
	// right
	mesh.indices.push_back(glm::uvec3(0, 1, 4));
	mesh.indices.push_back(glm::uvec3(4, 1, 5));
	// top
	mesh.indices.push_back(glm::uvec3(5, 6, 4));
	mesh.indices.push_back(glm::uvec3(6, 7, 4));



	glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);

	Load(GL_TEXTURE_CUBE_MAP_POSITIVE_X, "assets/textures/" + basename + "_right1.jpg");
	Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, "assets/textures/" + basename + "_left2.jpg");
	Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, "assets/textures/" + basename + "_top3.jpg");
	Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, "assets/textures/" + basename + "_bottom4.jpg");
	Load(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, "assets/textures/" + basename + "_front5.jpg");
	Load(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, "assets/textures/" + basename + "_back6.jpg");

	programId = loadProgram("skybox");

	GLCK(textureLoc = glGetAttribLocation(programId, "tCubemap"))
	GLCK(MVPloc = glGetUniformLocation(programId, "MVP"))

	glMesh.create();
	glMesh.update(programId);
	glMesh.upload(mesh);

	return true;
}

bool SkyBox::Load(GLuint target, const std::string filename) {
	int x, y, comp;
	printf ("start\n");
	unsigned char *data = stbi_load(filename.c_str(), &x, &y, &comp, 0);
	printf ("end\n");

	glTexImage2D(target, 0, GL_RGB, x, y, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S,
	GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T,
	GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R,
	GL_CLAMP_TO_EDGE);

	stbi_image_free(data);
	return true;
}

void SkyBox::Render(glm::mat4 mvp) {
	GLint OldCullFaceMode;
	glGetIntegerv(GL_CULL_FACE_MODE, &OldCullFaceMode);
	GLint OldDepthFuncMode;
	glGetIntegerv(GL_DEPTH_FUNC, &OldDepthFuncMode);

	glCullFace(GL_FRONT);
	glDepthFunc(GL_LEQUAL);
	/*
	 Pipeline p;
	 p.Scale(20.0f, 20.0f, 20.0f);
	 p.Rotate(0.0f, 0.0f, 0.0f);
	 p.WorldPos(m_pCamera->GetPos().x, m_pCamera->GetPos().y,
	 m_pCamera->GetPos().z);
	 p.SetCamera(m_pCamera->GetPos(), m_pCamera->GetTarget(),
	 m_pCamera->GetUp());
	 p.SetPerspectiveProj(m_persProjInfo);
	 m_pSkyboxTechnique->SetWVP(p.GetWVPTrans());
	 m_pCubemapTex->Bind(GL_TEXTURE0);
	 */

	GLCK(glUseProgram(programId));
	mvp[3] = glm::vec4(0, 0, 0, 1);
	GLCK(glUniformMatrix4fv(MVPloc, 1, GL_FALSE, glm::value_ptr(mvp)))

	GLCK(glUniform1i(textureLoc, 0))
	GLCK(glActiveTexture(GL_TEXTURE0))
	GLCK(glBindTexture(GL_TEXTURE_CUBE_MAP, textureId))

	glMesh.render();
	glCullFace(OldCullFaceMode);
	glDepthFunc(OldDepthFuncMode);
}
