#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"

#define TIMEOUT 15000          // 15sec * 1000ms
#define GAMESELECTTIMEOUT 10000 // 7sec * 1000ms

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

game currentGame = none;
game nextGame = none;
uint8_t gameState = 0;
uint64_t lasttimeOut = 0;
uint64_t GameSelectTimer = 0;

void HandleIdle(void)
{
  //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 = detectLED;
    turnOffLed(GREEN);
  }

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

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

// void HandleTimeOut(void)
// {
//   uint64_t currentmillis = millis();
//   if (!lasttimeOut)
//   {
//     lasttimeOut = currentmillis;
//   }

//   //check if lastTime is initialized or timeout expired
//   if ((currentmillis - lasttimeOut > TIMEOUT))
//   {
//     //handle timeout
//   }
//   else
//   {
//     if (anybutton())
//     {
//       //game in progress, update timer
//       lasttimeOut = currentmillis;
//     }
//   }
// }

void setup()
{
  initLeds();
  initButtons();
  initDetectLed();
  initSimpleLed();
}

void loop()
{
  handleButtons();
  //HandleTimeOut();
  HandleGameSelectTimeout();

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

  case SimpleLed:
  {
    handleSimpleLed();
  }
  break;

  case magicSwitchBoard:
  {
    handleMagicSwitchBoard();
  }
  break;

  case detectLED:
  {
    handleDetectLed();
  }
  break;

  case ChainGame:
  default:
  {
    HandleChainGame();
  }
  break;

  case none:
  {
    currentGame = SimpleLed;
    if (buttonIsPressed(YELLOW))
    {
      currentGame = ChainGame;
      turnOnLed(YELLOW);
    }
    if (buttonIsPressed(RED))
    {
      currentGame = magicSwitchBoard;
      turnOnLed(RED);
    }

    if (buttonIsPressed(GREEN))
    {
      currentGame = detectLED;
      turnOnLed(GREEN);
    }

    while (anybutton())
      ;

    turnOffAllLed();
  }
  break;
  }
}

#endif