#include "GameState.h"

void GameState :: gamestateMapStartup(vector <MapUnit> & getEnemyMapUnits, MapUnit & playerMapUnit)
{
  rander.startupMapScreen (getEnemyMapUnits, playerMapUnit);
}

void GameState :: gamestateMapAfterDraw(uint8_t numberOfButtonsToHandle)
{
  int32_t mapLeft, mapTop, mapWid, mapHei;

  inputBufferIterator = inputBuffer.begin ();
  while (inputBufferIterator < inputBuffer.end ())
    {
      if (inputBufferIterator -> getInput () == INPUT_MOUSEUP)
        {
          clearButtons (numberOfButtonsToHandle);

          if (isMouseOnHotSpot((*inputBufferIterator), buttonVect[0], false))
            setNewGameState (GAMESTATE_INVENTORY);

          if (isMouseOnHotSpot((*inputBufferIterator), buttonVect[1], false))
            {
              setNewGameState (GAMESTATE_GAME_MENU);
            }
        }
      if (inputBufferIterator -> getInput () == INPUT_MOUSEDOWN)
        {
          buttonUpdateClickStatus ((*inputBufferIterator));
        }
      if (inputBufferIterator -> getInput () == INPUT_MOUSEDRAG || inputBufferIterator -> getInput () == INPUT_MOUSEDOWN)
        {
          //checking to see if the user clicked on the map to set a waypoint
          if (inputBufferIterator -> getData()[2] == MOUSE_BUTTON_ACTION)
            {
              //extract coordinates
              rander.getDebugMapScreenCoordinates (rulez.getMap(), mapLeft, mapTop, mapWid, mapHei);
              rulez.setMapWayPoint (inputBufferIterator->getData()[0] - mapLeft, inputBufferIterator->getData()[1] - mapTop);
            }
        }
      if (inputBufferIterator -> getInput () == INPUT_QUITGAME)
        {
          addQuitCommand ();
        }

      inputBufferIterator ++;
    }

  rulez.mapAction (SDL_GetTicks());

  //if the player is on top of an enemy, begin battle
  if (rulez.isPlayerAboutToEngageInBattle())
    {
      setNewGameState (GAMESTATE_BATTLE_SWOOSH);
    }

  //check to see if the user reached the exit.
  if (rulez.isPlayerOnEndOfMap())
    setNewGameState (GAMESTATE_LEVEL_COMPLETE);
}
