#include <WiFi.h>
#include <WebServer.h>

// Wi-Fi credentials
const char* ssid = "IRONMAN1";
const char* password = "1234567890";

// Web server
WebServer server(80);

// LED pin
const int ledPin = 33;
bool isAlertActive = false;

void setup() {
  // Initialize serial monitor
  Serial.begin(115200);

  // Initialize LED pin
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected to Wi-Fi");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Define server routes
  server.on("/", handleMainPage);
  server.on("/alerton", handleAlertPage);
  server.on("/reset", handleResetPage);

  // Start the server
  server.begin();
  Serial.println("Server started");
}

void loop() {
  // Handle incoming client requests
  server.handleClient();

  // Blink LED if alert is active
  if (isAlertActive) {
    digitalWrite(ledPin, HIGH);
    delay(500);
    digitalWrite(ledPin, LOW);
    delay(500);
  }
}

// Main page handler
void handleMainPage() {
  isAlertActive = false; // Stop LED blinking
  digitalWrite(ledPin, LOW);

  String html = R"rawliteral(
    <!DOCTYPE html>
    <html>
    <head>
      <title>IndusBoard</title>
      <style>
        body {
          font-family: Arial, sans-serif;
          text-align: center;
          background: linear-gradient(135deg, #0078ff, #00bfff);
          color: white;
          margin: 0;
          padding: 0;
        }
        h1 {
          margin-top: 20%;
          font-size: 2.5em;
        }
      </style>
    </head>
    <body>
      <h1>IndusBoard AI Engine Fault Detection</h1>
    </body>
    </html>
  )rawliteral";

  server.send(200, "text/html", html);
}

// Alert page handler
void handleAlertPage() {
  isAlertActive = true; // Start LED blinking

  String html = R"rawliteral(
    <!DOCTYPE html>
    <html>
    <head>
      <title>Engine Fault Alert</title>
      <style>
        body {
          font-family: Arial, sans-serif;
          text-align: center;
          background: linear-gradient(135deg, #ff4040, #ff7f7f);
          color: white;
          margin: 0;
          padding: 0;
        }
        h1 {
          margin-top: 20%;
          font-size: 2.5em;
        }
        button {
          margin-top: 20px;
          padding: 15px 30px;
          font-size: 1em;
          color: white;
          background-color: #0078ff;
          border: none;
          border-radius: 5px;
          cursor: pointer;
          transition: transform 0.2s;
        }
        button:hover {
          transform: scale(1.1);
        }
      </style>
    </head>
    <body>
      <h1>ENGINE IS IN FAULT</h1>
      <button onclick="window.location.href='/reset'">Reset and Go Back</button>
    </body>
    </html>
  )rawliteral";

  server.send(200, "text/html", html);
}

// Reset page handler
void handleResetPage() {
  isAlertActive = false; // Stop LED blinking
  digitalWrite(ledPin, LOW);

  server.sendHeader("Location", "/", true);
  server.send(302, "text/plain", "Redirecting...");
}
