#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);

// // OLED FeatherWing buttons map to different pins depending on board:
// #if defined(ESP8266)
//   #define BUTTON_A  0
//   #define BUTTON_B 16
//   #define BUTTON_C  2
// #elif defined(ESP32) && !defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
//   #define BUTTON_A 15
//   #define BUTTON_B 32
//   #define BUTTON_C 14
// #elif defined(ARDUINO_STM32_FEATHER)
//   #define BUTTON_A PA15
//   #define BUTTON_B PC7
//   #define BUTTON_C PC5
// #elif defined(TEENSYDUINO)
//   #define BUTTON_A  4
//   #define BUTTON_B  3
//   #define BUTTON_C  8
// #elif defined(ARDUINO_NRF52832_FEATHER)
//   #define BUTTON_A 31
//   #define BUTTON_B 30
//   #define BUTTON_C 27
// #else // 32u4, M0, M4, nrf52840, esp32-s2 and 328p
//   #define BUTTON_A  9
//   #define BUTTON_B  6
//   #define BUTTON_C  5
// #endif

int buttonState = 0;

int lastButtonState = 0;

int buttonPushCounter = 0;

// affirmations go here
String message0 = "KC -3.5            ";
String message1 = "DET -6.5            ";
String message2 = "A. St. Brown Anytime TD            ";
String message3 = "A. Rodgers Over 0.5 INT            ";
String message4 = "C. McCaffery Anytime TD            ";
String message5 = "CHI +3.5            ";
String message6 = "MIA ML            ";
String message7 = "PHI -4.5            ";
String message8 = "D. Henry Over 94.5 RushYDS            ";
String message9 = "L. Jackson Over 45.5 RushYDS          ";
String message10 = "J. Allen Over 2.0 TDs            ";
String message11 = "P. Mahomes Over 284.5 PassYDS            ";
String message12 = "Kelce Anytime TD            ";


String messages[] = {message0, message1, message2, message3, message4, message5, message6, message7, message8, message9, message10, message11, message12};


void setup()
{
  pinMode(2, INPUT_PULLUP);
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);


  Serial.println("128x64 OLED FeatherWing test");
  delay(250); // wait for the OLED to power up
  display.begin(0x3C, true); // Address 0x3C default

  Serial.println("OLED begun");

  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  display.display();
  delay(1000);

  // Clear the buffer.
  display.clearDisplay();
  display.display();

  display.setRotation(1);
  //Serial.println("Button test");

  // pinMode(BUTTON_A, INPUT_PULLUP);
  // pinMode(BUTTON_B, INPUT_PULLUP);
  // pinMode(BUTTON_C, INPUT_PULLUP);

  // text display tests
  display.setTextSize(2);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0,0);
  display.print("Connecting to SSID\n'adafruit':");
  display.print("connected!");
  display.println("IP: 10.0.1.23");
  display.println("Sending val #0");
  display.display(); // actually display all of the above
}

void loop()
{
  // read the pushbutton input pin
  buttonState = digitalRead(2);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is LOW, then the button
      // went from off to on
      buttonPushCounter += 1;
      
      Serial.println("on");
      delay(3000); //wait three seconds
      Serial.println("Printing your sports fortune to the display");
      display.clearDisplay();
      display.setCursor(0,0);
      //display.println("The Packers will win");
      String displayRandomMessage = messages[random(12)];
      Serial.println(displayRandomMessage);
      display.println(displayRandomMessage);
      display.display(); // actually display all of the above
      delay(5000);  //wait five seconds
      display.clearDisplay();
      display.display();

      //Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW, then the button
      // went from on to off
      Serial.println("off");
    }
    // delay a little bit to avoid debouncing
    delay(5); // Wait for 5 millisecond(s)
  }
  // save the current state as the last state, for
  // the next time through the loop
  lastButtonState = buttonState;
  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the devision of two numbers
  //if (buttonPushCounter % 4 == 0) {
  //  digitalWrite(LED_BUILTIN, HIGH);
  //} else {
  //  digitalWrite(LED_BUILTIN, LOW);
  //}
}