/*
 * BasicWiFiConnection.ino
 * 
 * This example demonstrates the basic WiFi connection functionality
 * of the WiFiHandlerV2 class. It shows how to connect to a WiFi network,
 * display connection information, and handle reconnection.
 */

#include "wifi-handlerv2.h"

// WiFi credentials
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";

// Create WiFiHandler instance
WiFiHandlerV2 wifi;

void setup() {
  Serial.begin(115200);
  delay(1000);
  
  Serial.println("Starting BasicWiFiConnection example");
  
  // Connect to WiFi
  Serial.print("Connecting to WiFi network: ");
  Serial.println(ssid);
  
  if (wifi.connect(ssid, password)) {
    Serial.println("Connected successfully!");
    printNetworkInfo();
  } else {
    Serial.println("Connection failed!");
  }
}

void loop() {
  // Run wifi handler loop to maintain connection
  wifi.loop();
  
  // Check if we're still connected
  if (wifi.isConnected()) {
    // Print connection status every 10 seconds
    static unsigned long lastCheck = 0;
    if (millis() - lastCheck > 10000) {
      Serial.println("WiFi still connected!");
      
      // Print signal strength and quality
      Serial.print("Signal Strength (RSSI): ");
      Serial.print(wifi.getSignalStrength());
      Serial.println(" dBm");
      
      Serial.print("Signal Quality: ");
      Serial.print(wifi.getSignalQuality());
      Serial.println("%");
      
      lastCheck = millis();
    }
  } else {
    Serial.println("WiFi connection lost. Attempting to reconnect...");
    if (wifi.reconnect()) {
      Serial.println("Reconnected successfully!");
      printNetworkInfo();
    } else {
      Serial.println("Reconnection failed!");
      // Wait a bit before trying again
      delay(5000);
    }
  }
  
  // Other loop code here
  delay(1000);
}

void printNetworkInfo() {
  Serial.println("===== Network Information =====");
  Serial.print("SSID: ");
  Serial.println(wifi.getSSID());
  
  Serial.print("IP Address: ");
  Serial.println(wifi.getIP());
  
  Serial.print("MAC Address: ");
  Serial.println(wifi.getMacAddress());
  
  Serial.print("Signal Strength (RSSI): ");
  Serial.print(wifi.getSignalStrength());
  Serial.println(" dBm");
  
  Serial.print("Signal Quality: ");
  Serial.print(wifi.getSignalQuality());
  Serial.println("%");
  Serial.println("==============================");
}