#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>

#define DOWN_BUTTON 3
#define UP_BUTTON 2
#define RIGHT_BUTTON 4
#define LEFT_BUTTON 5
#define SPEAKER_PIN  6
int buttons[] = {DOWN_BUTTON, UP_BUTTON, RIGHT_BUTTON, LEFT_BUTTON};
int countdown = 0;
unsigned long milisBreakdown = 0;
int mode_state = 0;

RTC_DS1307 rtc;

LiquidCrystal lcd(12, 11, 10, 9, 8, 7);



void setup() {
  lcd.begin(16, 2);
  // lcd.print("hello world");

  Serial.begin(9600);

  for (int btn : buttons) {
    pinMode(btn, INPUT_PULLUP);
  }
  pinMode(SPEAKER_PIN, OUTPUT);

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
}

int getButtonPress() {
  return getButtonPress(false);
}
int getButtonPress(bool onlyOnce) {
  return getButtonPress(onlyOnce, false);
}
int getButtonPress(bool onlyOnce, bool holdIn) {
  if (onlyOnce) {
    for (int btn : buttons) {
      if (digitalRead(btn) == LOW) {
        while (digitalRead(btn) == LOW && holdIn) {
          tone(SPEAKER_PIN, 400, 11);
          delay(10);
        }
        if (!holdIn) {
          tone(SPEAKER_PIN, 400, 400);
          delay(400);
        }
        noTone(SPEAKER_PIN);
        return btn;
      }
    }
    return 0;
  }
  while (true) {
    for (int btn : buttons) {
      if (digitalRead(btn) == LOW) {
        while (digitalRead(btn) == LOW && holdIn) {
          tone(SPEAKER_PIN, 400, 11);
          delay(10);
        }
        if (!holdIn) {
          tone(SPEAKER_PIN, 400, 400);
          delay(400);
        }
        noTone(SPEAKER_PIN);
        return btn;
      }
    }
  }
}

void render(){
  switch (mode_state) {
    case 0:
      DateTime date = rtc.now();

      char *DateFormat = "DD/MM/YYYY ";
      char *TimeFormat = "hh:mm:ss";

      lcd.setCursor(8, 0);
      lcd.print(date.toString(TimeFormat));
      lcd.setCursor(0, 1);
      lcd.print(mode_state);
      break;
    case 1:
      lcd.setCursor(7, 0);
      lcd.print("00:00:00");
      lcd.setCursor(0, 1);
      lcd.print("<    TIMER     >");
      break;
    case 2:
      lcd.setCursor(7, 0);
      lcd.print("00:00:00");
      lcd.setCursor(0, 1);
      lcd.print("<  STOP WATCH  >");
      break;
    case 3:
      lcd.setCursor(8, 0);
      lcd.print("00:00:00");
      lcd.setCursor(0, 1);
      lcd.print("<  SET  ALARM  >");
      break;
    case 4:
      lcd.setCursor(8, 0);
      lcd.print("00:00:00");
      lcd.setCursor(0, 1);
      lcd.print("<  SET  CLOCK  >");
      break;
  }
}

void run() {
  lcd.clear();
  delay(700);
  render();
}


void loop() {
  // DOWN_BUTTON, UP_BUTTON, RIGHT_BUTTON, LEFT_BUTTON
  switch (getButtonPress(true, true)) {
    case LEFT_BUTTON:
      if (mode_state == 4) mode_state = 0;
      else mode_state++;
      // lcd.clear();
      break;
    case RIGHT_BUTTON:
      if (mode_state == 0) mode_state = 4;
      else mode_state--;
      // lcd.clear();
      break;
    // case UP_BUTTON:
    // case DOWN_BUTTON:
    //   if (mode_state) run();
    //   break;
  }

  render();

  // delay(400);

  

  delay(500);
}

GND5VSDASCLSQWRTCDS1307+