/*
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>
*/


void setupWiFi() {
  if(WiFi.status() == WL_CONNECTED) { return; }  // return if not connected
  switch(currentWiFiMode) {
    case 0: {
        Serial.println("[WiFi]               Connecting to WiFi...");
        String WiFiSSIDstr = getWiFiSSID();
        String WiFiPSKstr = getWiFiPSK();
        if(WiFiPSKstr == String() || WiFiSSIDstr == String()) { 
          currentWiFiMode = 1;
          Serial.printf("[WiFi]               No WiFi configured\n");
          setupWiFi();  // recursive call to reach the 'case 1' statement
          break;
        }
        char WiFiSSID[33];
        char WiFiPSK[64];
        
        // copy to char arrays as the WiFi library needs these (maximum length 32/63 chars)
        for(int i = 0; i<33; i++) { if(WiFiSSIDstr.length() >= i) {WiFiSSID[i] = WiFiSSIDstr[i];} else break; }
        for(int i = 0; i<64; i++) { if(WiFiPSKstr.length() >= i) {WiFiPSK[i] = WiFiPSKstr[i];} else break; }
        Serial.println(WiFiSSID);
        Serial.println(WiFiPSK);

        WiFi.disconnect();
        WiFi.mode(WIFI_AP_STA);
        WiFi.begin(WiFiSSID, WiFiPSK);
        // wait for 10 seconds for wifi to connect
        int start_timer = millis(); while(WiFi.status() != WL_CONNECTED) { if((millis()-start_timer) > 10000) break; }  
        if(WiFi.status() != WL_CONNECTED) {
          currentWiFiMode = 1;
          Serial.printf("[WiFi]               Unable to connect to %s\n", WiFiSSID);
          setupWiFi();  // recursive call to reach the 'case 1' statement
          break;
        }
        Serial.printf("[WiFi]               Connected to WiFi %s, got IP-Adress ", WiFiSSID);
        Serial.println(WiFi.localIP());
      }
      break;
    case 1: {
        if(!apON) {
          Serial.printf("[WiFi]               Opening AP\n[WiFi]               SSID: %s\n[WiFi]               PSK:  %s\n", apSSID.c_str(), apPSK.c_str());
          WiFi.mode(WIFI_AP);
          WiFi.softAP(apSSID, apPSK);
          apON = true;
          Serial.printf("[WiFi]               Opened AP successfully\n");
        }
      }
      break;
  }

}