#include "StateDefault.h"
#include "NerfStatTrakConfig.h"
#include "Globals.h"
#include "StateLoseTheGame.h"
#include "StateUserUnknown.h"


bool DefaultState::ShowHits = true;


void DefaultState::setup()
{
  introStart = currentTime;
  lastAction = currentTime;
}


void DefaultState::loop()
{
  if (buttonA->pressed() && buttonB->pressed())
  {
    // A + B
    setCurrentState(new UserUnknownState());
    return;
  }

  if (buttonA->changed() && !buttonA->pressed())
  {
    // A (triggered on release)
    hits++;

    if (!DefaultState::ShowHits)
      setShowHits(true);

    lastAction = currentTime;
  }

  // B short (triggered on release)
  if (buttonB->changed() && !buttonB->pressed() && buttonB->pressedMillis(currentTime) <= NerfButtonLongPressThreshold)
  {
    setShowHits(!DefaultState::ShowHits);
    lastAction = currentTime;
  }

  // B long
  if (buttonB->pressed() && buttonB->pressedMillis(currentTime) > NerfButtonLongPressThreshold)
  {
    setCurrentState(new LoseTheGameState());
    return;
  }

  // Shot
  if (buttonArmed->changed() && !buttonArmed->pressed())
  {
    // The shot has already been counted in Globals.cpp
    lastAction = currentTime;
  }


  if (showIntro)
  {
    if (currentTime - introStart >= NerfDefaultIntroTime)
      showIntro = false;

    if (DefaultState::ShowHits)
    {
      // Prevents warning: deprecated conversion from string constant to 'char*'
      char text[] = "Hits";
      display->writeTextLeft(text);
    }
    else
    {
      char text[] = "Shots";
      display->writeTextLeft(text);
    }
  }
  else
  {
    if (currentTime - lastAction >= NerfDefaultSleepTime)
    {
      sleep();

      lastAction = currentTime;
    }

    if (DefaultState::ShowHits)
      display->writeNumber(hits);
    else
      display->writeNumber(shots);
  }
}


void DefaultState::setShowHits(bool value)
{
  showIntro = true;
  introStart = currentTime;

  DefaultState::ShowHits = value;
}