/****************************************************************************

 Copyright (C) 2002-2008 Gilles Debunne. All rights reserved.

 This file is part of the QGLViewer library version 2.3.6.

 http://www.libqglviewer.com - contact@libqglviewer.com

 This file may be used under the terms of the GNU General Public License
 versions 2.0 or 3.0 as published by the Free Software Foundation and
 appearing in the LICENSE file included in the packaging of this file.
 In addition, as a special exception, Gilles Debunne gives you certain
 additional rights, described in the file GPL_EXCEPTION in this package.

 libQGLViewer uses dual licensing. Commercial/proprietary software must
 purchase a libQGLViewer Commercial License.

 This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

*****************************************************************************/

#ifndef SIMPLEVIEWER_H
#define SIMPLEVIEWER_H

#include <QOpenGLFunctions_4_0_Core>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QOpenGLTexture>
#include <QMatrix4x4>
#include <QGLViewer/qglviewer.h>
#include "../interfaces/visitor.h"
#include "../glnodes/glnode.h"
#include "../glnodes/scenegroup.h"
#include "../glnodes/shapes.h"
#include <stack>

QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)


struct PickedGeom {
    Shape* shape;
    QMatrix4x4 position;
};

class Viewer : public QGLViewer,
		protected QOpenGLFunctions_4_0_Core,
		public Visitor
{
Q_OBJECT
public:
	Viewer();
	~Viewer();

    virtual void visit(SceneGroup &s);
    virtual void visit(Cube &s);
    virtual void visit(Sphere &s);



    enum RotateDirection {
        X_CCW,
        X_CW,
        Y_CCW,
        Y_CW,
        Z_CCW,
        Z_CW
    };

public slots:
	void cleanup();
    void changeColor(QColor);
    void setPhong(bool);
    void setMagLinear(bool);
    void setMinLinear(bool);
    void deleteSelected();
    void toggleNormalMaps(bool);
    void rotateSelectedObjXCCW();
    void rotateSelectedObjXCW();
    void rotateSelectedObjYCCW();
    void rotateSelectedObjYCW();
    void rotateSelectedObjZCCW();
    void rotateSelectedObjZCW();
    void animationSpeedChange(int);

signals:
	int shapeSelected(int);
    bool cubeSelected(bool);

protected :
    virtual void draw();
    virtual void drawSkybox();
	virtual void init();

	virtual void mouseMoveEvent(QMouseEvent* e);
	virtual void mouseReleaseEvent(QMouseEvent* e);
	virtual void mousePressEvent(QMouseEvent *e);
	virtual void keyPressEvent(QKeyEvent *);
	virtual void keyReleaseEvent(QKeyEvent *);

	SceneGroup root;
	std::stack<QMatrix4x4> modelStack;

private:
    void resetSphereColors();
	void initShaders();
	void initGeometries();
	void initBuffers();
	void deselect();
    PickedGeom pickGeom(int, int);

	void animate();
//    void startAnimation();
//	void stopAnimation();

	void drawUi();

	// shader switching variables and constants;
    QOpenGLShaderProgram *colorPickerShaderProgram;
    QOpenGLShaderProgram *textureRenderShaderprogram;
    QOpenGLShaderProgram *skyboxRenderShaderProgram;
	QOpenGLShaderProgram *m_program;
	int m_vPositionLocation;
    int m_texColor;
    int m_texNormal;
	int m_colorLocation;
    int m_useNormalMap;
	int m_projMatrixLocation;
	int m_mvMatrixLocation;

    int s_texCoordsLocation;
    int s_vPositionLocation;
    int s_mvMatrixLocation;
    int s_colorLocation;
    int s_projMatrixLocation;
    int s_skyboxCubemapLocation;
    int s_vUvLocation;
    int m_vNormalLocation;

    int m_isPhongLoc;
    int m_isSkyLoc;
    int m_lDirLoc;
    int m_skyMultLoc;
	int m_drawTextLoc;
	int m_isLightLoc;

    int m_point1Loc;
    int m_point2Loc;
    int m_point3Loc;

    int m_c1Loc;
    int m_c2Loc;
    int m_c3Loc;

	int m_isPickingModeLoc;
	bool isPickingActivated = false;

	float angle_mult;
	float frame_mult = 1;

	clockid_t startTime;

    QOpenGLTexture *s_texture;

	SceneGroup* activeCell;
	QColor* activeColor;
	int activeShape;

	enum VAO_IDs { VAO_Cube, VAO_Sphere, NumVAOs };
	enum Buffer_IDs { VBO_Cube, VBO_Sphere, EBO_Sphere, NumBuffers };
	enum RenderBuffer_IDs { RenderBuffer_Main, NumRenderBuffers };

	GLuint m_VAOs[NumVAOs];
	GLuint m_Buffers[NumBuffers];
	GLuint m_RenderBuffers[NumRenderBuffers];

    Shape* generateShapeFromIndex(int);

	enum Textures{
		TEX_DRYGOUND,
		TEX_GRANITEFLOOR,
		TEX_GRASS,
		TEX_LIMESTONEWALLS,
		TEX_PIERREBOUCHARDEE,
		TEX_WOODFLOOR,

		TEX_LENGTH
	};

	unsigned int selectedTexture = TEX_WOODFLOOR;

    QString TexturePaths[TEX_LENGTH] = {
        "src/data/dry_ground.jpg",
        "src/data/granite_floor.jpg",
        "src/data/grass.jpg",
        "src/data/limestone_wall.jpg",
        "src/data/pierre_bouchardee.jpg",
        "src/data/wood_floor.jpg"
    };
    QString NormalPaths[TEX_LENGTH] = {
        "src/data/dry_ground_normals.jpg",
        "src/data/granite_floor_normals.jpg",
        "src/data/grass_normals.jpg",
        "src/data/limestone_wall_normals.jpg",
        "src/data/pierre_bouchardee_normals.jpg",
        "src/data/wood_floor_normals.jpg"
    };

	int sideColors[6] = {
		0xFF0000,
		0x00FF00,
		0x0000FF,
		0x00FFFF,
		0xFF00FF,
		0xFFFF00,
	};

    QOpenGLTexture *TexturePrograms[TEX_LENGTH * 2];
	QMatrix4x4 identityMatrix;

	QQuaternion rot;
	double  frame;

    void rotateSelected(RotateDirection);
};

#endif // SIMPLEVIEWER_H
