#ifndef UNIT_TEST

#include "Arduino.h"
#include "board.h"
#include "chainGame.h"
#include "detectled.h"
#include "magicSwitchBoard.h"
#include "simpleled.h"
#include "buttons.h"
#include "led.h"
#include "power.h"

#define GAMESELECTTIMEOUT 10000 // 10sec * 1000ms

typedef enum
{
  none,
  sleep,
  idle,
  SimpleLed,
  ChainGame,
  magicSwitchBoard,
  detectLED,
  last
} game;

game currentGame = none;
game nextGame = none;
bool newstate = false;
uint8_t gameState = 0;
uint64_t GameSelectTimer = 0;

void setNewState(game nextstate)
{
  if (nextstate != currentGame)
  {
    currentGame = nextstate;
    newstate = true;
  }
}

void HandleIdle(bool newstate)
{
  //green button first released
  if (!buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN) && (nextGame == none))
  {
    //prepare for next game
    nextGame = ChainGame;
    ResetChainGame();
    turnOffLed(YELLOW);
  }

  //red button first released
  if (buttonIsPressed(YELLOW) && !buttonIsPressed(RED) && buttonIsPressed(GREEN) & (nextGame == none))
  {
    //prepare for next game
    nextGame = magicSwitchBoard;
    turnOffLed(RED);
  }

  //green button first released
  if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && !buttonIsPressed(GREEN) & (nextGame == none))
  {
    //prepare for next game
    nextGame = SimpleLed;
    turnOffLed(GREEN);
  }

  //wait for all buttons to be switched off
  if (!anybutton())
  {
    setNewState(nextGame);
    nextGame = none;
  }
}

void HandleGameSelectTimeout(void)
{
  uint64_t currentmillis = millis();
  // yellow && red && green all on
  if (buttonIsPressed(YELLOW) && buttonIsPressed(RED) && buttonIsPressed(GREEN))
  {
    //all buttons pressed, wait for next game
    if (!GameSelectTimer)
    {
      GameSelectTimer = currentmillis;
    }
    else
    {
      //check timeout
      if (currentmillis - GameSelectTimer > GAMESELECTTIMEOUT)
      {
        setNewState(idle);
        GameSelectTimer = 0;
      }
    }
  }
  else
  {
    //no gameselect sequence initiated
    GameSelectTimer = currentmillis;
  }
}

void setup()
{
  initLeds();
  initButtons();
  initLowPower();
  initBattery();

  initDetectLed();
  initMagicSwitchBoard();
  initSimpleLed();
}

void loop()
{
  handleButtons();
  HandlePower();
  HandleGameSelectTimeout();

  switch (currentGame)
  {
  case idle:
  {
    HandleIdle(newstate);
    newstate = false;
  }
  break;

  case SimpleLed:
  {
    handleSimpleLed(newstate);
    newstate = false;
  }
  break;

  case magicSwitchBoard:
  {
    handleMagicSwitchBoard(newstate);
    newstate = false;
  }
  break;

  case detectLED:
  {
    handleDetectLed(newstate);
    newstate = false;
  }
  break;

  case ChainGame:
  default:
  {
    HandleChainGame(newstate);
    newstate = false;
  }
  break;

  case none:
  {
    batteryCheck();
    batterydisplay();
    delay(1000);
    turnOffAllLed();
    if (buttonIsPressed(GREEN) && currentGame == none)
    {
      setNewState(ChainGame);
      turnOnLed(GREEN);
    }
    if (buttonIsPressed(RED) && currentGame == none)
    {
      setNewState(magicSwitchBoard);
      turnOnLed(RED);
    }

    if (buttonIsPressed(YELLOW) && currentGame == none)
    {
      setNewState(SimpleLed);
      turnOnLed(YELLOW);
    }

    if (currentGame == none)
    {
      setNewState(SimpleLed);
    }

    //wait for all buttons idle
    while (anybutton())
    {
    }

    turnOffAllLed();
  }
  break;
  }
}

#endif