#include "RealTimeClock.h"
#include "DigitDisplay.h"
#include "StatusLed.h"
//#include "LedWheel.h"
#include "Structs.h"
#include "Color.h"


// Initialize components
RealTimeClock rtc;
DigitDisplay display(18, 19);
//LedWheel ledring(36, 17);
StatusLed ledLeft(25);
StatusLed ledRight(26);

Color blue(50, 100, 150, 1.0);
Phase nightPhase;
Time now;


void setup() {
  Serial.begin(9600);
  // Set Night phase
  nightPhase.startH = 22;
  nightPhase.endH = 6;
  // Start and sync realtime clock
  rtc.setup(&display);
  // Setup LED ring
  /*ledring.reset();
  if(!rtc.isNight(nightPhase)){
    ledring.setBrightness(0.8);
  } else {
    ledring.setBrightness(0.0);
  }*/


  /* Startup effects if it is not night
  if(!rtc.isNight(nightPhase) && false){
    ledring.rotate(2, 0.075);
    ledring.fadeWheel(1);
    ledring.fadeWheel(1);
  } */
}

void loop() {
  Serial.println("Next loop...");
  now.minute = rtc.getMinute();
  now.hour = rtc .getHour(); 

  // Check if it is night
  if(rtc.isNight(nightPhase)){
    display.setBrightness(5);
    display.displayTime(now.hour, now.minute);

    // Delay by the remaining milliseconds
    //delay((60 - rtc.getSecond()) * 1000);
    //return;
  } else { // Play effects at daytime
    if( now.minute == 0 ){ // If a new hour started
      display.setBrightness(display.getBrightness() + 2);
      delay(1000);
      display.setBrightness(display.getBrightness() - 2);
    } else if ( now.minute == 30 ){ // If another half hour passed
      display.setBrightness(display.getBrightness() + 1);
      // Fade around the LED Ring
      //ledring.fadeAround(ledring.getHandPos(now.hour, now.minute), 4, 0.075);
      delay(1000);
      // Fade back to the new hand position
      display.setBrightness(display.getBrightness() - 1);
    } else if ( now.hour == 0 && now.minute == 0 ){ // At midnight
      // Update realtime clock
      rtc.resync();
    } else {
      ;
      //ledring.showHands(now.hour, now.minute, 0.75f, 0.075f);
      //ledring.fadeAround(ledring.getHandPos(now.hour, now.minute), 4, 0.075);
      //ledring.rotate(5, 0.075);
    }
  }

  display.displayTime(now.hour, now.minute);

  // Delay by the remaining milliseconds
  delay((60 - rtc.getSecond()) * 1000);
} 
4-Digit Display