/*

    Webserver.ino

    This script handle web serving and RESTFUL API features
*/

void registerServeEndpoints() {
  server.serveStatic("/", LittleFS, "/index.html");

  /* Button Actions */
  server.on("/power/press", handlePowerBtnPress);
  server.on("/power/release", handlePowerBtnRelease);
  server.on("/reset/press", handleResetBtnPress);
  server.on("/reset/release", handleResetBtnRelease);
  server.on("/status", handleStatus);
  server.on("/ipaddr", handleGetIPAddr);
  
  /* RESTFUL API */
  server.on("/api/status", handleStatus);
  server.on("/api/power", handlePowerSequence);
  server.on("/api/reset", handleResetSequence);

  server.onNotFound(handleNotFound);
}

void sendOK(){
  server.send(200, "application/json", "\"ok\"");
}

/* Emulate the power button press events in hardware */
void handlePowerBtnPress() {
  digitalWrite(PWR_BTN, HIGH);
  digitalWrite(BTN_LED, LOW);
  Serial.println("[INFO] Power button pressed");
  sendOK();
}

void handlePowerBtnRelease() {
  digitalWrite(PWR_BTN, LOW);
  digitalWrite(BTN_LED, HIGH);
  Serial.println("[INFO] Power button released");
  sendOK();
}

/* Emulate the reset button press events in hardware */
void handleResetBtnPress() {
  digitalWrite(RST_BTN, HIGH);
  digitalWrite(BTN_LED, LOW);
  Serial.println("[INFO] Reset button pressed");
  sendOK();
}

void handleResetBtnRelease() {
  digitalWrite(RST_BTN, LOW);
  digitalWrite(BTN_LED, HIGH);
  Serial.println("[INFO] Reset button released");
  sendOK();
}


//Serving the LED status 
void handleStatus(){
  //Create a JSON object, 64 should be more than enough bytes to work with
  StaticJsonDocument<64> jsonDoc;
  jsonDoc["hdd"] = hddLedState;
  jsonDoc["pwr"] = pwrLedState;
  String jsonString;
  serializeJson(jsonDoc, jsonString);
  server.send(200, "application/json", jsonString);
}

//Return the ip address and mDNS name of the device
void handleGetIPAddr(){
  String ipAddrStr = WiFi.localIP().toString();
  StaticJsonDocument<128> jsonDoc;
  jsonDoc["ipaddr"] = ipAddrStr;
  jsonDoc["mdns"] = MDNS_NAME + ".local";
  String jsonString;
  serializeJson(jsonDoc, jsonString);
  server.send(200, "application/json", jsonString);
}

//Handle power up sequence
void handlePowerSequence() {
  if (server.method() == HTTP_POST) {
    Serial.println("[INFO] Toggling power button via RESTFUL API");
    pressPowerButton();
    sendOK();
  } else {
    server.send(405, "text/plain", "Method Not Allowed");
  }
}

//Handle reset sequence
void handleResetSequence() {
  if (server.method() == HTTP_POST) {
    Serial.println("[INFO]  Toggling reset button via RESTFUL API");
    pressResetButton();
    sendOK();
  } else {
    server.send(405, "text/plain", "Method Not Allowed");
  }
}

// Default not found handler
void handleNotFound() {
  String message = "404 - Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
}
