/*
 * This is a basic example on how to use Espalexa and its device declaration methods.
 */ 

#include <WiFi.h>
#include <Espalexa.h>

// prototypes
boolean connectWifi();

//callback functions
void firstLightChanged(uint8_t brightness);
void secondLightChanged(uint8_t brightness);
void thirdLightChanged(uint8_t brightness);

// Change this!!
const char* ssid = "Wokwi-GUEST";   // For Wokwi tests
const char* password = "";

boolean wifiConnected = false;

Espalexa espalexa;

EspalexaDevice* device3; //this is optional
int light=33;

void setup() {
  Serial.begin(115200);
  // Initialise wifi connection
  wifiConnected = connectWifi();
 // pinMode(LED_BUILTIN, OUTPUT);  
  pinMode(light, OUTPUT); 
  
  if(wifiConnected){
    
    // Define your devices here. 
    espalexa.addDevice("Light 1", firstLightChanged); //simplest definition, default state off
    espalexa.addDevice("Light 2", secondLightChanged, 255); //third parameter is beginning state (here fully on)
    
    device3 = new EspalexaDevice("Light 3", thirdLightChanged); //you can also create the Device objects yourself like here
    espalexa.addDevice(device3); //and then add them
    device3->setValue(128); //this allows you to e.g. update their state value at any time!

    espalexa.begin();
    
  } else
  {
    while (1) {
      Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
      delay(2500);
    }
  }
}
 
void loop()
{
   espalexa.loop();
   delay(1);
}

//our callback functions
void firstLightChanged(uint8_t brightness) {
    Serial.print("Device 1 changed to ");
    
    digitalWrite(light, HIGH);
    //do what you need to do here

    //EXAMPLE
    if (brightness) {
      Serial.print("ON, brightness ");
      Serial.println(brightness);
      digitalWrite(light, HIGH);
    }
    else  {
      Serial.println("OFF");
       digitalWrite(light, LOW);
    }
}

void secondLightChanged(uint8_t brightness) {
  
 // digitalWrite(LED_BUILTIN, LOW);
  //do what you need to do here
}

void thirdLightChanged(uint8_t brightness) {
 // digitalWrite(LED_BUILTIN, HIGH);
  //do what you need to do here
}

// connect to wifi – returns true if successful or false if not
boolean connectWifi() {
  boolean state = true;
  int i = 0;
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
  Serial.println("Connecting to WiFi");

  // Wait for connection
  Serial.print("Connecting...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    if (i > 20){
      state = false; break;
    }
    i++;
  }
  Serial.println("");
  if (state){
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  }
  else {
    Serial.println("Connection failed.");
  }
  return state;
}