//What we're working on here is the ability to utilize WiFiManager to not only save the
// the WiFi connection parameters but to also utilize the custom parameter capabilities of
// of the tool to store the appropriate MQTT parameters. 
//What's special about this particular project is that we're trying to make it work 
// not only with a real ESP32 but also in the Wokwi environment. In Wokwi, we can't get 
// to the web interface for the WebManager Server so we're working on a way to set 
// and pull default values from WiFiManager.
//This particular sketch is the initial one getting WiFiManager operational in Wokwi.
// There are a couple of lines of code for WiFiManagerParameter too.

#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
WiFiManager wm;

void setup() {
    WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP    
    // put your setup code here, to run once:
    Serial.begin(115200);
    //wifiConnect();

    WiFiManagerParameter custom_mqtt_server("server", "mqtt server", "mqtt_server", 40);
    wm.addParameter(&custom_mqtt_server);

    //wm.autoConnect("Wokwi-GUEST","");
    wm.preloadWiFi("Wokwi-GUEST", "");

    wm.setConfigPortalTimeout(12);



   //This is where I'm attempting to set the SSID password for development purposes.
    //wm.setWiFiSSID("Wokwi-GUEST");
    //wm.setWiFiPass("MyPassword");

    //Serial.print("wm.getWiFiSSID(): ");
    Serial.println("SSID: " + wm.getWiFiSSID());
    Serial.println("Password: " + wm.getWiFiPass());

    //reset settings - wipe credentials for testing
    //wm.resetSettings();

    wm.setConfigPortalBlocking(false);
    wm.setConfigPortalTimeout(60);
    //automatically connect using saved credentials if they exist
    //If connection fails it starts an access point with the specified name
    if(wm.autoConnect("AutoConnectAP")){
        Serial.println("connected...yeey :)");
    }
    else {
        Serial.println("Configportal running");
    }

    //char mqtt_serverVal[40];
    
    //mqtt_serverVal = custom_mqtt_server.getValue();
    Serial.print("custom_mqtt_server: ");

    Serial.println(custom_mqtt_server.getValue());
}

void loop() {
    wm.process();
    // put your main code here, to run repeatedly:

}

void wifiConnect() {
  WiFi.mode(WIFI_STA);
  WiFi.begin("Wokwi-GUEST", "");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
}


/*
#include "WiFiManager.h"          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
WiFiManager wm;
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", "", 40);

const int buttonOpen = 34;
const int buttonClose = 35;      // the number of the LED pin

const int ledPin1 = GPIO_NUM_21;
const int ledPin2 = GPIO_NUM_19;

int buttonOpenState;
int buttonCloseState;

void setup() {
  WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
  wm.addParameter(&custom_mqtt_server);
  wm.setConfigPortalBlocking(false);
  //wm.setSaveParamsCallback(saveParamsCallback);
  Serial.begin(115200);
  Serial.println("Hello, ESP32!");
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(buttonOpen, INPUT_PULLUP);
  pinMode(buttonClose, INPUT_PULLUP);
  if(wm.autoConnect("AutoConnectAP")){
      Serial.println("connected...yeey :)");
  }
  else {
      Serial.println("Configportal running");
  }
}
*/