#include "LocalInputCollection.h"

LocalInputCollection::LocalInputCollection ()
{
  inputs.clear();
}

LocalInputCollection::~LocalInputCollection ()
{
}

//reads the event to figure out what Input to add.
void LocalInputCollection::pollInput (SDL_Event & mainEvent)
{
  Input lit;
  lit.setInput(INPUT_NONE);

  switch (mainEvent.type)
    {
    case (SDL_KEYUP):
      break;
    case (SDL_KEYDOWN):
      if (mainEvent.key.keysym.sym == SDLK_ESCAPE)
        lit.setInput(INPUT_QUIT);
      break;
    case (SDL_MOUSEMOTION):
      lit.pushData(mainEvent.motion.x);
      lit.pushData(mainEvent.motion.y);

      if (mainEvent.motion.state==0||SDL_GetMouseState(NULL,NULL)==0)
        {
          lit.pushData(MOUSE_BUTTON_NONE);
          lit.setInput(INPUT_MOUSEMOTION);
        }
      else
        {
          lit.setInput(INPUT_MOUSEDRAG);
          if (mainEvent.button.button != 1)
            lit.pushData(MOUSE_BUTTON_HELP);
          else if (mainEvent.button.button == 1)
            lit.pushData(MOUSE_BUTTON_ACTION);
        }
      break;
    case (SDL_MOUSEBUTTONUP):
      lit.setInput(INPUT_MOUSEUP);
      lit.pushData(mainEvent.motion.x);
      lit.pushData(mainEvent.motion.y);

      if (mainEvent.button.button != 1)
        lit.pushData(MOUSE_BUTTON_HELP);
      else if (mainEvent.button.button == 1)
        lit.pushData(MOUSE_BUTTON_ACTION);
      break;
    case (SDL_MOUSEBUTTONDOWN):
      lit.setInput(INPUT_MOUSEDOWN);
      lit.pushData(mainEvent.motion.x);
      lit.pushData(mainEvent.motion.y);

      if (mainEvent.button.button != 1)
        lit.pushData(MOUSE_BUTTON_HELP);
      else if (mainEvent.button.button == 1)
        lit.pushData(MOUSE_BUTTON_ACTION);
      break;
    };

  inputs.push_back (lit);
}

//Returns the inputs. Outside apps are expected to read and remove entries as appropriate.
vector<Input> & LocalInputCollection::getInputs ()
{
  return inputs;
}
