#include "ESPAsyncTCP.h"
#include "ESPAsyncWebServer.h"
#include <ESP8266mDNS.h>
#include <LittleFS.h> 

#include "AP_setup.h"
#include "sensor.h"


AsyncWebServer server(80);
AsyncWebSocket ws("/ws"); // access at ws://[esp ip]/ws

FluidPressureSensor pressure_before_radiator_sensor(A0);
FluidPressureSensor pressure_after_radiator_sensor(A0);

Thermistor temperature_before_radiator(A0, 100000);
Thermistor temperature_after_radiator(A0, 100000);

Barometer barometer;

Multiplexer mux;


int i = 0;

//flag to use from web update to reboot the ESP
bool shouldReboot = false;

void onRequest(AsyncWebServerRequest *request){
  //Handle Unknown Request
  request->send(200);
}

void onBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){
  //Handle body
}

void onUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){
  //Handle upload
}

void onEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len){
  if(type == WS_EVT_CONNECT){
    //client connected
    os_printf("ws[%s][%u] connect\n", server->url(), client->id());
    client->ping();
  } else if(type == WS_EVT_DISCONNECT){
    //client disconnected
    os_printf("ws[%s][%u] disconnect: %u\n", server->url(), client->id());
  } else if(type == WS_EVT_ERROR){
    //error was received from the other end
    os_printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t*)arg), (char*)data);
  } else if(type == WS_EVT_PONG){
    //pong message was received (in response to a ping request maybe)
    os_printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len)?(char*)data:"");
  } else if(type == WS_EVT_DATA){
    //data packet
    AwsFrameInfo * info = (AwsFrameInfo*)arg;
    
    //the whole message is in a single frame and we got all of it's data
    


  }
}



void setup(){
  Serial.begin(115200);
  initAccessPoint();
  LittleFS.begin();


  // attach AsyncWebSocket
  ws.onEvent(onEvent);
  server.addHandler(&ws);

   // respond to GET requests on URL /heap
  server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/plain", String(ESP.getFreeHeap()));
  });

  // send a file when /index is requested
  server.on("/", HTTP_ANY, [](AsyncWebServerRequest *request){
    request->send(LittleFS, "/index.html");
  });


  // attach filesystem root at URL /fs
  server.serveStatic("/static", LittleFS, "/static");

  // Catch-All Handlers
  // Any request that can not find a Handler that canHandle it
  // ends in the callbacks below.
  server.onNotFound(onRequest);
  server.onRequestBody(onBody);

  server.begin();

  pinMode(D8, OUTPUT);
  barometer.init();
  barometer.setAmbientPressure();
}


void loop(){
  if(shouldReboot){
    Serial.println("Rebooting...");
    delay(100);
    ESP.restart();
  }
  async_wait(10);
  //Serial.println(barometer.readWindSpeed());
  Serial.println(barometer.readWindSpeed());
  //ws.cleanupClients();
}