//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

#define REMOTEXY_MODE__WIFI_CLOUD

#include <WiFi.h>
#include <RemoteXY.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP32Servo.h>

// RemoteXY connection settings 
#define REMOTEXY_WIFI_SSID "FABLAB-UNI"
#define REMOTEXY_WIFI_PASSWORD "fablabuni321"
#define REMOTEXY_CLOUD_SERVER "cloud.remotexy.com"
#define REMOTEXY_CLOUD_PORT 6376
#define REMOTEXY_CLOUD_TOKEN "b20f9f912916ebac532a12c0555cb59a"
#define REMOTEXY_ACCESS_PASSWORD "1234"

#pragma pack(push, 1)  
uint8_t const PROGMEM RemoteXY_CONF_PROGMEM[] =   // 65 bytes V19 
  { 255,5,0,1,0,58,0,19,0,0,0,73,84,73,78,69,82,65,78,84,
  69,0,31,1,106,200,1,1,3,0,10,28,11,48,48,48,4,26,31,79,
  78,0,31,79,70,70,0,14,16,71,73,27,77,0,94,26,70,30,111,39,
  39,16,26,37,0 };
  
struct {
  uint8_t pushSwitch_01;
  uint8_t editTime_01_hour;
  uint8_t editTime_01_minute;

  uint8_t led_01_r;
  uint8_t led_01_g;
  uint8_t led_01_b;

  uint8_t connect_flag;
} RemoteXY;   
#pragma pack(pop)

/////////////////////////////////////////////
//           OLED CONFIG                  //
/////////////////////////////////////////////

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C

#define SDA_PIN D4
#define SCL_PIN D5

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

/////////////////////////////////////////////
//           PINES                        //
/////////////////////////////////////////////

#define PIN_R D3
#define PIN_G D2
#define PIN_B D1
#define PIN_SERVO D7

Servo miServo;

/////////////////////////////////////////////
//                SETUP                   //
/////////////////////////////////////////////

void setup() 
{
  RemoteXY_Init ();

  // RGB
  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);

  // Servo
  miServo.attach(PIN_SERVO);

  // OLED
  Wire.begin(SDA_PIN, SCL_PIN);
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Sistema iniciado");
  display.display();
}

/////////////////////////////////////////////
//                LOOP                    //
/////////////////////////////////////////////

void loop() 
{ 
  RemoteXY_Handler ();

  // ====== NUEVA LOGICA RGB ======
  static uint8_t lastState = 0;
  static int colorIndex = 0;

  // Detectar cambio OFF -> ON
  if (RemoteXY.pushSwitch_01 == 1 && lastState == 0) {
    colorIndex++;
    if (colorIndex > 5) colorIndex = 0;
  }

  lastState = RemoteXY.pushSwitch_01;

  // CONTROL RGB con colores predefinidos
  if (RemoteXY.pushSwitch_01 == 1) {

    switch (colorIndex) {
      case 0: // Rojo
        analogWrite(PIN_R, 255);
        analogWrite(PIN_G, 0);
        analogWrite(PIN_B, 0);
        break;

      case 1: // Verde
        analogWrite(PIN_R, 0);
        analogWrite(PIN_G, 255);
        analogWrite(PIN_B, 0);
        break;

      case 2: // Azul
        analogWrite(PIN_R, 0);
        analogWrite(PIN_G, 0);
        analogWrite(PIN_B, 255);
        break;

      case 3: // Amarillo
        analogWrite(PIN_R, 255);
        analogWrite(PIN_G, 255);
        analogWrite(PIN_B, 0);
        break;

      case 4: // Morado
        analogWrite(PIN_R, 255);
        analogWrite(PIN_G, 0);
        analogWrite(PIN_B, 255);
        break;

      case 5: // Cyan
        analogWrite(PIN_R, 0);
        analogWrite(PIN_G, 255);
        analogWrite(PIN_B, 255);
        break;
    }

  } else {
    // Apagar RGB
    analogWrite(PIN_R, 0);
    analogWrite(PIN_G, 0);
    analogWrite(PIN_B, 0);
  }

  // 🎮 CONTROL SERVO (SE MANTIENE IGUAL)
  if (RemoteXY.pushSwitch_01 == 1) {
    miServo.write(90);
  } else {
    miServo.write(0);
  }

  // 🖥️ OLED (SE MANTIENE IGUAL)
  display.clearDisplay();

  display.setCursor(0,0);
  display.println("ITINERANTE");

  display.setCursor(0,15);
  display.print("BUTTON: ");
  display.println(RemoteXY.pushSwitch_01 ? "ON" : "OFF");

  display.setCursor(0,30);
  display.print("RGB:");
  display.print(colorIndex); // ahora mostramos el color activo
  display.println("");

  display.setCursor(0,45);
  display.print("Hora: ");
  display.print(RemoteXY.editTime_01_hour);
  display.print(":");
  display.println(RemoteXY.editTime_01_minute);

  display.display();

  RemoteXY_delay(100);
}