#include "..\Header\OpenGlViewer.h"
#include "..\Header\defines.h"
#include <iostream>


/////////////////////////////////////////////////////////////////////////
// Defined values

//opengl
#define DEFAULT_MAJOR_VERSION		4
#define DEFAULT_MINOR_VERSION		5
#define DEAFAULT_BACKGROUND			0.5000f, 0.8000f, 1.0000f, 0.0000f

//piplines
#define VERTEX_INDEX_XYZ		0
#define VERTEX_INDEX_UV			1

#define VERTEX_COMPONENTS_XYZ	3
#define VERTEX_COMPONENTS_UV	2

#define VERTEX_SIZE_XYZ			(sizeof(float) * VERTEX_COMPONENTS_XYZ)
#define VERTEX_SIZE_UV			(sizeof(float) * VERTEX_COMPONENTS_UV)

#define VERTEX_OFFSET_XYZ		0
#define VERTEX_OFFSET_UV		(VERTEX_SIZE_XYZ)


/////////////////////////////////////////////////////////////////////////
// public constructor/destructor

OpenGlViewer::OpenGlViewer(QWidget *parent)
	: QOpenGLWidget(parent)
{
	QSurfaceFormat format;
	format.setRenderableType(QSurfaceFormat::OpenGL);
	format.setProfile(QSurfaceFormat::CompatibilityProfile);
	format.setVersion(DEFAULT_MAJOR_VERSION, DEFAULT_MINOR_VERSION);

	setFormat(format);
}

OpenGlViewer::~OpenGlViewer()
{
	m_vertexArray.destroy();
	m_vertexBuffer.destroy();
	delete m_program;

	deleteData();
}


/////////////////////////////////////////////////////////////////////////
// private functions

void OpenGlViewer::initializeGL()
{
	initializeOpenGLFunctions();
	printContextInformation();

	//glEnable(GL_DEPTH_TEST);
	glClearColor(DEAFAULT_BACKGROUND);

	m_program = new QOpenGLShaderProgram();
	m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/simple.vert");
	m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/simple.frag");
	m_program->link();
	m_program->bind();

	m_vertexBuffer.create();
	m_vertexBuffer.bind();
	m_vertexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);

	m_vertexArray.create();
	m_vertexArray.bind();
	m_program->enableAttributeArray(0);
	m_program->enableAttributeArray(1);
	//m_program->setAttributeBuffer(0, GL_FLOAT, Vertex::positionOffset(), Vertex::PositionTupleSize, Vertex::stride());
	//m_program->setAttributeBuffer(1, GL_FLOAT, Vertex::colorOffset(), Vertex::ColorTupleSize, Vertex::stride());

	m_vertexArray.release();
	m_vertexBuffer.release();
	m_program->release();

}

void OpenGlViewer::resizeGL(int w, int h)
{
	//TODO: change perspective
}

void OpenGlViewer::paintGL()
{
	//TODO: paint here
	glClear(GL_COLOR_BUFFER_BIT);

	m_program->bind();
	m_vertexArray.bind();
	//glDrawArrays(GL_TRIANGLES, 0, sizeof(sg_vertexes) / sizeof(sg_vertexes[0]));
	m_vertexArray.release();
	m_program->release();
}

void OpenGlViewer::printContextInformation()
{
	QString glType;
	QString glVersion;
	QString glProfile;

	glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL";
	glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));

#define CASE(c) case QSurfaceFormat::c: glProfile = #c; break
	switch (format().profile())
	{
		CASE(NoProfile);
		CASE(CoreProfile);
		CASE(CompatibilityProfile);
	}
#undef CASE

	std::cout << glType.toStdString() << " - " << glVersion.toStdString() << " (" << glProfile.toStdString() << ")";
}

void OpenGlViewer::deleteData()
{
	if (m_vModels != NULL)
	{
		while (!m_vModels->empty())
		{
			// remove the last Model
			Model* modelVectorElement = m_vModels->back();
			m_vModels->pop_back();

			while (!modelVectorElement->segmList.empty())
			{
				// remove the last Segment
				Segment* segmentVectorElement = modelVectorElement->segmList.back();
				modelVectorElement->segmList.pop_back();

				// delete data from Segment
				delete[] segmentVectorElement->uv;
				delete[] segmentVectorElement->vertex;

				while (!segmentVectorElement->polyIndices.empty())
				{
					// clear the poly vector and remove it from the list
					segmentVectorElement->polyIndices.back().clear();
					segmentVectorElement->polyIndices.pop_back();
				}

				// delete the actual Segment
				delete segmentVectorElement;
			}

			// delete the actual Model
			delete modelVectorElement;
		}

		// delete the Model's Vector
		delete m_vModels;
	}

	while (!m_vTextures.empty())
	{
		// remove the last texture
		textureData* cursor = m_vTextures.back();
		m_vTextures.pop_back();

		// delete the texture's data
		cursor->data->clear();

		//delete the texture's data vector
		delete cursor->data;

		//delete the texture
		delete cursor;
	}
}


/////////////////////////////////////////////////////////////////////////
// public functions

void OpenGlViewer::setData(std::vector<Model*>* models, std::vector<textureData*> textures)
{
	m_vModels = models;
	m_vTextures = textures;
}

