#include "tm.h"
#include "vector.h"

// Declare a global vector for key-value pairs
Vector<int, 6> keyValuePairs;  // Assuming maximum string length of 6 characters

// Define pin and display classes (similar to Python classes Pin and TM1637)

// Define delay in microseconds
#define DELAYTENUS 10

// Define constants
const int loopDigitMax = 4;
const int loopMax = 16;
const int shiftOutputCount = 32;
uint16_t countup[8];
int brightness = 1;


TM1637 display;

// Helper function to check if a character is a digit
bool isDigit(char c) {
  return (c >= '0' && c <= '9');
}

// Helper function to convert a character to its corresponding digit
int charToDigit(char c) {
  return c - '0';
}

// Helper function to convert a digit to its corresponding character
char digitToChar(int digit) {
  return digit + '0';
}

// Helper function to convert an integer to a string
void intToString(int value, char* buffer, int bufferSize) {
  snprintf(buffer, bufferSize, "%d", value);
}

void fourDigitNumber(const char* number, char* result) {
  if (number[0] == 'N' && number[1] == 'A' && number[2] == '\0') {
    result[0] = 'N';
    result[1] = 'A';
    result[2] = '\0';
    return;
  }

  int digitCount = 0;
  int startIndex = 0;

  // Skip leading zeros
  while (number[startIndex] == '0') {
    startIndex++;
  }

  // If the entire number is zeros, pad with spaces
  if (number[startIndex] == '\0') {
    result[0] = ' ';
    result[1] = ' ';
    result[2] = ' ';
    result[3] = ' ';
    result[4] = '\0';
    return;
  }

  // Calculate the number of digits in the input number
  int inputLength = strlen(number + startIndex);

  // If there are non-zero digits, copy them to the end of the result
  for (int i = 0; i < inputLength && digitCount < 4; ++i) {
    result[3 - i] = number[inputLength - 1 - i];
    digitCount++;
  }

  // If less than four digits were extracted, pad the start of the result with spaces
  while (digitCount < 4) {
    result[3 - digitCount] = ' ';
    digitCount++;
  }
  result[4] = '\0';
}


void setup() {
    display.brightness(7);
  Serial.begin(115200); // Any baud rate should work
  // Serial.println("Hello Arduino\n");
  // Initialize your display pins
  display.initializePins();

  for (int i = 0; i < 32; ++i) {  
  countup[i] = 1;
  }
}

void loop() {



  // if (brightness < 7) {
  //   brightness++;
  // } else {
  //   brightness = 0;
  // }  // Buffer to hold the string representation of countup
  //     display.brightness(brightness);

  delay(200);
  keyValuePairs.clear();
  char countupString[5];
  char result[5];
  for (int i = 0; i < 32; ++i) {                                     // Buffer to hold the string representation of countup
    intToString(countup[i], countupString, sizeof(countupString));  // Convert countup to string
    fourDigitNumber(countupString, result);                         // Convert countupString to a four-digit number string
    keyValuePairs.push_back(KeyValuePair<int, 6>(i, result));
    if (countup[i] < 9999) {
      countup[i]++;
    } else {
      countup[i] = 1;
    }  // Buffer to hold the string representation of countup
  }


  display.show(keyValuePairs);  // Display the result
}
74HC595
74HC595
4-Digit Display
4-Digit Display
4-Digit Display
4-Digit Display
4-Digit Display
4-Digit Display
4-Digit Display
4-Digit Display
4-Digit Display
74HC595
74HC595
74HC595
74HC595
74HC595
74HC595