
#include <WiFi.h>

const char* ssid = "ESP32_AP";
const char* password = "12345678";

// RGB LED Pins
const int PIN_R = 27;
const int PIN_G = 26;
const int PIN_B = 25;
const int PIN_BUZZER = 14;

// RGB type: false = common cathode, true = common anode
const bool COMMON_ANODE = false;

WiFiServer server(8080);
WiFiClient client;

bool rgbOn = false;
int colorIndex = 0;

// Color table (R, G, B)
const int colors[][3] = {
  {255, 0, 0},    // 0 Red
  {0, 255, 0},    // 1 Green
  {0, 0, 255},    // 2 Blue
  {255, 255, 0},  // 3 Yellow
  {0, 255, 255},  // 4 Cyan
  {255, 0, 255},  // 5 Purple
  {255, 255, 255} // 6 White
};
const int numColors = 7;

// Buzzer state
bool buzzerOn = false;

void setup() {
  Serial.begin(115200);
  delay(1000);

  pinMode(PIN_R, OUTPUT);
  pinMode(PIN_G, OUTPUT);
  pinMode(PIN_B, OUTPUT);
  pinMode(PIN_BUZZER, OUTPUT);

  setRGB(0, 0, 0);
  noTone(PIN_BUZZER);

  Serial.println("\n=== ESP32 Receiver ===");

  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP: ");
  Serial.println(IP);

  server.begin();
  Serial.println("TCP Server: 8080");
}

void setRGB(int r, int g, int b) {
  if (COMMON_ANODE) {
    analogWrite(PIN_R, 255 - r);
    analogWrite(PIN_G, 255 - g);
    analogWrite(PIN_B, 255 - b);
  } else {
    analogWrite(PIN_R, r);
    analogWrite(PIN_G, g);
    analogWrite(PIN_B, b);
  }
}

void updateRGB() {
  if (!rgbOn) {
    setRGB(0, 0, 0);
    Serial.println("[Status] RGB OFF");
  } else {
    setRGB(colors[colorIndex][0], colors[colorIndex][1], colors[colorIndex][2]);
    Serial.print("[Status] RGB ON Color#");
    Serial.print(colorIndex);
    Serial.print(" (");
    Serial.print(colors[colorIndex][0]);
    Serial.print(",");
    Serial.print(colors[colorIndex][1]);
    Serial.print(",");
    Serial.print(colors[colorIndex][2]);
    Serial.println(")");
  }
}

void handleCommand(String cmd) {
  cmd.trim();
  Serial.print("[Command] ");
  Serial.println(cmd);

  if (cmd == "RGB_ON") {
    rgbOn = true;
    updateRGB();
    client.println("OK: RGB_ON");
  }
  else if (cmd == "RGB_OFF") {
    rgbOn = false;
    updateRGB();
    client.println("OK: RGB_OFF");
  }
  else if (cmd == "RGB_COLOR") {
    // ========== Fix 1: Color adjustment only works when light is on ==========
    if (!rgbOn) {
      Serial.println("[Rejected] Light is off, color adjustment invalid");
      client.println("ERR: LIGHT_IS_OFF");
      return;
    }
    colorIndex = (colorIndex + 1) % numColors;
    updateRGB();
    client.println("OK: RGB_COLOR");
  }
  else if (cmd == "BUZZ_ON") {
    // ========== Fix 2: Passive buzzer uses tone() ==========
    buzzerOn = true;
    tone(PIN_BUZZER, 1000);  // 1000Hz square wave
    Serial.println("[Status] Buzzer ON (1000Hz)");
    client.println("OK: BUZZ_ON");
  }
  else if (cmd == "BUZZ_OFF") {
    buzzerOn = false;
    noTone(PIN_BUZZER);  // Stop sound
    digitalWrite(PIN_BUZZER, LOW);
    Serial.println("[Status] Buzzer OFF");
    client.println("OK: BUZZ_OFF");
  }
  else if (cmd == "REMOTE_CONNECTED") {
    client.println("OK: CONNECTED");
  }
  else {
    client.println("ERR: UNKNOWN");
  }
}

void loop() {
  if (server.hasClient()) {
    if (!client || !client.connected()) {
      if (client) client.stop();
      client = server.available();
      Serial.println("[Network] Remote controller connected");
    }
  }

  if (client && client.connected() && client.available()) {
    String cmd = client.readStringUntil('\n');
    handleCommand(cmd);
  }
}
