/*                                          Basic Receiving
Name: Joshua Adams
Date: 4/22/24
Purpose of program:  To control whether certain functions run or not.

Requirements 
 - Global integers with the value of one 
 - Conditional if statements in he loop that 
 - Conditional statements allow the function to run if the value is 1

*/

//********libraries********
//****LEDS****
#include <Arduino.h>
//#include <FastLED.h>
#include "parser.h"
//#include "ledgfx.h"
//#include <LittleFS.h>
//#include <FS.h>
//#include <WiFi.h>
//#include <ESPmDNS.h>
//#include <iostream>

//using namespace std;


//#define STA_SSID "Bald Chicken"
//#define STA_PASS "Nathaniel10"
/*
#define AP_SSID "ESP Tester"
#define AP_PASS "Nathaniel10"


const uint32_t connectTimeoutMs = 10000;

#define FORMAT_LITTLEFS_IF_FAILED tru
//****LED Definitions****
  #define numleds 50
  #define ledpin 33

//****LEDS****
  CRGB leds[numleds] = {0};
  */
  uint8_t brightness = 15;
  //int maxbrightness = 40;
  uint8_t fadeBy;
  byte hue1 = 64;
  byte red = 80;
  byte green = 80;
  byte blue = 80;
  byte candleDelay = 10;
  byte r1 = 40;
  byte g1 = 40;
  byte b1 = 0;

  byte rPlus = 0;
  byte gPlus = 0;
  byte bPlus = 0;
  byte rMinus = 2;
  byte gMinus = 2;
  byte bMinus = 0;

  int primaryFunction = 1;
  int secondaryFunction = -1;
  int tertiaryFunction = -1;
  
//delays
  float fadeInDelay = 3;
  float fadeOutDelay = 3;

  uint32_t f1Delay1 = 10;
  uint32_t f1Delay2 = 10;
  uint32_t f1Delay3 = 5;

//Time
  unsigned long serialReadStart;
  double timeAtLast;
  double timeSinceLast;
  double hour;
  
  const char *hostName = "LED Tester";

