/*
  Example of writing info to Google Sheets over wifi.
  Original Source: https://www.youtube.com/watch?v=u7TYu61l0t4
*/
#include "WiFi.h"
#include <HTTPClient.h>
// Defining LED PINs on the ESP32 Board.
const int LED_PIN = 10;
const int VAL1_POT_PIN = 2;
const int VAL2_POT_PIN = 3;
int pedido;
String codigo;
String tipoSurtido;
int cantSol;
int cantSurt;
int ruleta;
int ruletaCodigo;
// Google script Web_App_URL.
const String WEB_APP_URL = "https://script.google.com/macros/s/AKfycbyLCdR-mEsyg7F-uC2eY14ObSBD4r6ZfjNNszDs2WIFxiY3Od6BznmlCqQhcX9RYT4/exec?";
const String SHEET_NAME = "Datos desde ESP32";
void setup() {
  Serial.begin(115200);
  Serial.println();
  pinMode(LED_PIN, OUTPUT);
  pinMode(VAL1_POT_PIN, INPUT);
  pinMode(VAL2_POT_PIN, INPUT);
  WiFi.mode(WIFI_STA);
  Serial.print("Connecting to WiFi");
  WiFi.begin("Wokwi-GUEST", "", 6);
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected.");
  Serial.println("------------");
  digitalWrite(LED_PIN, LOW);
}
void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    digitalWrite(LED_PIN, HIGH);
    // Create a URL for sending or writing data to Google Sheets.
    // String Send_Data_URL = WEB_APP_URL + "?sts=write";
    // Send_Data_URL += "&sheet=" + SHEET_NAME;
    // Send_Data_URL += "&val1=" + (String)analogRead(VAL1_POT_PIN);
    // Send_Data_URL += "&val2=" + (String)analogRead(VAL2_POT_PIN);
    //https://script.google.com/macros/s/AKfycbyLCdR-mEsyg7F-uC2eY14ObSBD4r6ZfjNNszDs2WIFxiY3Od6BznmlCqQhcX9RYT4/exec?
    //value1=109304&
    //value2='AMCN01'&
    //value3=363&
    //value4=362&
    //value5='Parcial'
    ruletaCodigo = random(10);
    codigo = "AMCN"+String(ruletaCodigo);
    ruleta = random(1000) - 100;
    pedido = random(100) + 110000;
    codigo = "AMCN02";
    cantSol = random(100) + 1;
    if (ruleta <= 0){
      cantSurt = 0;
      tipoSurtido = "Nula";
    }
    if (ruleta > 0 && ruleta < 800){
      cantSurt = cantSol;
      tipoSurtido = "Completa";
    }
    if (ruleta >= 800){
      cantSurt = cantSol - 10;
      tipoSurtido = "Parcial";
    }
    String Send_Data_URL = WEB_APP_URL;
    Send_Data_URL += "value1="+String(pedido)+"&";
    Send_Data_URL += "value2='"+codigo+"'&";
    Send_Data_URL += "value3="+String(cantSol)+"&";
    Send_Data_URL += "value4="+String(cantSurt)+"&";
    Send_Data_URL += "value5="+tipoSurtido+"'";
    
    Serial.println();
    Serial.println("-------------");
    Serial.println("Send data to Google Spreadsheet...");
    Serial.print("URL : ");
    Serial.println(Send_Data_URL);
    // Writing data to Google Sheets.
    HTTPClient http;
    // HTTP GET Request.
    http.begin(Send_Data_URL.c_str());
    http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
    // Gets the HTTP status code.
    int httpCode = http.GET();
    Serial.print("HTTP Status Code : ");
    Serial.println(httpCode);
    // Getting response from google sheets.
    String payload;
    if (httpCode > 0) {
      payload = http.getString();
      Serial.println("Response: " + payload);
    }
    http.end();
    digitalWrite(LED_PIN, LOW);
    Serial.println("-------------");
  }
  // Save every 10 seconds. Adjust to frequency desired. This is a blocking delay for simplicity, switch
  // to millis() mechanisim or simular if need to do other processing between writes. 
  delay(10000);
}