#include "allegro.h"

#include "InputObj.h"
#include "gameConstants.h"

#include <vector>
using namespace std;

InputObj :: InputObj ()
{
  //demo: should be read from the input files
  keyMapping = new vector <int> (keyNumberOfKeys, KEY_F1);
  keyPressed = new vector <bool> (keyNumberOfKeys, false);
  keyClicked = new vector <bool> (keyNumberOfKeys, false);
  keyMapping -> at (keyUp) = keyBindUp;
  keyMapping -> at (keyDown) = keyBindDown;
  keyMapping -> at (keyLeft) = keyBindLeft;
  keyMapping -> at (keyRight) = keyBindRight;
  keyMapping -> at (keyHyper) = keyBindHyper;
  keyMapping -> at (keyRestart) = keyBindRestart;
  keyMapping -> at (keyPause) = keyBindPause;
  keyMapping -> at (keyMenu) = keyBindMenu;
  keyMapping -> at (keyTaunt) = keyBindTaunt;
  mousePressed = new vector <bool> (3, false);
  mouseClicked = new vector <bool> (3, false);
  mouseControlThreshold = 30;
  mouseMickeysX = 0;
  mouseMickeysY = 0;
}

void InputObj :: processInput (bool gamePaused)
{
  //processes input. i.e. mouse and keyboard.
  //makes sure the game isn't paused, too.

  int i = 0;
  for (i = 0; i < keyNumberOfKeys; i ++) {
    if (keyPressed -> at (i) && !key[keyMapping -> at (i)])
      {
	keyClicked -> at (i) = true;
      }
    else
      {
	if (!gamePaused)
	  keyClicked -> at (i) = false;
      }
    
    keyPressed -> at(i) = key[keyMapping -> at (i)];
  }
  
  get_mouse_mickeys (&mouseMickeysX, &mouseMickeysY);
  
  for (i = 0; i < int (mousePressed -> size ()); i++) {
    if (mousePressed -> at (i) && !(mouse_b & ((2 << i)>>1)))
      mouseClicked -> at (i) = true;
    else if (!gamePaused)
      mouseClicked -> at (i) = false;
    
    if (mouse_b & ((2 << i)>>1))
      mousePressed -> at(i) = true;
    else
      mousePressed -> at(i) = false;
  }
}

vector<bool> * InputObj :: getKeyPressed ()
{ return keyPressed; }

vector<bool> * InputObj :: getKeyClicked ()
{ return keyClicked; }

vector<bool> * InputObj :: getMousePressed ()
{ return mousePressed; }

vector<bool> * InputObj :: getMouseClicked ()
{ return mouseClicked; }

int InputObj :: getMouseControlThreshold ()
{return mouseControlThreshold;}

void InputObj :: setMouseControlThreshold (int mt)
{mouseControlThreshold = mt;}

void InputObj :: getMouseMickeys (int & mx, int & my)
{mx = mouseMickeysX; my = mouseMickeysY;}

void InputObj :: clear ()
{
  //clears all input.
  keyPressed -> assign (keyNumberOfKeys, false);
  keyClicked -> assign (keyNumberOfKeys, false);
}

bool InputObj :: anyClicked ()
{
  //returns true if any key or mouse button has been clicked.
  for (int i = 0; i < int(keyClicked -> size ()); i ++)
    {if (keyClicked -> at (i))
      return true;}
  
  for (int i = 0; i < int(mouseClicked -> size ()); i ++)
    {if (mouseClicked -> at (i))
      return true;}

  return false;
}

vector <int> * InputObj :: getKeyMappings ()
{ return keyMapping; }

void InputObj :: setKeyMappings (vector <int> * map)
{ 
  vector <int> ::iterator iter1;
  vector <int> ::iterator iter2;

  iter1 = map -> begin (); iter2 = keyMapping -> begin ();

  while (iter1 < map -> end () && iter2 < keyMapping -> end ())
    { (*iter1) = (*iter2); iter1++; iter2++;}
}

void InputObj :: setKeyMapping (int index, int key)
{ 
  if (index >= 0 && index < int(keyMapping -> size ()))
    { keyMapping -> at (index) = key; }
  else
    allegro_message ("Set Key Mapping: Illegal index %d, \n", index);
}

InputObj :: ~InputObj ()
{
}
