#include <GL/freeglut.h>

#include "JancoProject.h"
#include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

void configureOpenGL(void);

CrystalPoint* app;

bool justMoved = false;

int main(int argc, char* argv[])
{
	app = new CrystalPoint();
	glutInit(&argc, argv);

    srand (time(NULL));

    configureOpenGL();

	app->init();

	glutDisplayFunc([]() { app->draw(); } );
	glutIdleFunc([]() { app->update(); } );
	glutReshapeFunc([](int w, int h) { app->width = w; app->height = h; glViewport(0, 0, w, h); });

	//Keyboard
	glutKeyboardFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = true; });
	glutKeyboardUpFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = false; });
	
	//Mouse
	glutPassiveMotionFunc([](int x, int y)
	{
		if (justMoved)
		{
			justMoved = false;
			return;
		}
		int dx = x - app->width / 2;
		int dy = y - app->height / 2;
		if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
		{
			app->mouseOffset = app->mouseOffset + Vec2f(dx,dy);
			glutWarpPointer(app->width / 2, app->height / 2);
			justMoved = true;
		}
	});

	glutMainLoop();
	return 0;
}

void configureOpenGL()
{
	//Init window and glut display mode
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutCreateWindow("Janco");
	glutFullScreen();

	//Depth testing
	glEnable(GL_DEPTH_TEST);

	//Alpha blending
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	//Alpha testing
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0.01f);

	//Lighting
	GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 0 };
	//GLfloat mat_shininess[] = { 5.0 };
	GLfloat light_position[] = { 0.0, 2.0, 1.0, 0 };
	GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 0 };
	GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 0 };
	glClearColor(0.7, 0.7, 1.0, 1.0);

	//glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	//glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
	//glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	//glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);

	//glEnable(GL_LIGHTING);
	//glEnable(GL_LIGHT0);

	glutSetCursor(GLUT_CURSOR_CROSSHAIR);
	glutWarpPointer(app->width / 2, app->height / 2);
}