#include <PinButton.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "thermistor.h"
#include "HardwareSerial.h"

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64
#define BUTTON_PIN 5
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
#define NTC_PIN A1

THERMISTOR thermistor(NTC_PIN, 10000, 3950, 10000);
uint16_t waterTemp;

PinButton mb(BUTTON_PIN);
bool tempMode = true;
bool editMode = false;
bool running = false;
unsigned long startTime = 0;
unsigned long currentTime;
unsigned long period;
String tempStatus = "";
String timeStatus = "stopped";
String teaTypes[] = {"green", "black", "oolong"};
int steepTemps[] = {170, 200, 180};
int steepTimes[] = {2, 4, 4};
int teaNum = 0;
String teaType = teaTypes[teaNum];
int steepTemp = steepTemps[teaNum];
int steepTime = steepTimes[teaNum];


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


void setup() {
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  tempDisplay();
}

void loop() {
  mb.update();
  waterTemp = 190;

  if (mb.isLongClick()) {
    switchMode();
  }

  if (mb.isDoubleClick() and tempMode) {
    editMode = !editMode;
    if (editMode) {
      tempEditDisplay();
    }
    else {
      tempDisplay();
    }
  }

  if (mb.isSingleClick() and tempMode and (!editMode)) {
    if (teaNum < 2) {
      teaNum += 1;
    }
    else {
      teaNum = 0;
    }
    teaType = teaTypes[teaNum];
    teaEditDisplay();
    delay(2000);
    tempDisplay();
  }

  if (mb.isSingleClick() and tempMode and editMode) {
    if (steepTemp < 210) {
      steepTemp += 5;
    } else {
      steepTemp = 170;
    }
    tempEditDisplay();
  }

  if (mb.isDoubleClick() and (!tempMode)) {
    editMode = !editMode;
    if (editMode) {
      timeEditDisplay();
    }
    else {
      timeDisplay();
    }
  }

  if (mb.isSingleClick() and (!tempMode) and editMode) {
    if (steepTime < 5) {
      steepTime += 1;
    } else {
      steepTime = 1;
    }
    timeEditDisplay();
  }

  if (mb.isSingleClick() and (!tempMode) and (!editMode)){
    if (running == false) {
      running = true;
      timeStatus = "running";
    } else {
      running = false;
      timeStatus = "stopped";
    }
    timeDisplay();
  }

  if ((!tempMode) and mb.isDoubleClick()) {
    Serial.println("change timer");
  }

}


void checkTemp() {
  if (waterTemp > steepTemp){
    tempStatus = "hot";
  }
  else if ((steepTemp - waterTemp) > 5) {
    tempStatus = "cold";
  }
  else {
    tempStatus = "ready for brewing";
  }
}

void checkTime() {
}


void tempDisplay() {
  checkTemp();
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println(tempStatus);
  display.display();
}

void teaEditDisplay() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println(teaType);
  display.display();
}

void tempEditDisplay() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println(steepTemp);
  display.display();
}

void timeEditDisplay() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println(steepTime);
  display.display();
}

void timeDisplay() {
  checkTime();
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println(timeStatus);
  display.display();
}

void switchMode() {
  if (tempMode) {
      tempMode = !tempMode;
      timeDisplay();
    }
    else {
      tempMode = !tempMode;
      tempDisplay();
    }
  Serial.println("mode changed");
}