#include <LiquidCrystal.h>
#include <Keypad.h>
#include <Servo.h>

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

const byte KEYPAD_ROWS = 4;
const byte KEYPAD_COLS = 4;
byte rowPins[KEYPAD_ROWS] = {5, 4, 3, 2};
byte colPins[KEYPAD_COLS] = {A3, A2, A1, A0};
char keys[KEYPAD_ROWS][KEYPAD_COLS] = {
  {'1', '2', '3', '+'},
  {'4', '5', '6', '-'},
  {'7', '8', '9', '*'},
  {'.', '0', '=', '/'}
};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, KEYPAD_ROWS, KEYPAD_COLS);

uint64_t value = 0;

void showSpalshScreen() {
  lcd.print("      Hello");
  lcd.setCursor(3, 1);
  String message = "MyCalculator";
  for (byte i = 0; i < message.length(); i++) {
    lcd.print(message[i]);
    delay(50);
  }
  delay(500);
}

void updateCursor() {
  if (millis() / 250 % 2 == 0 ) {
    lcd.cursor();
  } else {
    lcd.noCursor();
  }
}

// step1
const byte BUTTON_1 = 6;
const byte BUTTON_2 = 0;
const byte BUTTON_3 = 1;
const byte BUTTON_4 = 13;

void setup() {
  Serial.begin(115200);
  lcd.begin(16, 2);

  showSpalshScreen();
  lcd.clear();
  lcd.cursor();

  lcd.setCursor(1, 0);

  // step2
  pinMode(BUTTON_1, INPUT_PULLUP);
  pinMode(BUTTON_2, INPUT_PULLUP);
  pinMode(BUTTON_3, INPUT_PULLUP);
  pinMode(BUTTON_4, INPUT_PULLUP);
}

char operation = 0;
String memory = "";
String current = "";
uint64_t currentDecimal;
bool decimalPoint = false;

double calculate(char operation, double left, double right) {
  switch (operation) {
    case '+': return left + right;
    case '-': return left - right;
    case '*': return left * right;
    case '/': return left / right;
  }
}

// step3
void clearInput() {
  memory = "";
  current = "";
  operation = 0;
  decimalPoint = false;
  lcd.clear();
  lcd.setCursor(1, 0);
}

void deleteCharacter() {
  if (current != "") {
    if (memory != "") {
      current = "";
      lcd.setCursor(1, 1);
      lcd.print("               ");
      lcd.setCursor(1, 1);
    } else {
      current = "";
      lcd.setCursor(1, 0);
      lcd.print("               ");
      lcd.setCursor(1, 0);
    }
  }
}

void toggleSign() {
  if (current != "") {
    if (memory != "") {
      if (current.charAt(0) == '-') {
        current = current.substring(1);
      } else {
        current = "-" + current;
      }
      String c2 = current;
      deleteCharacter();
      current = c2;
      lcd.setCursor(1, 1);
      lcd.print(current);
      lcd.setCursor(current.length() + 1, 1);
    } else {
      if (current.charAt(0) == '-') {
        current = current.substring(1);
      } else {
        current = "-" + current;
      }
      String c2 = current;
      deleteCharacter();
      current = c2;
      lcd.setCursor(1, 0);
      lcd.print(current);
      lcd.setCursor(current.length() + 1, 0);
    }
  }
}

void Pi() {
  if (memory != "") {
    current = String(3.14);
    String c2 = current;
    deleteCharacter();
    current = c2;
    lcd.setCursor(1, 1);
    lcd.print(current);
    lcd.setCursor(current.length() + 1, 1);
  } else {
    current = String(3.14);
    String c2 = current;
    deleteCharacter();
    current = c2;
    lcd.setCursor(1, 0);
    lcd.print(current);
    lcd.setCursor(current.length() + 1, 0);
  }
}


void processInput(char key) {
  if ('-' == key && current == "") {
    current = "-";
    lcd.print("-");
    return;
  }

  switch (key) {
    case '+':
    case '-':
    case '*':
    case '/':
      if (!operation) {
        memory = current;
        current = "";
      }
      operation = key;
      lcd.setCursor(0, 1);
      lcd.print(key);
      lcd.setCursor(current.length() + 1, 1);
      return;

    case '=':
      float leftNum = memory.toDouble();
      float rightNum = current.toDouble();
      memory = String(calculate(operation, leftNum, rightNum));
      current = "";
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print(memory);
      lcd.setCursor(0, 1);
      lcd.print(operation);
      return;
  }

  if ('.' == key && current.indexOf('.') >= 0) {
    return;
  }

  if ('.' != key && current == "0") {
    current = String(key);
  } else if (key) {
    current += String(key);
  }

  lcd.print(key);
}

void loop() {
  updateCursor();

  char key = keypad.getKey();
  if (key) {
    processInput(key);
  }

  // step4
  if (digitalRead(BUTTON_1) == LOW) {
    clearInput();
  }

  if (digitalRead(BUTTON_2) == LOW) {
    deleteCharacter();
  }

  if (digitalRead(BUTTON_3) == LOW) {
    toggleSign();
  }

  if (digitalRead(BUTTON_4) == LOW) {
    Pi();
  }
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
keypad:R1
keypad:R2
keypad:R3
keypad:R4
keypad:C1
keypad:C2
keypad:C3
keypad:C4
lcd:VSS
lcd:VDD
lcd:V0
lcd:RS
lcd:RW
lcd:E
lcd:D0
lcd:D1
lcd:D2
lcd:D3
lcd:D4
lcd:D5
lcd:D6
lcd:D7
lcd:A
lcd:K
r1:1
r1:2
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
r2:1
r2:2
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
r3:1
r3:2
r4:1
r4:2
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
r5:1
r5:2