#include <dummy.h>

//Based on tutorial from https://www.twilio.com/blog/2015/05/light-up-your-hackpack-with-the-adafruit-neomatrix-library.html

//import libraries

#include <Adafruit_GFX.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoMatrix.h>


#include "RGB.h"

//define pin NeoMatrix is connected to
#define PIN 13
 
//initialize 8x8 matrix using the NeoMatrix library accourding to documentaiton

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,
    NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS + NEO_MATRIX_PROGRESSIVE,
    NEO_GRB + NEO_KHZ800);


void setup() {

  //initialize pixel brightness, text color, text wrapping options
  matrix.begin();
  matrix.setBrightness(30);
  matrix.setTextColor( matrix.Color(255, 255, 255) );
  matrix.setTextWrap(false);
}


void colorWipe(RGB color, uint8_t wait) {
  for(uint16_t row=0; row < 8; row++) {
    for(uint16_t column=0; column < 8; column++) {
      matrix.drawPixel(column, row, matrix.Color(color.r, color.g, color.b));
      matrix.show();
      delay(wait);
    }
  }
}

// Fade pixel (x, y) from startColor to endColor
void fadePixel(int x, int y, RGB startColor, RGB endColor, int steps, int wait) {
  for(int i = 0; i <= steps; i++) 
  {
     int newR = startColor.r + (endColor.r - startColor.r) * i / steps;
     int newG = startColor.g + (endColor.g - startColor.g) * i / steps;
     int newB = startColor.b + (endColor.b - startColor.b) * i / steps;

     matrix.drawPixel(x, y, matrix.Color(newR, newG, newB));
     matrix.show();
     delay(wait);
  }
}

//

// Fade full screen  from startColor to endColor
void crossFade(RGB startColor, RGB endColor, int steps, int wait) {
  for(int i = 0; i <= steps; i++) 
  {
     int newR = startColor.r + (endColor.r - startColor.r) * i / steps;
     int newG = startColor.g + (endColor.g - startColor.g) * i / steps;
     int newB = startColor.b + (endColor.b - startColor.b) * i / steps;

     matrix.fillScreen(matrix.Color(newR, newG, newB));
     matrix.show();
     delay(wait);
  }
}

//Draws Twilio logo from tutorial
void drawLogo() {
  int logo[8][8] = {
    {0,0,0,0,0,0,0},
    {0,1,1,0,0,1,1,0},
    {0,1,1,0,0,1,1,0},
    {0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0},
    {0,1,1,0,0,1,1,0},
    {0,0,0,0,0,0,0}
    };

    for(int row = 0; row<8; row++) {
      for (int column = 0; column <8; column++) {
        if(logo[row][column] == 1) {
          fadePixel(column, row, red, white, 120, 5);
        }
      }
    }
}

//scroll text
void scrollText(String textToDisplay) {
  int x = matrix.width();

  // Account for 6 pixel wide characters plus a space
  int pixelsInText = textToDisplay.length() * 7;

  matrix.setCursor(x, 0);
  matrix.print(textToDisplay);
  matrix.show();

  while(x > (matrix.width() - pixelsInText)) {
    matrix.fillScreen(matrix.Color(red.r, red.g, red.b));
    matrix.setCursor(--x, 0);
    matrix.print(textToDisplay);
    matrix.show();
    delay(150);
  }
}

void loop() {
//  matrix.drawPixel(0,0,matrix.Color(red.r, red.g, red.b));
//colorWipe(red, 50);
//matrix.fillScreen(matrix.Color(white.r, white.g, white.b));
//matrix.fillScreen(0);
//fadePixel(0,0, red, white, 100, 50);
//crossFade(red, white, 100, 50);
//drawLogo();
//scrollText("hola");

  crossFade(off, white, 50, 5);
  delay(1000);

  colorWipe(red,50);
  delay(1000);

  drawLogo();
  delay(2000);

  matrix.fillScreen(matrix.Color(red.r, red.g, red.b));
  matrix.show();
  String hola = "this is text";
  scrollText(hola);
  scrollText(hola);
  delay(500);

  crossFade(red, white, 120,5);
  crossFade(white, off, 120,5);
  delay(1000);              
}
