//
//
//

#include "ProjGlobals.h"
#include "WiFiStateHandler.h"

#include <DNSServer.h>				///< Required for Captive Portal behavior.
const byte DNS_PORT = 53;			///< Capture DNS requests on port 53
//DNSServer 	dnsServer;				///< Used for the captive portal AP mode

WiFiConnectState wifiConState;      ///< Track the current WiFi state


void WiFiStateSet(WiFiConnectState newState) {
  wifiConState = newState;
}

WiFiConnectState WiFiStateGet() {
  return wifiConState;
}

WiFiConnectState WiFiStateHandler() {
  static unsigned long timeStateChange = 0;
  // @todo static unsigned long timeLEDControl;
  // @todo unsigned long timeLEDCycle;
  String ssid = wifiConfig.getSSID();
  String pass = wifiConfig.getPassword();
  IPAddress myIP;

  // @todo 
  #if 0
  timeLEDCycle = millis() - timeLEDControl;
  if (timeLEDCycle >= LED_PERIOD__MS) {
    timeLEDControl = millis();
    timeLEDCycle = 0;
  }
  #endif

  switch (wifiConState) {
    case WFC_ConnectToAP:
      // @todo isStationMode = true;
      WiFi.mode(WIFI_STA);
      wifi_set_sleep_type(NONE_SLEEP_T);    // rumor is that this fixes multicast receive
      //ssid = wifiConfig.getSSID();
      //pass = wifiConfig.getPassword();
      WiFi.begin(ssid.c_str(), pass.c_str());
      timeStateChange = millis();
      // @todo timeLEDControl = timeStateChange;
      wifiConState = WFC_JoiningAP;
      break;
    case WFC_JoiningAP:
      // @todo if (timeLEDCycle <= LED_JOINING_MS) {
        // @todo SetLED(LED_WIFI, 1);
      // @todo } else {
        // @todo SetLED(LED_WIFI, 0);
      // @todo }
      if (WiFi.status() == WL_CONNECTED) {
        myIP = WiFi.localIP();
        Serial.printf("IP Address: %s\n", myIP.toString().c_str());
        timeStateChange = millis();
        //StartWebServer();
        wifiConState = WFC_JoinedAP;
      } else if (millis() - timeStateChange > 30000) {
        timeStateChange = millis();
        wifiConState = WFC_CreateAP;  // failed for 30 sec, now what. retry or CreateAP?
      }
      break;
    case WFC_CreateAP:
      // @todo if (timeLEDCycle <= LED_APMODE__MS) {
        // @todo SetLED(LED_WIFI, 1);
      // @todo } else {
        // @todo SetLED(LED_WIFI, 0);
      // @todo }
      // @todo isStationMode = false;
      Serial.printf("Starting Access Point %s\n", AP_NAME.c_str());
      WiFi.softAP(AP_NAME.c_str());
      myIP = WiFi.softAPIP();
      Serial.printf("IP Address: %s\n", myIP.toString().c_str());
      //dnsServer.start(DNS_PORT, "growcontroller.info", myIP);
      timeStateChange = millis();
      //StartWebServer();
      wifiConState = WFC_HostingAP;
      break;
    case WFC_JoinedAP:
      // @todo if (timeLEDCycle <= LED_JOINED__MS) {
        // @todo SetLED(LED_WIFI, 1);
      // @todo } else {
        // @todo SetLED(LED_WIFI, 0);
      // @todo }
      // @todo server.handleClient();
      break;
    case WFC_HostingAP:
      // @todo if (timeLEDCycle <= LED_APMODE__MS) {
        // @todo SetLED(LED_WIFI, 1);
      // @todo } else {
        // @todo SetLED(LED_WIFI, 0);
      // @todo }
      // @todo server.handleClient();
//      dnsServer.processNextRequest();
      break;
    default:
    case WFC_Idle:
      break;
  }
  return wifiConState;
}
