/* File System API */

//File delete API
void handleFileDelete(AsyncWebServerRequest *request) {
  String path = GetPara(request, "path");
  if (path == "") {
    request->send(400, "text/plain", "Missing 'path' parameter");
    return;
  }
  Serial.print("Requested delete path: ");
  Serial.println(path);

  if (SD.exists(path)) {
    if (SD.remove(path)) {
      request->send(200, "text/plain", "File removed");
    } else {
      request->send(500, "text/plain", "Failed to delete file");
    }
  } else {
    request->send(404, "text/plain", "File not found");
  }

}

//File download API
void handleFileDownload(AsyncWebServerRequest *request) {
  String path = GetPara(request, "path");
  if (path == "") {
    request->send(404, "text/plain", "'path' parameter not given");
    return;
  }
  Serial.print("Requested path: ");
  Serial.println(path);
  if (SD.exists(path)) {
    String contentType = getMime(path);
    request->send(SD, path, contentType, false);
  } else {
    request->send(404, "text/plain", "File not found");
  }
}

//List dir API
void handleListDir(AsyncWebServerRequest *request) {
  //Get the folder path to be listed
  //As ESP8266 dont have enough memory for proper struct to json conv, we are hacking a json string out of a single for-loop
  String jsonString = "[";
  String folderSubPath = GetPara(request, "dir");
  String folderPath = "/" + folderSubPath;
  if (SD.exists(folderPath)) {
    File root = SD.open(folderPath);
    bool firstObject = true;
    if (root) {
      while (true) {
        File entry = root.openNextFile();
        if (!entry) {
          // No more files
          break;
        } else {
          //There are more lines. Add a , to the end of the previous json object
          if (!firstObject) {
            jsonString = jsonString + ",";
          } else {
            firstObject = false;
          }
        }

        String isDirString = "true";
        if (!entry.isDirectory()) {
          isDirString = "false";
        }

        jsonString = jsonString + "{\"Filename\":\"" + entry.name() + "\",\"Filesize\":" + String(entry.size()) + ",\"IsDir\":" + isDirString + "}";
        entry.close();
      }
      root.close();

      jsonString += "]";
      request->send(200, "application/json", jsonString);
    } else {
      request->send(500, "text/plain", "500 - Path open error");
    }
  } else {
    request->send(404, "text/plain", "404 - Path not found");
  }
  Serial.println(folderPath);
}

// Function to handle file uploads
void handleFileUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
  static File uploadFile;
  static String uploadPath = "/";

  if (request->hasParam("dir")) {
    uploadPath = "/" + request->getParam("dir")->value() + "/";
  }

  if (index == 0) {
    String path = uploadPath + filename;
    Serial.printf("Upload Start: %s\n", path.c_str());
    uploadFile = SD.open(path, FILE_WRITE);
    if (!uploadFile) {
      Serial.println("Failed to open file for writing");
      return request->send(500, "text/plain", "File Upload Failed");
    }
  }

  // Write the received data to the file
  if (uploadFile) {
    uploadFile.write(data, len);
  }

  if (final) {
    // Close the file at the final chunk
    if (uploadFile) {
      uploadFile.close();
      Serial.printf("Upload End: %s (%u)\n", filename.c_str(), index + len);
      return request->send(200, "text/plain", "File Uploaded");
    }
    else {
      return request->send(500, "text/plain", "File Upload Failed");
    }
  }
}