/*
void initDNS() {
  if (!MDNS.begin(hostName)) {
    Serial.println("MDNS responder started");
  }
   //server.on("/", [](AsyncWebServerRequest * request)
   //{ 
   //request->send_P(200, "text/html", site);
   //});
}
void initAP() {
    WiFi.enableAP(true);
    Serial.println("Initializing AP");
    WiFi.softAPConfig(AP_LOCALIP, AP_GATEWAY, AP_SUBNET, AP_DHCP_LEASE_START);
      // You can remove the password parameter if you want the AP to be open.
      // a valid password must have more than 7 characters
    if (!WiFi.softAP(AP_SSID, AP_PASS)) {
      log_e("Soft AP creation failed.");
      while(1);
      }
      //IPAddress local_ip = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(AP_LOCALIP);
    }

// Initialize WiFi
void initWiFi() {
  WiFi.disconnect(false, true);
  WiFi.persistent(false);
  WiFi.setAutoReconnect(true);
  WiFi.setSleep(false);
  WiFi.mode(WIFI_STA);
  //WiFi.setHostname(hostName);
  WiFi.config(STA_LOCALIP, STA_GATEWAY, STA_SUBNET, STA_DNS1, STA_DNS2);
  WiFi.begin(STA_SSID, STA_PASS);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi");
    Serial.println("Please Wait.....");
    delay(500);
    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("Connected to WiFi.");
      printf("Connected to WiFi");
      break;      
      }
    }
  }

void SetHue(int hue) {
  for(int i = 0; i < numleds; i++) { leds[i].setHue(hue);}
  FastLED.show();
 }

void SetAll(int f, byte red, byte green, byte blue) {
    for(int i = 0; i < numleds; i++) {
      leds[i].r = red;
      leds[i].g = green;
      leds[i].b = blue;
    }
    FastLED.show();
  }
*/
void SetAll(byte red, byte green, byte blue) {
    //for(int i = 0; i < numleds; i++) {
    //  leds[i].r = red;
    //  leds[i].g = green;
    //  leds[i].b = blue;
   // }
    //FastLED.show(); 
  } 

  /*

void CandleFlame() {
  //bright yellow r,g,b = 255,255,0
  uint8_t myRandom;
  RandomNum(myRandom);
  int ledPos = myRandom;

  for(int i = 0; i < 5; i++) {
    leds[ledPos + i].setRGB(r1,g1,b1);
    leds[ledPos - i].setRGB(r1,g1,b1);
    for(int j = 0; j <= i; j++){
      //leds[j].CRGB::addToRGB(3);
      leds[j].r + rPlus;
      leds[j].g + gPlus;
      leds[j].b + bPlus; 
    }
      delay(candleDelay);
    FastLED.show();
    }
  
  for(int k = 0; k <= numleds; k++) {
    leds[k].CRGB::subtractFromRGB(5);
    }
  }

void WhiteFlameBackup(byte red, byte green, byte blue)  { 
  for(int i = 0; i < numleds; i++) {  //loop #1 Drawing one led and moving to the nexr loop
    leds[i].r = f[1].r;
    leds[i].g = f[1].g;
    leds[i].b = f[1].b;
  for(int j = 0; j <= i; j++)  //Loop through only what the main loop has drawn so not to draw past the fade. Adds one pixel per cycle
    leds[j].CRGB::addToRGB(2);  //Adding one per cycle along with additional brightness to each pixel previously drawn appears to fade in
    FastLED.show();
    delay(f1Delay1);
  }
  while(leds->getLuma() > 0) {  //  loop #3
    for(int k = 0; k <= numleds - 1; k++) 
        leds[k].CRGB::subtractFromRGB(2); // 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(f1Delay2);      
    }  
 }

void WhiteFlame(byte red, byte green, byte blue)  { 
  for(int i = 0; i < numleds; i++) {  //loop #1 Drawing one led and moving to the nexr loop
      leds[i].r = leds[i].r;
      leds[i].g = leds[i].g;
      leds[i].b = leds[i].b;
    for(int j = 0; j < numleds; j++)  //loop #2 assigning brightness 0 to j and iterating to 50
      leds[j].CRGB::addToRGB(2)+leds[j+1].CRGB::addToRGB(1);  // Due to drawing one led and raising the brightness by 3 each step it appears to fade in
      FastLED.show();
      delay(f1Delay1);
    }
    //Serial.println("Light: "+String(leds->getLuma()));
    //while(leds->getLuma() > 0) {  //  loop #3
    //  for(int k = 0; k <= numleds - 1; k++) 
    //      leds[k].CRGB::subtractFromRGB(2); // 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(f1Delay2);      
    //}
      for(int k = 0; k <= numleds - 1; k++) {
        leds[k].r = leds[k].r;
        leds[k].g = leds[k].g;
        leds[k].b = leds[k].b;
        for(int l = 0; l <= numleds; l++) {
          leds[l].CRGB::subtractFromRGB(3)+leds[l+1].CRGB::subtractFromRGB(1);
        }
        FastLED.show();
        delay(f1Delay2);
      }
  }
*/
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");
    delay(10);
  //LEDS
    //pinMode(ledpin, OUTPUT);
    //pinMode(LED_BUILTIN, OUTPUT);
    delay(100);
  //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_POWERLIMITER
    //set_max_power_indicator_LED(LED_BUILTIN);
   // FastLED.setMaxPowerInMilliWatts(900);
    //Serial.println("Clearing LED's.");
    //FastLED.clear();
    //FastLED.show();

    //initAP();
    //initWiFi();
    //initDNS();
  //
    f[1].r = 40;
    f[1].g = 40;
    f[1].b = 0;

  }
  

void loop() { 
   if(Serial.available() > 0) {
    Serial.println("Strip1 off, parsing incoming serial communication!");
    track.readingSerial = true;
    ReadSerial();
    }
  
  if(primaryFunction == 1 && track.readingSerial == false) {
      //WhiteFlame(red, green, blue);
      //WhiteFlameBackup(f[1].r, f[1].g, f[1].b);
    }
  
  if(secondaryFunction == 1 && track.readingSerial == false) {
      //CandleFlame();
    }

  if(tertiaryFunction == 1 && track.readingSerial == false) {
      
    }
    
  }