/* ESP32 HTTP IoT Server Example for Wokwi.com

  https://wokwi.com/arduino/projects/320964045035274834

  To test, you need the Wokwi IoT Gateway, as explained here:

  https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway

  Then start the simulation, and open http://localhost:9080
  in another browser tab.

  Note that the IoT Gateway requires a Wokwi Club subscription.
  To purchase a Wokwi Club subscription, go to https://wokwi.com/club
*/

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>


#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6

WebServer server(80);

const int LED1 = 26;
const int LED2 = 27;
const int LED3 = 12;
const int LED4 = 25;
const int LED5 = 5;
const int LED6 = 18;
const int LED7 = 21;
const int LED8 = 32;
String response="";

class Puerto{
  private:
    int posicion;
    int zona;
    int consumo;
    bool estado;
  public: 
    Puerto (){}
    Puerto(int p, int z, int c, bool e):posicion(p), zona(z), consumo(c),estado(e) {}
    int getPosicion(){return posicion;}
    int getZona(){return zona;}  
    int getConsumo(){return consumo;}  
    bool getEstado(){return estado;}  
    void setPosicion(int p){posicion=p;}
    void setZona(int z){zona=z;}
    void setConsumo(int c){consumo=c;}
    void setEstado(bool e){estado=e;}

    
    

};

Puerto puerto[8];
String body="";
bool led1State = false;
bool led2State = false;

void sendHtml() {
}

void setup(void) {
  Serial.begin(115200);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);



  WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
  Serial.print("Connecting to WiFi ");
  Serial.print(WIFI_SSID);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println(" Connected!");

  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());




  // registro de puertos disponibles
  
  
  
  puerto[0]=Puerto(LED1,1001,0,false);
  puerto[1]=Puerto(LED2,1001,0,false);
  puerto[2]=Puerto(LED3,1002,0,false);
  puerto[3]=Puerto(LED4,1002,0,false);
  puerto[4]=Puerto(LED5,1003,0,false);
  puerto[5]=Puerto(LED6,1003,0,false);
  puerto[6]=Puerto(LED7,1004,0,false);
  puerto[7]=Puerto(LED8,1004,0,false);


  Serial.println(puerto[0].getPosicion());






  server.on("/", sendHtml);

  server.on(UriBraces("/arduino/{}"), []() { // por requerimientos 
    String led = server.pathArg(0);
    Serial.print("Toggle LED #");
    Serial.println(led);
    


    switch (led.toInt()) {

      // Set up
      case 1: 
        enviarPuertos();
        
        break;
      // Encender o apagar 
      case 2:  // handle 


        led=server.arg("puerto"); 
          
          Serial.println(led);
          Puerto ledPuerto;

          for (int i =0;i<sizeof puerto/sizeof puerto[0];i=i+1){
            if (puerto[i].getPosicion()==led.toInt()){
              puerto[i].setEstado(!puerto[i].getEstado());
              ledPuerto=puerto[i];
            }
          }
          Serial.println(ledPuerto.getPosicion());
          
          
          digitalWrite(ledPuerto.getPosicion(),ledPuerto.getEstado());//cambia estado de led

          response="ok";
        break;
      

      

    }
  
  server.send(200, "application/json", response); 

    
  });

  server.begin();
  Serial.println("HTTP server started");
}


void enviarPuertos(){
  response="";
  for (int i =0;i<sizeof puerto/sizeof puerto[0];i=i+1){
      
     response=response+puerto[i].getPosicion();
      response=response+","+puerto[i].getConsumo();
      response=response+","+puerto[i].getEstado();
      response=response+";";
 }
 




   
}

void crearDispositivo(){
  
}

void loop(void) {
  server.handleClient();

  delay(2);


}