#include <Adafruit_NeoPixel.h>
#include "RTClib.h"

#define RGBLEDPIN D5
#define N_LEDS 120

int hours_switch = D7;
int minutes_switch = D6;
int hours_offset = 0;
int minutes_offset = 0;
int the_hours = 0;
int the_minutes = 0;
bool pm = false;

// initialisation module RTC
RTC_DS3231 rtc;

// initialisation NeoPixel
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, RGBLEDPIN, NEO_GRB + NEO_KHZ800);

// luminosité
int the_brightness = 50;

// couleurs
uint32_t color_white = strip.Color(255, 255, 255);

// tableau des mots
int words[31][15] = {
  
  {0, 1, 3, 4, 5}, // #0 / IL EST
  
  {7, 8, 9}, // #1 / UNE
  {11, 12, 13, 14}, // #2 / DEUX
  {25, 26, 27, 28, 29}, // #3 / TROIS
  {19, 20, 21, 22, 23, 24}, // #4 / QUATRE
  {15, 16, 17, 18}, // #5 / CINQ
  {30, 31, 32}, // #6 / SIX
  {35, 36, 37, 38}, // #7 / SEPT
  {41, 42, 43, 44}, // #8 / HUIT
  {56, 57, 58, 59}, // #9 / NEUF
  {51, 52, 53}, // #10 / DIX
  {45, 46, 47, 48}, // #11 / ONZE
  
  {61, 62, 63, 64, 65}, // #12 / HEURE
  {61, 62, 63, 64, 65, 66}, // #13 / HEURES
  
  {86, 87, 88, 89}, // #14 / MIDI
  {68, 69, 70, 71, 72, 73}, // #15 / MINUIT
  
  {100, 101, 102, 103}, // #16 / CINQ (2)
  {90, 91, 92}, // #17 / DIX (2)
  {83, 84, 110, 111, 112, 113, 114}, // #18 / ET QUART
  {94, 95, 96, 97, 98}, // #19 / VINGT
  {94, 95, 96, 97, 98, 99, 100, 101, 102, 103}, // #20 / VINGT CINQ
  {83, 84, 115, 116, 117, 118, 119}, // #21 / ET DEMIE
  {78, 79, 80, 81, 82, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103}, // #22 / MOINS VINGT-CINQ
  {78, 79, 80, 81, 82, 94, 95, 96, 97, 98}, // #23 / MOINS VINGT
  {75, 76, 78, 79, 80, 81, 82, 110, 111, 112, 113, 114}, // #24 / MOINS LE QUART
  {78, 79, 80, 81, 82, 90, 91, 92}, // #25 / MOINS DIX
  {78, 79, 80, 81, 82, 100, 101, 102, 103}, // #26 / MOINS CINQ
  
  {108}, // #27 / 1 POINT
  {107, 108}, // #28 / 2 POINTS
  {106, 107, 108}, // #29 / 3 POINTS
  {105, 106, 107, 108} // #30 / 4 POINTS
  
};

void setup() {

  Serial.begin(57600);

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  pinMode(hours_switch, INPUT);
  pinMode(minutes_switch, INPUT);
  
  strip.begin();
  strip.show();
  
  strip.setBrightness(the_brightness);

}

void loop() {

  if (digitalRead(hours_switch) == LOW) {
    hours_offset++;
  }
  
  if (digitalRead(minutes_switch) == LOW) {
    minutes_offset++;
  }
   
  strip.clear();

  int what_time_is_it[5] = {};
  what_time_is_it[0] = 0; // IL EST

  DateTime now = rtc.now();

  the_hours = now.hour();
  the_minutes = now.minute();
  
  the_hours += hours_offset;
  the_minutes += minutes_offset;

  while (the_minutes > 59) {
    the_minutes -= 60;
    the_hours++;
  }

  while (the_hours > 23) {
    the_hours -= 24;
  }

  if (the_minutes >= 5 && the_minutes < 10) {
    what_time_is_it[3] = 16; // CINQ (2)
  }
  else if (the_minutes >= 10 && the_minutes < 15) {
    what_time_is_it[3] = 17; // DIX (2)
  }
  else if (the_minutes >= 15 && the_minutes < 20) {
    what_time_is_it[3] = 18; // ET QUART
  }
  else if (the_minutes >= 20 && the_minutes < 25) {
    what_time_is_it[3] = 19; // VINGT
  }
  else if (the_minutes >= 25 && the_minutes < 30) {
    what_time_is_it[3] = 20; // VINGT-CINQ
  }
  else if (the_minutes >= 30 && the_minutes < 35) {
    what_time_is_it[3] = 21; // ET DEMIE
  }
  else if (the_minutes >= 35 && the_minutes < 40) {
    what_time_is_it[3] = 22; // MOINS VINGT-CINQ
  }
  else if (the_minutes >= 40 && the_minutes < 45) {
    what_time_is_it[3] = 23; // MOINS VINGT
  }
  else if (the_minutes >= 45 && the_minutes < 50) {
    what_time_is_it[3] = 24; // MOINS LE QUART
  }
  else if (the_minutes >= 50 && the_minutes < 55) {
    what_time_is_it[3] = 25; // MOINS DIX
  }
  else if (the_minutes >= 55) {
    what_time_is_it[3] = 26; // MOINS CINQ
  }

  if (the_minutes >= 35) {
    the_hours++;
  }

  if (the_hours > 12) {
    the_hours -= 12;
    pm = true;
  }
  else {
    pm = false;
  }

  int the_minutes_last_digit = the_minutes % 10;

  switch (the_minutes_last_digit) {
    
    case 1:
    case 6:
      what_time_is_it[4] = 27; // 1 POINT
      break;
      
    case 2:
    case 7:
      what_time_is_it[4] = 28; // 2 POINTS
      break;
      
    case 3:
    case 8:
      what_time_is_it[4] = 29; // 3 POINTS
      break;
      
    case 4:
    case 9:
      what_time_is_it[4] = 30; // 4 POINTS
      break;
      
  }

  if (the_hours == 12 && !pm) {
    what_time_is_it[1] = 14; // MIDI
  }
  else if (the_hours == 0 || (the_hours == 12 && pm)) {
    what_time_is_it[1] = 15; // MINUIT
  }
  else {
    what_time_is_it[1] = the_hours;
  }

  if (the_hours == 1) {
    what_time_is_it[2] = 12; // HEURE
  }
  else if (the_hours > 1 && the_hours < 12) {
    what_time_is_it[2] = 13; // HEURES
  }
  
  int m = sizeof(words[0]) / sizeof(words[0][0]);
  int n = sizeof(what_time_is_it) / sizeof(what_time_is_it[0]);
  
  for(int j = 0; j < n; j++) {
    for(int i = 0; i < m; i++) {
      strip.setPixelColor(words[what_time_is_it[j]][i], color_white);
    }
  }
   
  strip.show();

  delay(500);
  
}
