#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
  #define tamanoMax 9
  int numeroEntero = 0;
  char textoChar[tamanoMax];
  String textoString = String(numeroEntero);
  int margenIzq;
  int caracteresNumero;
  int cantSol = 0;
  int cantSurt = 0;
  String colorBoton = "";
  int oldValVer ;
  int oldValAzu ;
  int oldValAma ;
  int oldValRoj ;
  int oldValNeg ;
  int newValVer ;
  int newValAzu ;
  int newValAma ;
  int newValRoj ;
  int newValNeg ;
// Declaramos el pin al que estará conectado el Push Button Verde
// Un pulso indicará 'Cantidad Surtida'
int pinButtonVerde = 33; //8  32
// Declaramos el pin al que estará conectado el Push Button Azul
// Un pulso indicará 'Incremento de Cantidad Surtida'
int pinButtonAzul = 32; //7   35
// Declaramos el pin al que estará conectado el Push Button Verde Rojo
// Un pulso indicará 'Surtido Nulo'
int pinButtonRojo = 26; //11   26
// Declaramos el pin al que estará conectado el Push Button Amarillo
// Un pulso indicará 'Decrecer Cantidad Surtida'
int pinButtonAmarillo = 27;  //14   12
// Declaramos el pin al que estará conectado el Push Button Negro
// Un pulso indicará 'Requerimiento de Reabasto'
int pinButtonNegro = 25;  //10   25
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Surtido Spick ESP32 y OLED!");
  
  //Declaramos los Pines de los 5 Push Button como Salida
  pinMode(pinButtonVerde, INPUT);
  pinMode(pinButtonAzul, INPUT);
  pinMode(pinButtonRojo, INPUT);
  pinMode(pinButtonAmarillo, INPUT);
  pinMode(pinButtonNegro, INPUT);
  oldValVer = LOW;
  oldValAzu = LOW;
  oldValAma = LOW;
  oldValRoj = LOW;
  oldValNeg = LOW;
  newValVer = LOW;
  newValAzu = LOW;
  newValAma = LOW;
  newValRoj = LOW;
  newValNeg = LOW;
  colorBoton = "";
  u8g2.begin();
  //u8g2.setFont(u8g2_font_ncenB08_tr); // Establecer la fuente del texto
  u8g2.setFont(u8g2_font_logisoso26_tf); // Establecer la fuente del texto
  u8g2.setFontMode(1); // Modo de texto transparente
  u8g2.setFontDirection(0); // Dirección del texto
  //refrescaPantalla();
  Serial.println("---------------- Inicio de Surtido --------------------");
}
void loop() {
  // Inicializa colorBoton
  colorBoton = ""; 
  // Lee el estado de los 5 Push Button
  newValVer = digitalRead(pinButtonVerde);
  newValAzu = digitalRead(pinButtonAzul);
  newValAma = digitalRead(pinButtonAmarillo);
  newValRoj = digitalRead(pinButtonRojo);
  newValNeg = digitalRead(pinButtonNegro);
  //El Push Button Verde se ha presionado
  if (newValVer != oldValVer)  {
    colorBoton = "Verde";
    delay(10);
  } 
  //El Push Button Azul se ha presionado
  if (newValAzu != oldValAzu)  {
    colorBoton = "Azul";
    delay(10);
  } 
  //El Push Button Amarillo se ha presionado
  if (newValAma != oldValAma)  {
    colorBoton = "Amarillo";
    delay(10);
  } 
  //El Push Button Rojo se ha presionado
  if (newValRoj != oldValRoj)  {
    colorBoton = "Rojo";
    delay(10);
  } 
  //El Push Button Negro se ha presionado
  if (newValNeg != oldValNeg)  {
    colorBoton = "Negro";
    delay(10);
  } 
    // if(colorBoton != ""){
    //   Serial.print("  ");
    //   Serial.print(newValVer);
    //   Serial.print("  ");
    //   Serial.print(newValAzu);
    //   Serial.print("  ");
    //   Serial.print(newValAma);
    //   Serial.print("  ");
    //   Serial.print(newValRoj);
    //   Serial.print("  ");
    //   Serial.println(newValNeg);
      // newValVer = LOW;
      // newValAzu = LOW;
      // newValAma = LOW;
      // newValRoj = LOW;
      // newValNeg = LOW;
      // oldValVer = LOW;
      // oldValAzu = LOW;
      // oldValAma = LOW;
      // oldValRoj = LOW;
      // oldValNeg = LOW;
    //   colorBoton = "";
    // }
      if (colorBoton != ""){
        if (colorBoton == "Verde") {
            Serial.println("Registra Cantidad Surtida: " +  String(cantSurt));
        }
        if (colorBoton == "Azul") {
            cantSurt = cantSurt + 1;
            Serial.println("Aumenta Cantidad Surtida a: " + String(cantSurt));
        }
        if (colorBoton == "Amarillo") {
            cantSurt = cantSurt - 1;
            Serial.println("Disminuye Cantidad Surtida a: " + String(cantSurt));
        }
        if (colorBoton == "Rojo") {
            Serial.println("Registra Surtido Nulo");
        }
        if (colorBoton == "Negro") {
            Serial.println("Registra Requerimiento de Reabasto");
        }
                  
          char textoChar[tamanoMax];
          String textoString = colorBoton;
          caracteresNumero = colorBoton.length();
          margenIzq = int((180-(caracteresNumero)*26)/2);
          textoString.toCharArray(textoChar , tamanoMax);
          u8g2.clearBuffer();
          u8g2.setFont(u8g2_font_logisoso16_tf); // Establecer la fuente del texto
          u8g2.drawStr(margenIzq, 45, textoChar); // Escribir texto en la pantalla
          u8g2.sendBuffer(); // Enviar el buffer a la pantalla
          //delay(1000);
          u8g2.clearBuffer();
        
          colorBoton = "";
          oldValVer = newValVer;
          oldValAzu = newValAzu;
          oldValAma = newValAma;
          oldValRoj = newValRoj;
          oldValNeg = newValNeg;
        }
        else{
         u8g2.clearBuffer();
        }
  if (colorBoton != ""){
    SigPartida();    
  }
}
void SigPartida(){
  refrescaPantalla(); 
// print a random number from 0 to 10000
  numeroEntero = random(100);
  cantSol = numeroEntero;
  cantSurt = cantSol;
  char textoChar[tamanoMax];
  String textoString = String(numeroEntero);
  caracteresNumero = textoString.length();
  margenIzq = int((180-(caracteresNumero+1)*26)/2);
  // Conversion de string a char
  textoString.toCharArray(textoChar , tamanoMax);
  Serial.println("Cantidad Solicitada: " + textoString);
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_logisoso32_tf); // Establecer la fuente del texto
  u8g2.drawStr(margenIzq, 45, textoChar); // Escribir el número aleatorio texto en la pantalla
  u8g2.sendBuffer(); // Enviar el buffer a la pantalla
}
void refrescaPantalla(){
  // put your main code here, to run repeatedly:
  //delay(10); // this speeds up the simulation
  u8g2.clearBuffer(); // Limpiar el buffer
  u8g2.setFont(u8g2_font_logisoso26_tf); // Establecer la fuente del texto
  u8g2.drawStr(21, 45, "SPick!"); // Escribir texto en la pantalla
  delay(500);
  u8g2.sendBuffer(); // Enviar el buffer a la pantalla
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_logisoso16_tf); // Establecer la fuente del texto
  u8g2.drawStr(1, 25, "El surtido"); // Escribir texto en la pantalla
  u8g2.drawStr(1, 55, "inteligente!"); // Escribir texto en la pantalla
  delay(800);
  u8g2.sendBuffer(); // Enviar el buffer a la pantalla
}