
#include <X11/Xlib.h>

#include <X11/keysym.h>

#define GL_GLEXT_PROTOTYPES

#include <GL/glext.h>

#include <GL/gl.h> // GL 1.1 functions 

#include <GL/glx.h>

#include <stdio.h>

#include <event.h>

#include <time.h>

#include <vector.h>


#include <vector2.h>



class eventManger{
	
	Display * 	mainDisplay;

	Window 		mainWindow;

	Window 		RootWindow;

	event * lastEvent;

	clock_t lastMouseDownTime;

	constructor() {

		this->lastEvent 	= new event(); 

		this->lastEvent->mouse->button = -1;


	}	

	event * fetchEvent() {
		
		event * currentEvent = new event();


		currentEvent->mouse->button = -1;

		Window qRoot;

		Window qChild;

		unsigned int qMask;

		int childX;

		int childY;

		int mouseX;

		int mouseY;

		int child;


		XWindowAttributes window;

		if( XGetWindowAttributes( this->mainDisplay, this->mainWindow, &window ) ) {

		}



		if( XQueryPointer( this->mainDisplay, this->RootWindow, &qRoot, &qChild, &mouseX, &mouseY, &childX, &childY, &qMask ) )
		{

			mouseX -= window.x;

			mouseY -= window.y;

			for(int i = 0; i < sizeof(int) * 8; i++)
			{
				int mask = 1 << sizeof(int) * 8 - i - 1;

				if(mask & qMask)
				{
					//printf("1");
				}
				else
				{
					//printf("0");
				}

			}

			//printf("(%d)\n", qMask);

			if( qMask == Button1MotionMask ) {

				//printf("LeftMouse\n");

				currentEvent->mouse->button		= 0;

			}


			if( qMask == Button2MotionMask ) {

				printf("Button2MotionMask\n");
				
			}


			if( qMask == Button3MotionMask ) {

				printf("RightMouse\n");
				
			}

			if( qMask == Button4MotionMask ) {

				printf("Button2MotionMask\n");
				
			}


			if( qMask == Button5MotionMask ) {

				printf("Button2MotionMask\n");
				
			}

			if( qMask == ShiftMask ) {

				printf("Pressed shift\n");
				
			}

			if( qMask == ControlMask ) {

				printf("Pressed control\n");
				
			}


			if( qMask == EnterWindowMask ) {

				//printf("EnterWindowMask\n");
				
			}



			XEvent   event;

			int keyboardEventCount	=  XPending( this->mainDisplay );

			//printf("eventCount:	%i\n\n", keyboardEventCount);

			while( XPending( this->mainDisplay ) ) {


				XNextEvent( this->mainDisplay, &event );

				switch ( event.type ) {

					case KeyPress:

						printf("key has been pressed.  %i\n\n", event.xkey.keycode);

					break;


					case KeyRelease:

						printf("key has been released.  %i\n\n", event.xkey.keycode);

					break;


					

					case Expose:

					break;



					default:

						//printf("event not defined:		%i\n\n",  event.type);

						//break;

				}

				//printf("event:	%i\n\n", event.type);

			}

			//printf(" Mouse is at (%d,%d)\n", windowX, windowY );

			//printf(" Mouse X: %d 	mouse Y: %d \n", mouseX, mouseY );

			//XGrabKeyboard( this->mainDisplay, this->RootWindow, 0, GrabModeAsync, GrabModeAsync, CurrentTime);


			//printf("qchild is at (%d,%d)\n", mouseX, mouseY);

		}

		bool hasBorder 					= true;

		int  borderCorrection			= 0;



		if( hasBorder ) {

			borderCorrection			= 12;

		}


		currentEvent->mouse->position 	= new vector2( mouseX, mouseY + borderCorrection  );

		currentEvent->screen->size		= new vector2( window.width, window.height + borderCorrection );

		//currentEvent->mouse->eventTypes	= new vector();

		vector<char *> * mouseEvents	 = currentEvent->mouse->eventTypes;

		mouseEvents = new vector();

		if( this->lastEvent->mouse->position.x != currentEvent->mouse->position.x || 
			this->lastEvent->mouse->position.y != currentEvent->mouse->position.y ) {

			mouseEvents->add( "mousemove" );

		}

		if( this->lastEvent->mouse->button != 0 && currentEvent->mouse->button == 0 ) {

			this->lastMouseDownTime = clock();

			printf("Mouse down\n\n");

			mouseEvents->add( "mousedown" );

		}

		if( this->lastEvent->mouse->button == 0 && currentEvent->mouse->button != 0 ) {

			clock_t difference = clock() - this->lastMouseDownTime;

			int milliseconds = difference * 1000 / CLOCKS_PER_SEC;

			if( milliseconds < 150 ) {

				printf("click event\n\n");


				mouseEvents->add( "click" );

			}

			printf("mouseup  event\n\n");

			mouseEvents->add( "mouseup" );
			//printf("%i\n\n", milliseconds);
			

		}

		currentEvent->mouse->eventTypes = mouseEvents;


		this->lastEvent = currentEvent;

		return currentEvent;

	}

}