//********libraries********
//****LEDS****
#include <Arduino.h>
#include <FastLED.h>

#define FASTLED_INTERRUPT_RETRY_COUNT 1
//#include <wifi_setup.h>
//********DEFINITIONS********
//****LEDS****
  #define numleds 20
  #define ledpin 33
  #define AUTO_RECONNECT true
  #define numcolors 6

//********VARIABLES********
  int lightning[5];
  float fadeInDelay = 3;
  float fadeOutDelay = 3;
  int l;
  uint8_t lastl;
  uint8_t lastr;
  uint8_t lastg;
  uint8_t lastb;
  uint8_t hue = 0;

//****LEDS****
  CRGB leds[numleds] = {0};
  int brightness = 75;
  //int maxbrightness = 40;
  uint8_t fadeBy;
  byte red = 80;
  byte green = 80;
  byte blue = 80;
  byte r1 = numleds;
  byte g1 = numleds;
  byte b1 = numleds; 
  int debugCounter = 0;

//Variables for new parsing
  char serialIn[10][20];

//Function control variables Multiplied by -1 to toggle from 1 to -1 to 1 and so on. Then used as a flag in an if statementto activate the functions
  int primaryFunction = 1;
  int secondaryFunction = 1;
  int tertiaryFunction = 1;
  
//chars for use in parsing  
  char strings[10][20];//Captures initial delimited input
  char section[10][15];
  char sec[10][15];
  int funct1 = 1;
  int funct2 = 1;
  int effect1 = 1;
  int effect2 = 1;
  int effect3 = 1;
  
  bool passed = false;
 
  uint8_t f1Delay1 = 50;
  uint8_t f1Delay2 = 10;
  uint8_t f1Delay3 = 5;

  uint8_t f2Delay1 = 10;
  uint8_t f2Delay2 = 10;
  uint8_t f2Delay3 = 10;
  uint8_t f2Delay4 = 10;

  uint8_t f3Delay1 = 10;
  uint8_t f3Delay2 = 10;
  uint8_t f3Delay3 = 2;
  uint8_t f3Delay4 = 50;
  uint8_t f3Delay5 = 50;

//Command variables   
  //Section 1  -  Choose what you would like to interact with
  char sec0[6][10] =  //  section[0]
    {
    "get",      //  [0]
    "set",      //  [1] 
    "clear",
    "read",
    "write",
    };
  char sec1[5][10] =    //  section[1]
    {
    "function",
    "effect",
    "strip",
    "spiffs",       
    };
  char sec2[2][15] =   
    {

    };
  char sec3[8][10] =
    {
    "fillall",
    "bright", 
    "fade",
    "rgb",
    "speed",
    "hue",
    "delay",            
    };
  char sec4[12][10] =
    {
    "yes","no",      
    "on","off",
    "red","green","blue",      
    "add","subtract",    
    "up","down",
    };
  char functionControl = 'f';



    void ReadSerial() {
    while(Serial.available() > 0) {
      char s1[] = "";
      size_t numBytes = Serial.readBytesUntil('\n', s1, sizeof(numBytes) - 1);
      s1[numBytes] = '\0';

      strncpy(serialIn[0], s1, sizeof(numBytes) - 1);
      Serial.println("serial In: "+String(serialIn[0]));

      
      //Serial.println(s2[0]);

    /*For the below to work you must have comparison statements
    that allow the function to call when = to 1 The comparisons are
    (set to zero to start the program with disabled)
    int primaryFunction = 1;
    int secondaryFunction = 1;
    int tertiaryFunction = 1;
    When called with f1(primary), f2(secondary) f3(tertiary)
    you multiply the 1 by -1 every time to jump between 1 and -1 
    */
      if(serialIn[0][0] == functionControl) {
          int funct = atoi(&serialIn[0][1]);
          if(funct == 1) { 
            primaryFunction *= -1; 
            Serial.println("Primary Function switched");
            }
          if(funct == 2) { 
            secondaryFunction *= -1; 
            Serial.println("Secondary Function switched");
            }
          if(funct == 3) { 
            tertiaryFunction *= -1; 
            Serial.println("Tertiary Function switched");
            }
          memset(serialIn[0], 0, sizeof(serialIn[0]));
        }
      }
    }
    
  //}

void WhiteFlame(byte red, byte green, byte blue)  { 
 // Serial.println("White Flame");
  //int light = leds->getLuma();  // Getting the brightness current brightness at 0
  for(int i = 0; i < numleds; i++) {  //loop #1 Drawing one led and moving to the nexr loop
      leds[i].r = red;
      leds[i].g = green;
      leds[i].b = blue;
      //Serial.println("Starting loops 1 and 2");
    for(int j = leds->getLuma(); j < brightness; j++) //loop #2 assigning brightness 0 to j and iterating to 50
      leds[j].CRGB::addToRGB(3);  // Due to drawing one led and raising the brightness by 3 each step it appears to fade in
      FastLED.show();
      delay(10);
    }
    while(leds->getLuma() > 0) {  //  loop #3
    //Serial.println("Starting loops 3 and 4");
      for(int k = 0; k < numleds; k++) 
          leds[k].CRGB::subtractFromRGB(3); // loop #4 Causes the fade out due to he 3 getting higher on each iteration, for at he end it's almost faded all ou5
          FastLED.show();
          delay(30);      
    }
  }

void setup() 
  {
    //Serial.setTimeout(3000);
    Serial.begin(115200);
    delay(500);
    Serial.println("Starting Serial Communications");

    ulong quitter = Serial.getTimeout();
    Serial.println("Serial Timeout is: "+String(quitter));
    
    Serial.println("Setting up defined pins");
    //****LEDPINS****
    //pinMode(ledpin, OUTPUT);
    //pinMode(LED_BUILTIN, OUTPUT);

    Serial.println("Setting up LED Constructor");
    //****LED_CONSTRUCTOR****
    FastLED.addLeds<WS2812B, ledpin, GRB>(leds, numleds);
    FastLED.setBrightness(brightness);

    Serial.println("Setting up Power Limits");
    //****LED_POPWERLIMITER****
    //set_max_power_indicator_LED(LED_BUILTIN);
    FastLED.setMaxPowerInMilliWatts(900);

    Serial.println("Clearing LED's.");
    FastLED.clear();
    FastLED.show();
      }
void loop() { 

  ReadSerial();

  red = 80;
  green = 80;
  blue = 80;
 
  if(primaryFunction == 1) {

    WhiteFlame(red, green, blue);
    //primaryFunction = 0;
  }

  secondaryFunction = 0;  

  if(secondaryFunction == 1) {

    WhiteFlame(red, green, blue);
    //secondaryFunction = 0;
  }

  tertiaryFunction = 0;

  if(tertiaryFunction == 1)
    {
      WhiteFlame2(red, green, blue);
      //tertiaryFunction = 0;
    }
  }