#include "GameState.h"

void GameState :: gamestateBattlePlayerPhaseAfterDraw (uint8_t numButtonsToUse)
{
  int selectedPower;
  inputBufferIterator = inputBuffer.begin ();
  while (inputBufferIterator < inputBuffer.end ())
    {
      if (inputBufferIterator -> getInput () == INPUT_MOUSEUP)
        {
          clearButtons (numButtonsToUse);

          if (isMouseOnHotSpot((*inputBufferIterator), buttonVect[0], false))
            {
              setNewGameState (GAMESTATE_BATTLE_CHECK_FOR_RESULT);
              rulez.declarePlayerWon ();
            }

          if (isMouseOnHotSpot((*inputBufferIterator), buttonVect[1], false))
            setNewGameState (GAMESTATE_BATTLE_CHECK_FOR_RESULT);

          if (isMouseOnHotSpot((*inputBufferIterator), buttonVect[2], false))
            {
              setNewGameState (GAMESTATE_GAME_MENU);
            }

          //Did the user click in the power selector?
          //If so, update information about the selected power:
          //a) Update the text feeder's information
          //b) if it was selected before, confirm the power choice and load up.
          selectedPower=updateSelectedPower(playerPowerSelector, (*inputBufferIterator), helpReference);
          if (selectedPower!=-1)
            {
              preparePowerPreview(selectedPower);
            }
        }
      if (inputBufferIterator -> getInput () == INPUT_MOUSEDOWN)
        {
          buttonUpdateClickStatus ((*inputBufferIterator), numButtonsToUse);
        }
      if (inputBufferIterator -> getInput () == INPUT_QUITGAME)
        {
          addQuitCommand ();
        }
      inputBufferIterator ++;
    }
}

//Checks to see if the user clicked on the power selector.
//If a power is selected it is highlighted, and its index is returned.
//Otherwise nothing changes in the power selector and the index is set to -1.
//helpRef may be updated based on the button pressed.
int GameState::updateSelectedPower(PowerSelector & ps, Input & comm, string & helpRef)
{
  int32_t mouseX, mouseY, butt;

  mouseX = comm.getData()[0];
  mouseY = comm.getData()[1];
  butt = comm.getData()[2];

  int panelPressed = checkInputOnPowerSelector(ps, mouseX, mouseY);

  if (panelPressed == -1)
    return -1;

  //Check for button pressed, change helpRef if necessary

  ps.setSelected(panelPressed);
  return panelPressed;
}

//prepares the textFeeder to show a preview of the power the user just selected.
void GameState::preparePowerPreview(int selectedPower)
{
  char sprintfBait[255];
  string realStringToPrint;
  textFeeders[0].empty();

  int abilityType, quantityOrRecharge, allowedMisses, drawingDifficulty;
  string abilityName, desc;
  uint8_t pattern, bonusDamage, rechRemaining;
  bool minimalDamageWarning;

  //get relevant information
  rulez.compareAbilityToEnemy(selectedPower, abilityType, abilityName, desc, quantityOrRecharge, rechRemaining, pattern, allowedMisses, bonusDamage, minimalDamageWarning, drawingDifficulty);

  textFeeders[0].empty();

  //see if it's the wait command.
  if (abilityType==ABILITY_TYPE_WAIT)
    {
      rander.addFormattedTextToTextFeeder(abilityName, textFeeders[0], true);
      rander.addFormattedTextToTextFeeder(desc, textFeeders[0]);
      return;
    }

  //first line is PowerName---Recharge/Quantity

  //need to prepare recharge/quantity.
  if (abilityType==ABILITY_TYPE_ITEM)
    {
      if (quantityOrRecharge>0)
        {
          sprintf (sprintfBait, "x%i", quantityOrRecharge);
          realStringToPrint=abilityName+"    ";
          realStringToPrint+=sprintfBait;
          rander.addFormattedTextToTextFeeder(realStringToPrint, textFeeders[0], true, 0, 1);
        }
      else
        {
          realStringToPrint=abilityName+"    ";
          rander.addFormattedTextToTextFeeder(realStringToPrint, textFeeders[0], false);
          rander.addFormattedTextToTextFeeder("NONE!", textFeeders[0], true, 1, 1);
        }
    }
  else if (abilityType==ABILITY_TYPE_NATURAL)
    {
      rander.addFormattedTextToTextFeeder(abilityName+"    ", textFeeders[0], false);
      if (rechRemaining>0)
        {
          sprintf (sprintfBait, "wait for %u ticks", rechRemaining);
          realStringToPrint=sprintfBait;
          rander.addFormattedTextToTextFeeder(realStringToPrint, textFeeders[0], true, 1, 1);
        }
      else
        {
          sprintf (sprintfBait, "Recharge Ticks: %i", quantityOrRecharge);
          realStringToPrint=sprintfBait;
          rander.addFormattedTextToTextFeeder(realStringToPrint, textFeeders[0], true, 0, 1);
        }
    }

  //second line is the description.
  rander.addFormattedTextToTextFeeder(desc, textFeeders[0], true);

  //third line is the number of allowed misses/bonus damage. Or a minimalDamageWarning.
  if (minimalDamageWarning)
    rander.addFormattedTextToTextFeeder("Minimal Damage Warning! 1 damage if perfect!", textFeeders[0], true, 1, 1);
  else
    {
      sprintf (sprintfBait, "%i", allowedMisses);
      realStringToPrint=sprintfBait;
      rander.addFormattedTextToTextFeeder(realStringToPrint, textFeeders[0], false, 1);
      rander.addFormattedTextToTextFeeder(" Misses, ", textFeeders[0], false, 0);
      sprintf (sprintfBait, "%i", bonusDamage);
      realStringToPrint=sprintfBait;
      rander.addFormattedTextToTextFeeder(realStringToPrint, textFeeders[0], false, 1);
      rander.addFormattedTextToTextFeeder(" Bonus Damage", textFeeders[0], true, 0, 1);
    }

  //last line is the drawing difficulty.
  if (drawingDifficulty <= -5)
    rander.addFormattedTextToTextFeeder("Impossible to draw!", textFeeders[0], true, 0);
  else if (drawingDifficulty == 0)
    rander.addFormattedTextToTextFeeder("Fair to draw.", textFeeders[0], true, 0);
  else if (drawingDifficulty >= 5)
    rander.addFormattedTextToTextFeeder("Too easy to draw!", textFeeders[0], true, 0);
}
