// The module deals with the response from the server - including parsing it.  

#include <iostream>
#include <sstream>
#include <cctype>
#include <cstring>

#include "ESP8266HttpClient.h"

void computerConnectionSetup() {
  ESP8266setup();
}

String getFieldValue(const String& json, const String& fieldName);

float getTempData() {
  static float data[3];
  String json = getDataFromServer();
  
  String currentTemp = getFieldValue(json, "temp_c"); 

  
  Serial.print("CURRENTTEMP: ");
  Serial.println(currentTemp);

  return atof(currentTemp.c_str());
}


String getFieldValue(const String& json, const String& fieldName) {
  // Find the start and end index of the field in the JSON string
  int startIndex = json.indexOf(fieldName) + fieldName.length() + 2;
  if (startIndex == -1) {
    return "Field not found";
  }
  int endIndex = json.indexOf(",", startIndex);
  if (endIndex == -1) {
    endIndex = json.indexOf("}", startIndex);
    if (endIndex == -1) {
      return "Error parsing JSON";
    }
  }

  // Extract the field value from the JSON string
  return json.substring(startIndex, endIndex);
}
