#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <IRremote.h>
#include <RTClib.h>
#include <Wire.h>

RTC_DS1307 RTC;
IRrecv IR(3);


// ------PAROLA--------
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 8
#define SPEED_TIME 3
#define PAUSE_TIME  2

#define CLK_PIN   13
#define DATA_PIN  11
#define CS_PIN    10
MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
//-------PAROLA--------

#define mills  60000;


//-----Predefined string that holds the MAX output ------
char D1Text[20] = "";
char D2Text[20] = "";

//------SCENE VARIABLE----------
int scene = 0;

//----------Reset Display function----------
void resetDisplay(){
  P.displayReset(0);
  P.displayReset(1);
}

//-----------------RTC CLOCK FUNCTIONS------------
void getTime(DateTime now) {
  uint8_t h = now.hour() > 12 ? now.hour()-12 : now.hour();
  sprintf(D1Text, "%d:%02d", h, now.minute());
  sprintf(D2Text, "%02d",now.second());
}



void setup(void) {
  //-----S E T U P   I R   R E C I E V E R--------
  IR.enableIRIn();
  Serial.begin(9600);

  //-----S E T U P   R T C--------
  Wire.begin();
  RTC.begin(); 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    RTC.adjust(DateTime(__DATE__, __TIME__));
    delay(1000);
  }
  
  //-----S E T U P  P A R O L A------
  P.begin(2);
  P.setInvert(false);
  P.setZone(0, 0, 3);
  P.setZone(1, 4, 7);
  P.displayZoneText(0, D1Text, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_SCROLL_LEFT);
  P.displayZoneText(1, D2Text, PA_CENTER, SPEED_TIME, PAUSE_TIME, PA_PRINT, PA_SCROLL_LEFT);
}


//timer variables
unsigned long countdownStartTime = 0;
unsigned long countdownDuration = 0;

//start
void startCountdown() {
  countdownStartTime = millis();
}


//------COUNTDOWN TIME SETTING FUNCTION-----------
int h = 0, m = 0;
bool next = false;
void setTime() {
  countdownDuration = ((h*60) + m) * mills;
  sprintf(D1Text, "%d:%02d", h, m);
  next ? sprintf(D2Text, "MIN") : sprintf(D2Text, "HR");
}


//-----COUNTDOWN TIMER FUNCTION------
void countDown() {
  if(countdownStartTime != 0) {
    unsigned long currentTime = millis();
    if(currentTime - countdownStartTime >= countdownDuration) {
      countdownStartTime = 0;
      strcpy(D1Text, "Time");
      strcpy(D2Text, "over!");
    } else {
      unsigned long remainingTime = countdownDuration - (currentTime - countdownStartTime);
      int hours = remainingTime / 3600000;
      int minutes = (remainingTime % 3600000) / 60000;
      int seconds = (remainingTime % 60000) / 1000;
      sprintf(D1Text, "%dh%02dm", hours, minutes);
      sprintf(D2Text, "%02d", seconds);
    }
  } 
}


//------------REMOTE CONTROLLER FUNCTION------------
void RemoteController() {
  if (IR.decode()) {
    long long key = IR.decodedIRData.decodedRawData;
    //Serial.println(IR.decodedIRData.decodedRawData, HEX);

    switch (key) {
      case 0x57A8FF00: // play
       Serial.println("Play");
       if(countdownDuration == 0) {
        scene = 1;
       } else {
        Serial.println(countdownDuration);
        startCountdown();
        scene = 2;
       }
       break;

      case 0x3DC2FF00: // reset
        Serial.println("Reset");
        countdownDuration = 0;
        countdownStartTime = 0;
        h=0;
        m=0;
        scene = 0;
        break;

      case 0x6F90FF00:
        Serial.println("NEXT");
        next = true;
        break;

      case 0x1FE0FF00:
        Serial.println("PREV");
        next = false;
        break;

      case 0xFD02FF00:
        Serial.println("+");
        next ? m+=5 : h++;
        setTime();
        break;

      case 0x6798FF00:
        Serial.println("-");
        if(next && m) m--;
        if(!next && h) h--;
        setTime();
        break;
      
      case 0x9768FF00: //zero
        scene = 0;
        break;
      
      case 0xCF30FF00: //one
        scene = 1;
        break;

      case 0xE718FF00: //two
        scene = 2;
        break;
      default:
        Serial.println("Something messy!");
    }
    delay(100);
    IR.resume();
  }
}


void loop(void) {
  DateTime now = RTC.now(); //RTC TIME CAPTURE
  P.displayAnimate(); // PAROLA ANIMATE
  RemoteController(); //EXECUTING REMOTE CONTROLLER FUNCTION
  resetDisplay(); // RESET DISPLAY EVRYTIME

  //---------SWITCHING DIFFERENT SCENES--------
  switch(scene) {
    case 0:
     //P.setTextEffect(0, PA_SCROLL_UP, PA_SCROLL_UP);
      getTime(now); //REAL TIME CLOCK SCENE
      //getDate(now);
      break;

    case 1:
      setTime(); //COUNTDOWN TIME SETTING SCENE
      break;

    case 2:
      countDown(); //COUNTDOWN TIMER SCENE
      break;

    default:
    Serial.println("Nothing!");
    getTime(now);
  }
}
GND5VSDASCLSQWRTCDS1307+