#include <vector>
char* c = "char";

String s = "string";

char n = '\0';
char o = NULL;
char p = 'p';

using namespace std;





const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newui = false;

int dataNumber = 0;             // new for this version

void ParseInput(char * ti)
{
  //if(!)

}

char *FindStrings(char* t)
{
  /* The purpose of this function is to iterate through a strcture of
  Arrays to check if any of the " "(space parsed input) matches with any of 
  the menu options*/

  static int s = strlen(t);

  static char *input = NULL;

  char* result = new char[s + 1];

  if( t != NULL ) 
  {
    input = t;
  }

  //Used after the final token is found
  if(input == NULL)
  {
    //while("char used for token" != "\0")
    return "\0";
  }
    
    int i =0;

      for(; input[i] < s; i++)
      {
        if(input[i] != ' ' && input[i] != '\0')
          {
            result[i] = input[i];
          }

        else if(i == s)
          {
            result[i] = input[i];
            result[i+1] = '\0';
            break;
          }

        else if(input[i] == ' ')
          {
            result[i] = '\0';
            input = input + i + 1;
            return result;
          }
      }
    input = NULL;
    return result;
  }


void ReadSerial() 
{
    char UI[100] = "";
    static byte ndx = 0;
    char rc;
    char marker = '\n';
    
    while(Serial.available() > 0 && newui == false) 
    {
      rc = Serial.read();

        if (rc != marker) 
        {
          UI[ndx] = rc;
          ndx++;

            if (ndx >= numChars) // If input is greater than allotted memory
            {
              ndx = numChars - 1; // Expand the memory to meed the needs of the input
            }
        }
        
        else if( rc == marker)
        {
          UI[ndx] = '\0'; // terminate the string
          ndx = 0;        // Zero the index number
          newui = true;   // Flag new input as true
        }
    }

    Serial.println(UI);

    char *ui;
    strcpy(ui, UI);

    memset(UI,0,sizeof(UI));

    if(newui == true) 
    {
      char * token = FindStrings(ui);
      printf("token = %s\n", token);
    }
}



void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");
};


void loop() 
{
  delay(10); // this speeds up the simulation
  if(Serial.available() > 0)
  {
  ReadSerial();
  }
}