#ifndef UNIT_TEST
#ifdef ARDUINO

#include "chainGame.h"

uint8_t patternIndex = 0;
bool patternFlag = false;
//bool firstpattern = false;
uint16_t cheatbutton = 0;
bool cheatButtonFlag = false;


uint16_t ledpattern[4] =
    {
        1,
        3,
        1,
        2};

int patternlength = sizeof(ledpattern) / sizeof(ledpattern[0]);

void nextPattern(void)
{
  if (patternIndex < patternlength - 1)
  {
    patternIndex++;
  }
  else
  {
    patternIndex = 0;
  }
}

void ResetChainGame(void)
{
  patternIndex = 0;
  patternFlag = false;
  //firstpattern = false;
  cheatbutton = 0;
  cheatButtonFlag = false;
}

void HandleChainGame(void)
{
  if (!patternFlag && !cheatButtonFlag)
  {
    if (buttonIsPressed(ledpattern[patternIndex]))
    {
      //pattern button pressed, turn on LED, set flag
      turnOnLed(ledpattern[patternIndex]);
      patternFlag = true;
      cheatbutton = 0;
    }
    else if (buttonIsPressed(cheatbutton))
    {
      // cheatbutton pressed, turn on cheat led, set flag
      turnOnLed(cheatbutton);
      cheatButtonFlag = true;
    }
    else if (anybutton())
    {
      // if any other button is pressed, clear cheat button
      //cheatbutton = 0;
    }
  }

  if (!buttonIsPressed(ledpattern[patternIndex]))
  {
    // pattern switch is open, turn off pattern LED
    turnOffLed(ledpattern[patternIndex]);
  }

  if (!buttonIsPressed(cheatbutton) && cheatButtonFlag)
  {
    // cheat switch is open, turn of cheat LED
    turnOffLed(cheatbutton);
    cheatButtonFlag = false;
    cheatbutton = 0;
  }

  if (!anybutton())
  {
    //all switches are open, turn off all LEDs
    turnOffAllLed();
    if (patternFlag)
    {
      // pattern LED was triggerd, reset flag, move to next pattern
      patternFlag = false;
      nextPattern();
    }
  }

  //check cheatbuttons
  if (buttonIsPressed(4) && (cheatbutton == 0))
  {
    // cheatbutton 4 (momentary 1) was closed, set cheatbutton to 1
    cheatbutton = 1;
  }
  else if (buttonIsPressed(5) && (cheatbutton == 0))
  {
    // cheatbutton 5 (momentary 2) was closed, set cheatbutton to 2
    cheatbutton = 2;
  }
  else if (buttonIsPressed(6) && (cheatbutton == 0))
  {
    // cheatbutton 5 (momentary 3) was closed, set cheatbutton to 3
    cheatbutton = 3;
  }
}

#endif
#endif
