#include <Arduino.h>
#define FASTLED_INTERNAL
#include <FastLED.h>
#include "font8x8_basic.h"

#define NUM_LEDS 70
#define WIDTH 70

CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
CRGB leds3[NUM_LEDS];
CRGB leds4[NUM_LEDS];
CRGB leds5[NUM_LEDS];
CRGB leds6[NUM_LEDS];
CRGB leds7[NUM_LEDS];
CRGB leds8[NUM_LEDS];


void clearDisplay() {
  fill_solid(leds1, NUM_LEDS, CRGB::Black);
  fill_solid(leds2, NUM_LEDS, CRGB::Black);
  fill_solid(leds3, NUM_LEDS, CRGB::Black);
  fill_solid(leds4, NUM_LEDS, CRGB::Black);
  fill_solid(leds5, NUM_LEDS, CRGB::Black);
  fill_solid(leds6, NUM_LEDS, CRGB::Black);
  fill_solid(leds7, NUM_LEDS, CRGB::Black);
  fill_solid(leds8, NUM_LEDS, CRGB::Black);
  FastLED.show();
}

void displayChar(char letter, int xOffset) {
    if (xOffset >= WIDTH - 8) return; // Simple boundary check
    Serial.println("setting char " + String(letter));
    int ord = letter; // Get ASCII value
    const char *bitmap = font8x8_basic[ord]; // Get the bitmap for this character

    for (int col = 0; col < 8; col++) {
        for (int row = 0; row < 8; row++) {
            if (bitmap[col] & (1 << row)) {
                // For each bit in the bitmap, decide which LED strip (column) and which LED in the strip (row) to light up
                // Example for the first column (adjust for xOffset and actual pin numbers for your setup):
                if (xOffset + col < WIDTH) { // Check if within the display width
                    switch (col) {
                        case 0: leds1[row] = CRGB::White; break;
                        case 1: leds2[row] = CRGB::White; break;
                        case 2: leds3[row] = CRGB::White; break;
                        case 3: leds4[row] = CRGB::White; break;
                        case 4: leds5[row] = CRGB::White; break;
                        case 5: leds6[row] = CRGB::White; break;
                        case 6: leds7[row] = CRGB::White; break;
                        case 7: leds8[row] = CRGB::White; break;
                        // Add cases for other columns, adjusting for xOffset
                        // You'll need a more dynamic way to select which CRGB array to use based on col + xOffset
                    }
                }
            } else {
                // Similarly, turn off the LED if not part of the character
                if (xOffset + col < WIDTH) {
                    switch (col) {
                        case 0: leds1[row] = CRGB::Black; break;
                        case 1: leds2[row] = CRGB::Black; break;
                        case 2: leds3[row] = CRGB::Black; break;
                        case 3: leds4[row] = CRGB::Black; break;
                        case 4: leds5[row] = CRGB::Black; break;
                        case 5: leds6[row] = CRGB::Black; break;
                        case 6: leds7[row] = CRGB::Black; break;
                        case 7: leds8[row] = CRGB::Black; break;
                    }
                }
            }
        }
    }
    // You'll need to call FastLED.show() after setting all LEDs for a character
    FastLED.show();
    xOffset += 9; // Assuming you move 9 pixels to the right for the next character
}


// Main function to display the AFL score
void displayScore(int goals, int points) {
  clearDisplay(); // Clear the display before showing new score
  
  int total = goals * 6 + points; // Calculate total score
  char scoreStr[20];
  sprintf(scoreStr, "%i.%i %i", goals, points, total); // Format the score string
  Serial.println(scoreStr);
  int xOffset = 0; // Start from the leftmost position of the matrix
  for (int i = 0; scoreStr[i] != '\0'; i++) {
    displayChar(scoreStr[i], xOffset); // Display each character
  }
  
  FastLED.show(); // Update the display with the new score
}

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("Starting...");
  pinMode(5, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(17, OUTPUT);
  pinMode(18, OUTPUT);
  FastLED.addLeds<WS2812B, 5, GRB>(leds1, NUM_LEDS);
  FastLED.addLeds<WS2812B, 12, GRB>(leds2, NUM_LEDS);
  FastLED.addLeds<WS2812B, 13, GRB>(leds3, NUM_LEDS);
  FastLED.addLeds<WS2812B, 14, GRB>(leds4, NUM_LEDS);
  FastLED.addLeds<WS2812B, 15, GRB>(leds5, NUM_LEDS);
  FastLED.addLeds<WS2812B, 16, GRB>(leds6, NUM_LEDS);
  FastLED.addLeds<WS2812B, 17, GRB>(leds7, NUM_LEDS);
  FastLED.addLeds<WS2812B, 18, GRB>(leds8, NUM_LEDS);
}

void loop() {
  Serial.println("Setting score");
  displayScore(8, 4);
  delay(1000);
}