void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait for the serial port to connect
  }

  // Example input strings
  String inputString1 = "m,[6,10,15],[6640,4000, 2500],[1,4200,300,500]";
  String inputString2 = "a,[3,5,8],[1200],[9]";

  // Call the processCommand function with the input string
  processCommand(inputString1);
  processCommand(inputString2);
}

void loop() {
  // Nothing to do in the loop
}

// Function to process the command based on the starting character
void processCommand(String input) {
  // Check the first character to determine the command type
  char commandType = input.charAt(0);
  
  if (commandType == 'm') {
    Serial.println("Command type 'm' detected. Parsing multiple lists...");
    parseAndPrintLists(input.substring(2)); // Pass the substring after "m,"
  } else if (commandType == 'a') {
    Serial.println("Command type 'a' detected. Performing other task...");
    performAnotherTask(input.substring(2)); // Pass the substring after "a,"
  } else {
    Serial.println("Unknown command type.");
  }
}

// Function to parse and print lists (similar to the previous code)
void parseAndPrintLists(String input) {
  // 2D array to store values from the lists
  int lists[3][4];  // Assume a maximum of 4 values per list
  int listSizes[3]; // Array to keep track of the sizes of each list
  
  // Extract the lists and store their sizes
  parseString(input, lists, listSizes);
  
  // Print the lists and their sizes
  for (int i = 0; i < 3; i++) {
    Serial.print("List ");
    Serial.print(i + 1);
    Serial.print(": ");
    printArray(lists[i], listSizes[i]);
    Serial.print(" (Size: ");
    Serial.print(listSizes[i]);
    Serial.println(")");
  }
  if (lists[2][0] != 0) {                              // Check if the motor number is 0 for no sugar ignore it else process sugar accordingly
    Serial.print("Sugar Motor:");
    Serial.print(lists[2][0]);
    Serial.print(", Rotations:");
    Serial.println(lists[2][1]);  // Rotate Stepper according to received steps
  }
  for (int motorCount = 0; motorCount < listSizes[0]; motorCount++) {
    int motorNumber = lists[0][motorCount];  // Get motor number
    int rotations = lists[1][motorCount];    // Get motor rotations
    Serial.print("Motor Number:");
    Serial.print(motorNumber);
    Serial.print(", Moto Rotations:");
    Serial.println(rotations);
  }
}

// Function to perform a different task based on command type 'a'
void performAnotherTask(String input) {
  int singleList[4]; // Assume a maximum of 4 values for simplicity
  int index = 0; // Declare an index variable
  int size = extractList(input, index, singleList); // Pass the variable instead of 0

  Serial.print("Single List (command 'a'): ");
  printArray(singleList, size);
  Serial.print(" (Size: ");
  Serial.print(size);
  Serial.println(")");
}


// Function to parse the input string into multiple lists and store their sizes
void parseString(String input, int lists[][4], int listSizes[]) {
  int currentIndex = 0; // To keep track of where we are in the string
  int listIndex = 0;    // To keep track of the current list being parsed

  // Loop through the string and extract each list
  while (listIndex < 3 && currentIndex < input.length()) {
    listSizes[listIndex] = extractList(input, currentIndex, lists[listIndex]);
    listIndex++;
  }
}

// Function to extract a list from the string and return the number of elements in the list
int extractList(String input, int &index, int outputList[]) {
  int valueCount = 0;

  // Move to the start of the list (skip until '[')
  while (input[index] != '[' && index < input.length()) {
    index++;
  }
  index++; // Skip '['

  // Read numbers until the end of the list
  while (input[index] != ']' && index < input.length()) {
    outputList[valueCount] = readNumber(input, index);
    valueCount++;
    
    // Skip the comma if there is one
    if (input[index] == ',') {
      index++;
    }
  }

  // Move past the closing bracket ']'
  if (input[index] == ']') {
    index++;
  }

  return valueCount; // Return the number of values found in this list
}

// Function to read a number from the string
int readNumber(String input, int &index) {
  String numberString = "";
  while (isDigit(input[index])) {
    numberString += input[index];
    index++;
  }
  return numberString.toInt();
}

// Function to print an array of integers
void printArray(int array[], int size) {
  Serial.print("[");
  for (int i = 0; i < size; i++) {
    Serial.print(array[i]);
    if (i < size - 1) {
      Serial.print(", ");
    }
  }
  Serial.print("]");
}