#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

// Project: Remote PC Power Control via ESP8266
// Hardware components:
// 1. Controller: NodeMCU ESP8266 V3 (CH340)
// 2. Relay: 1-Channel 3.3V Low-Level Trigger Relay
// 3. Power: USB Type-C (5V)
// 4. Connection: 
//    - Relay VCC -> NodeMCU VIN (5V) or 3V
//    - Relay GND -> NodeMCU GND
//    - Relay IN  -> NodeMCU D1 (GPIO5)
//    - Relay COM & NO -> PC Motherboard Power SW pins
// 
// Features:
// - Web-server for remote control via VPN
// - mDNS support (access via http://mainpc.local)
// - Force Shutdown support (8s long press via /force)
// - Physical FLASH button on board triggers the relay
// - English UI

// Your network config
const char* ssid = "WiFI_NAME_REGISTRY_DEPENDANT"; // Change this
const char* password = "WiFI_PASSWORD"; // Change this
const char* hostName = "myname"; // Change this to access in browser with myname.local

const int relayPin = 5;       // D1 (GPIO5)
const int buttonPin = 0;      // FLASH button (GPIO0)
const int ledPin = 2;         // Onboard LED (D4)

// Durations in milliseconds
const int standardPress = 500; 
const int forcePress = 8000;    // Force kill duration (8 sec)

ESP8266WebServer server(80);

void pressButton(int durationMs) {
  Serial.print("Action: pressing button for ");
  Serial.print(durationMs);
  Serial.println("ms");

  digitalWrite(ledPin, LOW);    
  digitalWrite(relayPin, LOW);  
  
  delay(durationMs);            
  
  digitalWrite(relayPin, HIGH); 
  digitalWrite(ledPin, HIGH);   
}

void handleRoot() {
  String html = "<html><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1'>";
  html += "<style>body{text-align:center;font-family:Arial,sans-serif;background:#f4f4f4;padding:20px;}";
  html += ".btn{width:100%;max-width:300px;height:80px;font-size:20px;font-weight:bold;color:white;border:none;border-radius:15px;margin:15px 0;cursor:pointer;display:block;margin-left:auto;margin-right:auto;transition:0.2s;}";
  html += ".on{background:#2ecc71;box-shadow:0 5px #27ae60;} .on:active{transform:translateY(4px);box-shadow:0 2px #27ae60;}";
  html += ".off{background:#e74c3c;box-shadow:0 5px #c0392b;} .off:active{transform:translateY(4px);box-shadow:0 2px #c0392b;}";
  html += "a{color:#7f8c8d;text-decoration:none;display:block;margin-top:20px; font-size: 14px;} h1{color:#2c3e50;}</style></head>";
  html += "<body><h1>PC Remote Control</h1>";
  
  html += "<button class='btn on' onclick=\"location.href='/power'\">POWER ON</button>";
  html += "<button class='btn off' onclick=\"if(confirm('Are you sure you want to FORCE SHUTDOWN?')){location.href='/force';}\">FORCE OFF</button>";
  
  html += "<p>Network name: http://" + String(hostName) + ".local</p></body></html>";
  server.send(200, "text/html", html);
}

void handlePower() {
  server.send(200, "text/html", "<h1>OK! Standard press sent.</h1><script>setTimeout(function(){window.location.href='/';},2000);</script>");
  pressButton(standardPress); 
}

void handleForce() {
  server.send(200, "text/html", "<h1>OK! Force shutdown sent.</h1><script>setTimeout(function(){window.location.href='/';},4000);</script>");
  pressButton(forcePress); 
}

void setup() {
  Serial.begin(115200);
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  
  digitalWrite(relayPin, HIGH); 
  digitalWrite(ledPin, HIGH);  

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

  Serial.println("\nWiFi Connected!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin(hostName)) { 
    Serial.println("mDNS responder started: http://" + String(hostName) + ".local"); 
  }

  server.on("/", handleRoot);
  server.on("/power", handlePower);
  server.on("/force", handleForce);
  server.begin();
  
  MDNS.addService("http", "tcp", 80);
}

void loop() {
  MDNS.update();
  server.handleClient();

  if (digitalRead(buttonPin) == LOW) {
    delay(50); 
    if (digitalRead(buttonPin) == LOW) {
      pressButton(standardPress);
      while(digitalRead(buttonPin) == LOW) delay(10);
    }
  }
}