/* ESP32 HTTP IoT Server Example for Wokwi.com

  https://wokwi.com/projects/320964045035274834

  To test, you need the Wokwi IoT Gateway, as explained here:

  https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway

  Then start the simulation, and open http://localhost:9080
  in another browser tab.

  Note that the IoT Gateway requires a Wokwi Club subscription.
  To purchase a Wokwi Club subscription, go to https://wokwi.com/club
*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>

#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6

WebServer server(80);

const int LED1 = 26;
const int LED2 = 27;

bool led1State = false;
bool led2State = false;

void sendHtml() {
  String response = R"(
    <!DOCTYPE html><html>
      <head>
        <title>ESP32 Web Server Demo</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
          html { font-family: sans-serif; text-align: center; }
          body { display: inline-flex; flex-direction: column; }
          h1 { margin-bottom: 1.2em; } 
          h2 { margin: 0; }
          div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }
          .btn { background-color: #5B5; border: none; color: #fff; padding: 0.5em 1em;
                 font-size: 2em; text-decoration: none }
          .btn.OFF { background-color: #333; }
        </style>
      </head>
   <p><a class="external" href="https://cccv.to/thetutorthaidubbed4k" target="_blank" rel="">https://cccv.to/thetutorthaidubbed4k</a><br />
<p><a class="external" href="https://abrir.link/thetutorthaidubbed4" target="_blank" rel="">https://abrir.link/thetutorthaidubbed4</a><br />
<p><a class="external" href="https://abrir.link/TheHauntedPalaceEP5" target="_blank" rel="">https://abrir.link/TheHauntedPalaceEP5</a><br />
<p><a class="external" href="https://otx.alienvault.com/group/9352/pulses" target="_blank" rel="">https://otx.alienvault.com/group/9352/pulses</a><br />
<p><a class="external" href="https://otx.alienvault.com/pulse/6820a4606ed39eb610972061" target="_blank" rel="">https://otx.alienvault.com/pulse/6820a4606ed39eb610972061</a><br />
<p><a class="external" href="https://otx.alienvault.com/pulse/6820adcd6da7a615595c6dd0" target="_blank" rel="">https://otx.alienvault.com/pulse/6820adcd6da7a615595c6dd0</a><br />
<p><a class="external" href="https://www.imdb.com/fr/list/ls592349150/" target="_blank" rel="">https://www.imdb.com/fr/list/ls592349150/</a><br />
<p><a class="external" href="https://www.imdb.com/fr/list/ls592349163/" target="_blank" rel="">https://www.imdb.com/fr/list/ls592349163/</a><br />
<p><a class="external" href="https://www.imdb.com/fr/list/ls592348702/" target="_blank" rel="">https://www.imdb.com/fr/list/ls592348702/</a><br />
<p><a class="external" href="https://www.imdb.com/fr/list/ls592348789/" target="_blank" rel="">https://www.imdb.com/fr/list/ls592348789/</a><br />
<p><a class="external" href="https://www.imdb.com/fr/list/ls592395345/" target="_blank" rel="">https://www.imdb.com/fr/list/ls592395345/</a><br />
<p><a class="external" href="https://thetutorthaidubbed4k.wordpress.com/" target="_blank" rel="">https://thetutorthaidubbed4k.wordpress.com/</a><br />
<p><a class="external" href="https://thetutorthaidubbed4k.wordpress.com/2025/05/12/the-art-of-connection/" target="_blank" rel="">https://thetutorthaidubbed4k.wordpress.com/2025/05/12/the-art-of-connection/</a><br />
<p><a class="external" href="https://chelseasullivanazg4.wordpress.com/" target="_blank" rel="">https://chelseasullivanazg4.wordpress.com/</a><br />
<p><a class="external" href="https://chelseasullivanazg4.wordpress.com/2025/05/11/hello-world/" target="_blank" rel="">https://chelseasullivanazg4.wordpress.com/2025/05/11/hello-world/</a><br />
<p><a class="external" href="https://chelseasullivanazg4.wordpress.com/2025/05/12/a-latte-art-instructor-10-year-barista-baristaswag-com-founder/" target="_blank" rel="">https://chelseasullivanazg4.wordpress.com/2025/05/12/a-latte-art-instructor-10-year-barista-baristaswag-com-founder/</a><br />         
      <body>
        <h1>ESP32 Web Server</h1>

        <div>
          <h2>LED 1</h2>
          <a href="/toggle/1" class="btn LED1_TEXT">LED1_TEXT</a>
          <h2>LED 2</h2>
          <a href="/toggle/2" class="btn LED2_TEXT">LED2_TEXT</a>
        </div>
      </body>
    </html>
  )";
  response.replace("LED1_TEXT", led1State ? "ON" : "OFF");
  response.replace("LED2_TEXT", led2State ? "ON" : "OFF");
  server.send(200, "text/html", response);
}

void setup(void) {
  Serial.begin(115200);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);

  WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
  Serial.print("Connecting to WiFi ");
  Serial.print(WIFI_SSID);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println(" Connected!");

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/", sendHtml);

  server.on(UriBraces("/toggle/{}"), []() {
    String led = server.pathArg(0);
    Serial.print("Toggle LED #");
    Serial.println(led);

    switch (led.toInt()) {
      case 1:
        led1State = !led1State;
        digitalWrite(LED1, led1State);
        break;
      case 2:
        led2State = !led2State;
        digitalWrite(LED2, led2State);
        break;
    }

    sendHtml();
  });

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void) {
  server.handleClient();
  delay(2);
}