/*
  ESP32 HTTPClient Example

  https://wokwi.com/arduino/projects/324787238284034642

*/

//        void sendData() {
//          if (http.begin(client, url)) {
//            String datas = "{\"var1\":";
//            datas = datas + var1 + ",\"var2\":";
//            datas = datas + var2 + ",\"var3\":";
//            datas = datas + var3+ ",\"var4\":";
//            datas = datas + var4 + "}";
//
//            int data = data.length();
//
//            http.addHeader("Content-Type", "application/json");
//            http.addHeader("Accept", "*/*");
//            http.addHeader("Content-Length", String(data));
//
//            int httpCode = http.POST(datas);
//            http.setTimeout(500);
//            String payload = http.getString();
//            http.end();
//          }
//        }



#include <WiFi.h>
#include <HTTPClient.h>
#include <LiquidCrystal_I2C.h>

const char* ssid = "Wokwi-GUEST";
const char* password = "";

const String url = "https://uqfholeibiattsbqjoui.supabase.co/rest/v1/tabela01?select=*";

LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);

HTTPClient http;

void spinner() {
  static int8_t counter = 0;
  const char* glyphs = "\xa1\xa5\xdb";
  LCD.setCursor(15, 1);
  LCD.print(glyphs[counter++]);
  if (counter == strlen(glyphs)) {
    counter = 0;
  }
}

void setup() {

  Serial.begin(115200);
  while (!Serial)    {     }

  LCD.init();
  LCD.backlight();
  LCD.clear();

  WiFi.begin("Wokwi-GUEST", "");

  LCD.setCursor(0, 0);
  LCD.print("Conectando-se a ");
  LCD.setCursor(0, 1);
  LCD.print("Rede WiFi ");

  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    spinner();
  }

  LCD.clear();
  LCD.setCursor(0, 0);
  LCD.print("Endereco IP: ");
  LCD.setCursor(0, 1);
  LCD.print(WiFi.localIP());

  delay(1000);

}

void loop() {

  LCD.setCursor(11, 1);
  LCD.print("FETCH");

  Serial.println("Requisicao: FETCH");
  //Serial.print("Fetching " + url + "... ");

  http.begin(url);

  http.addHeader("apikey", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InVxZmhvbGVpYmlhdHRzYnFqb3VpIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDE0NTI1ODUsImV4cCI6MjAxNzAyODU4NX0.i0mY4GA26iYrEE3NyWqRBPXIy9dmQrfHZ73KnyYqBLI");
  http.addHeader("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InVxZmhvbGVpYmlhdHRzYnFqb3VpIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MDE0NTI1ODUsImV4cCI6MjAxNzAyODU4NX0.i0mY4GA26iYrEE3NyWqRBPXIy9dmQrfHZ73KnyYqBLI");

  int statusCode = http.GET();
  Serial.print("Status: ");
  Serial.println(statusCode);

  if (statusCode > 0) {
    String response = http.getString();
    Serial.println("Retorno: ");
    Serial.println(response);
  }

  LCD.setCursor(11, 1);
  LCD.print(" SHOW");

  delay(15000);

}