#include <WiFi.h>
#include <PubSubClient.h>
#include <driver/i2s.h>
#include <Freenove_WS2812_Lib_for_ESP32.h> 

// --- CONNECTIVITY ---
const char* WIFI_SSID = "Aussie Broadband 8729";
const char* WIFI_PASS = "Ffdfmunfca";

const char* MQTT_BROKER = "192.168.20.13"; 
const int   MQTT_PORT   = 1883;
const char* MQTT_USER   = "mqtt-user";
const char* MQTT_PASS   = "sam4jo";

const char* TOPIC_AUDIO = "voice/audio_stream";
const char* TOPIC_STATUS = "voice/status"; 

// --- HARDWARE ---
#define I2S_SCK 4
#define I2S_WS  5
#define I2S_SD  6
#define I2S_PORT I2S_NUM_0
#define LED_PIN  1  // Updated to Pin 1
#define NUM_LEDS 12

// --- TUNING ---
#define VAD_THRESHOLD 1000 
const int BLOCK_SIZE = 512;
int16_t sBuffer[BLOCK_SIZE];

WiFiClient espClient;
PubSubClient mqtt(espClient);
Freenove_ESP32_WS2812 strip(NUM_LEDS, LED_PIN, 0, TYPE_GRB);

// --- STATES ---
enum DeviceState { 
  STATE_IDLE, 
  STATE_STREAMING,   // Sending audio (LED OFF)
  STATE_LISTENING,   // Wake Word Confirmed (LED BLUE)
  STATE_PROCESSING,  // Sending to Whisper (LED RED)
  STATE_SUCCESS      // Done (LED GREEN)
};
DeviceState currentState = STATE_IDLE;
unsigned long stateTimer = 0;

const i2s_config_t i2s_config = {
  .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
  .sample_rate = 16000,
  .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
  .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
  .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
  .dma_buf_count = 8,
  .dma_buf_len = 64,
  .use_apll = true
};

const i2s_pin_config_t pin_config = {
  .bck_io_num = I2S_SCK,
  .ws_io_num = I2S_WS,
  .data_out_num = I2S_PIN_NO_CHANGE,
  .data_in_num = I2S_SD
};

// --- LED HELPER ---
void setRingColor(uint8_t r, uint8_t g, uint8_t b) {
  strip.setAllLedsColor(r, g, b);
  strip.show();
}

// --- MQTT HANDLING ---
void mqttCallback(char* topic, byte* payload, unsigned int length) {
  char msg[length + 1];
  memcpy(msg, payload, length);
  msg[length] = '\0'; // Null terminate
  
  String message = String(msg);
  Serial.print("Status Recv: "); Serial.println(message);

  if (message == "WAKE") {
    // Server heard "Hey Jarvis"
    currentState = STATE_LISTENING;
    setRingColor(0, 0, 255); // BLUE
  }
  else if (message == "OK") {
    // Command executed
    currentState = STATE_SUCCESS;
    stateTimer = millis();
    setRingColor(0, 255, 0); // GREEN
  }
}

void setup() {
  delay(2000);
  Serial.begin(115200);
  
  mqtt.setBufferSize(2048);
  
  strip.begin();
  strip.setBrightness(30);
  setRingColor(50, 50, 50); // White on Boot

  i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  i2s_set_pin(I2S_PORT, &pin_config);
  
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    setRingColor(0, 0, 100); delay(100); setRingColor(0,0,0);
  }
  
  mqtt.setServer(MQTT_BROKER, MQTT_PORT);
  mqtt.setCallback(mqttCallback);
  
  setRingColor(0,0,0); // Idle = Off
}

void loop() {
  if (!mqtt.connected()) {
    if (mqtt.connect("ESP32Mic", MQTT_USER, MQTT_PASS)) {
      mqtt.subscribe(TOPIC_STATUS);
    }
  }
  mqtt.loop();

  // Reset Success/Green LED after 3 seconds
  if (currentState == STATE_SUCCESS && millis() - stateTimer > 3000) {
    currentState = STATE_IDLE;
    setRingColor(0,0,0);
  }

  size_t bytesRead;
  i2s_read(I2S_PORT, &sBuffer, sizeof(sBuffer), &bytesRead, portMAX_DELAY);

  // Gain Boost (x6)
  for (int i = 0; i < bytesRead / 2; i++) {
    int32_t boosted = sBuffer[i] * 6; 
    if (boosted > 32767) boosted = 32767;
    if (boosted < -32768) boosted = -32768;
    sBuffer[i] = (int16_t)boosted;
  }
  
  // Calculate Volume
  long sum = 0;
  for (int i = 0; i < bytesRead / 2; i++) {
    sum += abs(sBuffer[i]);
  }
  int avg = sum / (bytesRead / 2);

  // --- LOGIC ---
  
  // 1. Detect Sound (Invisible)
  if (currentState == STATE_IDLE && avg > VAD_THRESHOLD) {
    currentState = STATE_STREAMING;
    stateTimer = millis();
    // LED stays OFF (We don't know if it's Jarvis yet)
    
    // Send trigger buffer
    mqtt.publish(TOPIC_AUDIO, (const uint8_t*)sBuffer, bytesRead);
  }

  // 2. Stream Audio
  // We stream if we are just hearing noise OR if we are actively listening for a command
  if (currentState == STATE_STREAMING || currentState == STATE_LISTENING) {
    mqtt.publish(TOPIC_AUDIO, (const uint8_t*)sBuffer, bytesRead);
    
    if (avg > VAD_THRESHOLD) stateTimer = millis();
    
    // Silence Timeout (1.5s)
    if (millis() - stateTimer > 1500) {
      if (currentState == STATE_LISTENING) {
        // If we were listening, now we process
        currentState = STATE_PROCESSING;
        mqtt.publish(TOPIC_STATUS, "processing"); 
        setRingColor(255, 0, 0); // RED (Thinking)
      } else {
        // If we were just streaming noise and never heard Jarvis, go back to sleep
        currentState = STATE_IDLE;
        setRingColor(0, 0, 0);
      }
    }
  }
}
