#include "../../include/sharedIncludes.h"
#include "eventHandler.h"


extern Scalar rot[3];
extern Scalar dist;
bool zoom = false;
bool pitch = false;


void quitProg( int code )
{
	SDL_ShowCursor(SDL_ENABLE);
  SDL_Quit( );
  exit( code );
}

void handleKeyDown( SDL_keysym* keysym )
{
	switch( keysym->sym ) {
    case SDLK_ESCAPE:
      quitProg( 0 );
      break;
    case SDLK_SPACE:
		  break;
    default:
      break;
  }
}

void handleKeyUp( SDL_keysym* keysym )
{
	switch( keysym->sym ) {
    case SDLK_SPACE:
		  break;
    default:
        break;
    }
}

void handleMouseMotion(SDL_MouseMotionEvent *motion)
{
  //printf("x: %4d, y: %4d, xrel: %4d, yrel: %4d\n", motion->x, motion->y, motion->xrel, motion->yrel);
  if( zoom )
  {
    dist += motion->yrel;
  }
  else if( pitch )
  {
    rot[0] += motion->yrel;
  }
  else
  {
    rot[1] += motion->xrel;
  }
}

void handleMouseButtonDown(SDL_MouseButtonEvent *button)
{
	switch( button->button ) {
    case SDL_BUTTON_LEFT:
      pitch = true;
      break;
	  case SDL_BUTTON_RIGHT:
      zoom = true;
		  break;
	  case SDL_BUTTON_MIDDLE:
		  break;
    default:
      break;
  }
}

void handleMouseButtonUp(SDL_MouseButtonEvent *button)
{
	switch( button->button ) {
    case SDL_BUTTON_LEFT:
      pitch = false;
      break;
	  case SDL_BUTTON_RIGHT:
      zoom = false;
		  break;
	  case SDL_BUTTON_MIDDLE:
		  break;
    default:
      break;
  }
}

void processEvents( void )
{
  SDL_Event event;

  while( SDL_PollEvent( &event ) )
  {
    switch( event.type )
    {
      case SDL_KEYDOWN:
        /* Handle key presses. */
        handleKeyDown( &event.key.keysym );
        break;
      case SDL_KEYUP:
        /* Handle key presses. */
        handleKeyUp( &event.key.keysym );
        break;
      case SDL_MOUSEMOTION:
        handleMouseMotion(&event.motion);
        break;
      case SDL_MOUSEBUTTONDOWN:
        handleMouseButtonDown(&event.button);
        break;
      case SDL_MOUSEBUTTONUP:
        handleMouseButtonUp(&event.button);
        break;
      case SDL_QUIT:
        /* Handle quit requests (like Ctrl-c). */
        quitProg( 0 );
        break;
    }
  }
}

