#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const int optocoupler = D2;

const char* ssid = "LeWe";
const char* password = "5275274365464843";

ESP8266WebServer server(80);

int lightStatus = 0; // 0 off, 1 on, 2 pulse


void setup() {
  Serial.begin(9600);
  
  pinMode(LED_BUILTIN, OUTPUT);  // initialize onboard LED as output
  digitalWrite(LED_BUILTIN, HIGH);

  pinMode(optocoupler, OUTPUT);
  digitalWrite(optocoupler, LOW);

  // Connect WiFi
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.hostname("OnAir");
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(250);
    digitalWrite(LED_BUILTIN, LOW);
    delay(250);
    digitalWrite(LED_BUILTIN, HIGH);
  }
  Serial.println();
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  WiFi.setAutoReconnect(true);
  WiFi.persistent(true);
  digitalWrite(LED_BUILTIN, HIGH);

  server.on("/click", handle_click);
  server.on("/light", handle_light);
  server.onNotFound(handle_NotFound);

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

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

void cycleStatus() {
  digitalWrite(optocoupler, HIGH);
  delay(350);
  digitalWrite(optocoupler, LOW);
  delay(150);

  if (lightStatus < 2) {
    lightStatus++;
  }
  else {
    lightStatus = 0;
  }
  
  Serial.print("click --> ");
  Serial.println(lightStatus);
}

void handle_click() {
  if (server.hasArg("status")) {

    int targetStatus = server.arg("status").toInt();
    Serial.print("targetStatus: ");
    Serial.println(targetStatus);

    if (targetStatus >= 0 && targetStatus <= 2) {
      while (lightStatus != targetStatus) {
        cycleStatus();
      }
    }
  }
  else {
    cycleStatus();
  }

  String response = "Status set to ";
  response.concat(lightStatus);
  server.send(200, "text/plain", response);
}

String getButtonStatus(int button) {
  if (lightStatus == button) {
    return "button_on";
  }
  return "button_off";
}

void handle_light(){
  if (server.hasArg("status")) {

    int targetStatus = server.arg("status").toInt();
    Serial.print("targetStatus: ");
    Serial.println(targetStatus);

    if (targetStatus >= 0 && targetStatus <= 2) {
      while (lightStatus != targetStatus) {
        cycleStatus();
      }
    }
  }
  
  String response = "<!DOCTYPE html><html>";
  response += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
  response += "<link rel=\"icon\" href=\"data:,\">";
  response += "<style>html { font-family: sans-serif; display: inline-block; margin: 0px auto; text-align: center; } \n";
  response += ".button_on { background-color: #4CAF50; border: none; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;} \n";
  response += ".button_off { background-color: #F0F0F0; border: none; color: darkgray; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;} \n";
  response += "</style></head>";
  response += "<body><h1>On Air</h1>";
  response += "<p><a href=\"/light?status=0\"><button class=\"" + getButtonStatus(0) + "\">Off</button></a></p>";
  response += "<p><a href=\"/light?status=1\"><button class=\"" + getButtonStatus(1) + "\">On</button></a></p>";
  response += "<p><a href=\"/light?status=2\"><button class=\"" + getButtonStatus(2) + "\">Pulse</button></a></p>";
  response += "</body></html>";
  
  server.send(200, "text/html", response);
}

void handle_NotFound(){
  server.send(404, "text/plain", "On Air: Page not found");
}
