#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <HTTPClient.h>
#define ledPIN 0
#define dept 11
#define substance 12 

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

const String url = "http://178.150.120.28:9999/alert";
const String add_history_url = "http://178.150.120.28:9999/add_history";
const String get_danger_level_url = "http://178.150.120.28:9999/get_danger_level";
int danger_level;




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

void setup() {
  analogReadResolution(9);

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

	Serial.begin(9600);
  connectToWiFi();
   pinMode(ledPIN, OUTPUT);

   danger_level = getDangerLevelGetData(String(substance));
}

void connectToWiFi() {
    WiFi.begin(ssid, password);

    Serial.print("Connecting to WiFi");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.print(" OK! Connected to WiFi!\n");
    Serial.print("Connecting to server: " + url + "... ");
}

void sendDataToServer(String data) {
    HTTPClient http;
    http.begin(url);
    http.addHeader("Content-Type", "Content-Type: application/json");


    int httpResponseCode = http.POST(data);

    if (httpResponseCode > 0) {
        Serial.print("HTTP ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
    } else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
    }
}

void addHistorySendData(String data) {
    HTTPClient http;
    http.begin(add_history_url);
    http.addHeader("Content-Type", "Content-Type: application/json");


    int httpResponseCode = http.POST(data);

    if (httpResponseCode > 0) {
        Serial.print("HTTP ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
    } else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
    }
}

double getDangerLevelGetData(String data) {
    HTTPClient http;
    http.begin(get_danger_level_url);
    http.addHeader("Content-Type", "Content-Type: application/json");


    int httpResponseCode = http.POST(data);

    if (httpResponseCode > 0) {
        Serial.print("HTTP ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        return payload.toDouble();
    } else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
         return 0;
    }
}

void loop() {
  int sensorValue = analogRead(A1); 
  
  String text = "";
  text += sensorValue;

  String historyData = String(dept)+"/" + String(substance) + "/" + text;
   
   addHistorySendData(historyData);

  if(sensorValue >= danger_level) {
    digitalWrite(ledPIN,HIGH);
    String alertData = String(dept)+"/" + String(substance) + "/" + text;
    sendDataToServer(alertData);  
  } else {
     digitalWrite(ledPIN,LOW); 
     text = "0";
     String alertData = String(dept)+"/" + String(substance) + "/" + text;
    sendDataToServer(alertData);  
  }

  String toPrint = "";
  toPrint += sensorValue;

 LCD.print(toPrint);
LCD.setCursor(0,0);
delay(300);
}