#include <cstdlib>
#include <SDL/SDL.h>
#include "Input.h"
#include <SDL/SDL_gfxPrimitives.h>

int main ( int argc, char** argv )
{
	// initialize SDL video
	if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
	{
		printf( "Unable to init SDL: %s\n", SDL_GetError() );
		return 1;
	}

	// make sure SDL cleans up before exit
	atexit(SDL_Quit);

	// create a new window
	Uint16 w(1440), h(900);
	SDL_Surface* screen = SDL_SetVideoMode(w, h, 32,
			SDL_HWSURFACE|SDL_DOUBLEBUF);
	if ( !screen )
	{
		printf("Unable to set video: %s\n", SDL_GetError());
		return 1;
	}

	SDL_WM_SetCaption("Color Generator", 0);

	// variables
	SDL_Rect dstrect;
	const Uint32 delay(18);
	Uint32 tps( SDL_GetTicks() ), tps_old(0);

	// graph
	int i(0);
	int r(128), g(128), b(128);
	bool ubr(false), ubg(false), ubb(false);
	bool dbr(false), dbg(false), dbb(false);

	// program main loop
	bool done = false;
	while (!done)
	{
		// message processing loop
		SDL_Event event;
		while (SDL_PollEvent(&event))
		{
			// check for messages
			switch (event.type)
			{
				// exit if the window is closed
				case SDL_QUIT:
					done = true;
					break;

					// check for keypresses
				case SDL_KEYDOWN:
					// exit if ESCAPE is pressed
					switch ( event.key.keysym.sym )
					{
						case SDLK_ESCAPE:
							done = true;
							break;
						case SDLK_a:
						case SDLK_KP7:
						case SDLK_7:
							ubr = true;
							dbr = false;
							break;
						case SDLK_q:
						case SDLK_KP1:
						case SDLK_1:
							ubr = false;
							dbr = true;
							break;
						case SDLK_z:
						case SDLK_KP8:
						case SDLK_8:
							ubg = true;
							dbg = false;
							break;
						case SDLK_s:
						case SDLK_KP2:
						case SDLK_2:
							ubg = false;
							dbg = true;
							break;
						case SDLK_e:
						case SDLK_KP9:
						case SDLK_9:
							ubb = true;
							dbb = false;
							break;
						case SDLK_d:
						case SDLK_KP3:
						case SDLK_3:
							ubb = false;
							dbb = true;
							break;
						default:
							break;
					}
					break;

				case SDL_KEYUP:
					switch ( event.key.keysym.sym )
					{
						case SDLK_a:
						case SDLK_KP7:
						case SDLK_7:
							ubr = false;
							dbr = false;
							break;
						case SDLK_q:
						case SDLK_KP1:
						case SDLK_1:
							ubr = false;
							dbr = false;
							break;
						case SDLK_z:
						case SDLK_KP8:
						case SDLK_8:
							ubg = false;
							dbg = false;
							break;
						case SDLK_s:
						case SDLK_KP2:
						case SDLK_2:
							ubg = false;
							dbg = false;
							break;
						case SDLK_e:
						case SDLK_KP9:
						case SDLK_9:
							ubb = false;
							dbb = false;
							break;
						case SDLK_d:
						case SDLK_KP3:
						case SDLK_3:
							ubb = false;
							dbb = false;
							break;
						default:
							break;
					}
					break;

			} // end switch event tye
		} // end of message processing

		if ( r < 255 && ubr ) r++;
		if ( r > 0 && dbr ) r--;
		if ( g < 255 && ubg ) g++;
		if ( g > 0 && dbg ) g--;
		if ( b < 255 && ubb ) b++;
		if ( b > 0 && dbb ) b--;

		// calculs
		i++;
		if ( i > w ) i = 0;

		// DRAWING STARTS HERE
		vlineRGBA(screen, i, 0, h, r/2, g/2, b/2, 255);
		roundedBoxRGBA(screen, 15, 15, w-15, h-400, 15, r, g, b, 255);

		for ( int j(1); j<=3; j++ )
		{
			filledCircleRGBA(screen, j*w/4, h-200, 149, 128, 128, 128, 255);
		}

		filledPieRGBA(screen, w/4, h-200, 150, 0, r*359/255, 255, 0, 0, 255 );
		filledPieRGBA(screen, w/2, h-200, 150, 0, g*359/255, 0, 255, 0, 255 );
		filledPieRGBA(screen, 3*w/4, h-200, 150, 0, b*359/255, 0, 0, 255, 255 );

		stringRGBA(screen, w/4+140, h-130, "Rouge", 255, 0, 0, 255);
		stringRGBA(screen, w/2+140, h-130, "Vert", 0, 255, 0, 255);
		stringRGBA(screen, 3*w/4+140, h-130, "Bleu", 0, 0, 255, 255);

		// DRAWING ENDS HERE

		// finally, update the screen :)
		SDL_Flip(screen);

		// time management
		do
		{
			tps = SDL_GetTicks();
		} while ( tps-tps_old < delay );
		tps_old = tps;
	} // end main loop

	// all is well ;)
	printf("Exited cleanly\n");
	return 0;
}
