#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>

#define PIN 6
#define WIDTH 16
#define HEIGHT 16

Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(WIDTH, HEIGHT, PIN,
  NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
  NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
  NEO_GRB + NEO_KHZ800);

void setup() {
  matrix.begin();
  matrix.show(); // Initialize all pixels to 'off'
  setupChessboard();
}

void loop() {
  // No need to do anything in the loop for now
}

void setupChessboard() {
  // Set up the initial positions of the pieces
  // Pawns
  for (char col = 'A'; col <= 'H'; col++) {
    lightUpSquare(col, 2, matrix.Color(255, 255, 255)); // White pawns
    lightUpSquare(col, 7, matrix.Color(0, 0, 255)); // Black pawns
  }

  // Rooks
  lightUpSquare('A', 1, matrix.Color(255, 0, 0)); // White rook
  lightUpSquare('H', 1, matrix.Color(255, 0, 0)); // White rook
  lightUpSquare('A', 8, matrix.Color(0, 0, 128)); // Black rook
  lightUpSquare('H', 8, matrix.Color(0, 0, 128)); // Black rook

  // Knights
  lightUpSquare('B', 1, matrix.Color(0, 255, 0)); // White knight
  lightUpSquare('G', 1, matrix.Color(0, 255, 0)); // White knight
  lightUpSquare('B', 8, matrix.Color(0, 128, 0)); // Black knight
  lightUpSquare('G', 8, matrix.Color(0, 128, 0)); // Black knight

  // Bishops
  lightUpSquare('C', 1, matrix.Color(255, 255, 0)); // White bishop
  lightUpSquare('F', 1, matrix.Color(255, 255, 0)); // White bishop
  lightUpSquare('C', 8, matrix.Color(128, 128, 0)); // Black bishop
  lightUpSquare('F', 8, matrix.Color(128, 128, 0)); // Black bishop

  // Queens
  lightUpSquare('D', 1, matrix.Color(255, 0, 255)); // White queen
  lightUpSquare('D', 8, matrix.Color(128, 0, 128)); // Black queen

  // Kings
  lightUpSquare('E', 1, matrix.Color(0, 255, 255)); // White king
  lightUpSquare('E', 8, matrix.Color(0, 128, 128)); // Black king
}

void lightUpSquare(char kolonne, int rad, uint32_t color) {
  int x = kolonne - 'A'; // char er ascii-nummeret til en bokstav
                        // ved å trekke fra "A" så får vi et tall fra 0 til 7
                        // A - A = 0, B - A = 1 osv.
  int y = rad - 1;
  
  // Light up the four pixels in the square
  matrix.drawPixel(x * 2, y * 2, color); // Top-left pixel
  matrix.drawPixel(x * 2 + 1, y * 2, color); // Top-right pixel
  matrix.drawPixel(x * 2, y * 2 + 1, color); // Bottom-left pixel
  matrix.drawPixel(x * 2 + 1, y * 2 + 1, color); // Bottom-right pixel
  
  matrix.show();
}