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


#include "SegmentDisplayChars.h"

#define CharPipeLeft SegmentE | SegmentF
#define CharPipeRight SegmentB | SegmentC


struct Frame
{
  uint32_t time;
  char frame[6];
};


Frame const animation[] = {
  // Snake towards the middle
  { 100, { SegmentF, 0, 0, 0, 0, SegmentB } },
  { 100, { SegmentF | SegmentE, 0, 0, 0, 0, SegmentB | SegmentC } },
  { 100, { SegmentE | SegmentD, 0, 0, 0, 0, SegmentC | SegmentD } },
  { 100, { SegmentD | SegmentC, 0, 0, 0, 0, SegmentD | SegmentE } },
  { 100, { SegmentC | SegmentB, 0, 0, 0, 0, SegmentE | SegmentF } },

  { 100, { SegmentB, SegmentF, 0, 0, SegmentB, SegmentF } },
  { 100, { 0, SegmentF | SegmentE, 0, 0, SegmentB | SegmentC, 0 } },
  { 100, { 0, SegmentE | SegmentD, 0, 0, SegmentC | SegmentD, 0 } },
  { 100, { 0, SegmentD | SegmentC, 0, 0, SegmentD | SegmentE, 0 } },
  { 100, { 0, SegmentC | SegmentB, 0, 0, SegmentE | SegmentF, 0 } },

  { 100, { 0, SegmentB, SegmentF, SegmentB, SegmentF, 0 } },
  { 100, { 0, 0, SegmentF | SegmentE, SegmentB | SegmentC, 0, 0 } },
  { 100, { 0, 0, SegmentE | SegmentD, SegmentC | SegmentD, 0, 0 } },
  { 100, { 0, 0, SegmentD | SegmentC, SegmentD | SegmentE, 0, 0 } },
  { 500, { 0, 0, SegmentC | SegmentB, SegmentE | SegmentF, 0, 0 } },

  // Slide open to reveal 'you'
  { 100, { 0, 0, CharPipeLeft | CharO, CharU | CharPipeRight, 0, 0 } },
  { 100, { 0, CharPipeRight, CharO, CharU, CharPipeLeft, 0 } },
  { 100, { 0, CharPipeLeft | CharY, CharO, CharU, CharPipeRight, 0 } },
  { 100, { CharPipeRight, CharY, CharO, CharU, 0, CharPipeLeft } },
  { 100, { CharPipeLeft, CharY, CharO, CharU, 0, CharPipeRight } },
  { 500, { 0, CharY, CharO, CharU, 0, 0 } },

  // The rest of the text
  { 750, { 0, CharL, CharO, CharS, CharT, 0 } },
  { 750, { 0, 0, CharT, CharH, CharE, 0 } },
  { 500, { SegmentDP, CharG, CharA | SegmentDP, CharM, CharE | SegmentDP, 0 } },
  { 500, { 0, CharG | SegmentDP, CharA, CharM | SegmentDP, CharE, SegmentDP } },
  { 500, { SegmentDP, CharG, CharA | SegmentDP, CharM, CharE | SegmentDP, 0 } },
  { 500, { 0, CharG | SegmentDP, CharA, CharM | SegmentDP, CharE, SegmentDP } },
  { 500, { SegmentDP, CharG, CharA | SegmentDP, CharM, CharE | SegmentDP, 0 } },
  { 500, { 0, CharG | SegmentDP, CharA, CharM | SegmentDP, CharE, SegmentDP } },
  { 500, { SegmentDP, CharG, CharA | SegmentDP, CharM, CharE | SegmentDP, 0 } },
  { 500, { 0, CharG | SegmentDP, CharA, CharM | SegmentDP, CharE, SegmentDP } },

  { 0, { 0, 0, 0, 0, 0, 0 } }
};



void LoseTheGameState::setup()
{
  animationTime = currentTime;
  animationStep = 0;
}

void LoseTheGameState::loop()
{
  if (buttonA->pressed() || buttonB->pressed())
  {
    setCurrentState(new DefaultState());
  }

  if (currentTime - animationTime > animation[animationStep].time)
  {
    animationStep++;
    animationTime = currentTime;

    if (animation[animationStep].time == 0)
      setCurrentState(new DefaultState());
  }

  display->writeRaw(animation[animationStep].frame);
}