//Inject zeroconf attr into the MDNS respond (For scanning by ArozOS)
void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
  //Define the domain of the HDSv2 devices
  MDNS.addDynamicServiceTxt(p_hService, "domain", "fl1010-rgb.hds.imuslab.com");
  MDNS.addDynamicServiceTxt(p_hService, "protocol", "hdsv3");

  //Define the OEM written values
  MDNS.addDynamicServiceTxt(p_hService, "uuid", deviceUUID.c_str());
  MDNS.addDynamicServiceTxt(p_hService, "name", (String(MDNS_NAME) + "_" + deviceUUID).c_str());
  MDNS.addDynamicServiceTxt(p_hService, "model", DEVICE_NAME);
  MDNS.addDynamicServiceTxt(p_hService, "vendor", "imuslab");
  MDNS.addDynamicServiceTxt(p_hService, "version", "1.00");
}

void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
  MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
}

//Handlers for Web Server
void handle_index() {
  server.send(200, "text/html", "[OK] RGB Fill Light - WiFi Mode API Server");
}


void handle_status() {
  DynamicJsonDocument doc(256);
  doc["r"] = values[0];
  doc["g"] = values[1];
  doc["b"] = values[2];

  String json;
  serializeJson(doc, json);
  server.send(200, "application/json", json.c_str());
}

void handle_uuid() {
  server.send(200, "application/json", "\"" + deviceUUID + "\"");
}

void handle_setRGB() {
  String r = server.arg("r");
  String g = server.arg("g");
  String b = server.arg("b");

  if (r.length() > 0) {
    int intR = r.toInt();
    if (intR < 0 || intR > 255) {
      server.send(200, "application/json", "{\"error\":\"invalid r (red) value given\"}");
      return;
    }

    values[0] = intR;
  }

  if (g.length() > 0) {
    int intG = g.toInt();
    if (intG < 0 || intG > 255) {
      server.send(200, "application/json", "{\"error\":\"invalid g (green) value given\"}");
      return;
    }

    values[1] = intG;
  }

  if (b.length() > 0) {
    int intB = b.toInt();
    if (intB < 0 || intB > 255) {
      server.send(200, "application/json", "{\"error\":\"invalid b (blue) value given\"}");
      return;
    }

    values[2] = intB;
  }
  //Update light color
  setLightColor(values[0], values[1], values[2]);

  //Return ok
  server.send(200, "application/json", "\"ok\"");
}

void handle_setColorTemp() {
  String k = server.arg("k");
  String b = server.arg("b");

  int intK = 5000;
  if (k.length() > 0) {
    intK = k.toInt();
    if (intK < WHITE_MIN_TEMP) {
      server.send(200, "application/json", "{\"error\":\"color temperature too low\"}");
      return;
    } else if (intK > WHITE_MAX_TEMP) {
      server.send(200, "application/json", "{\"error\":\"color temperature too high\"}");
      return;
    }
  } else {
    server.send(200, "application/json", "{\"error\":\"k (color temperature) is not set\"}");
    return;
  }

  int brightness = 128; //50%
  if (b.length() > 0) {
    int intB = b.toInt();
    if (intB < 0 || intB > 255) {
      server.send(200, "application/json", "{\"error\":\"invalid b (brightness) value given\"}");
      return;
    }

    brightness = intB;
  }

  //Set color temperature
  setColorTemperature(intK, brightness);

  //Return ok
  server.send(200, "application/json", "\"ok\"");
}

void handle_off() {
  //Set RGB to 0
  //Update light color
  values[0] = 0;
  values[1] = 0;
  values[2] = 0;
  setLightColor(values[0], values[1], values[2]);

  //Return ok
  server.send(200, "application/json", "\"ok\"");
}

void registerAPI() {
  // Connect to Wi-Fi
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());


  //Startup MDNS Responder
  MDNS.setHostProbeResultCallback(hostProbeResult);
  if (!MDNS.begin((String(MDNS_NAME) + "_" + deviceUUID).c_str())) {
    Serial.println("Error setting up MDNS responder!");
  }

  //Advertise the port that you are using
  MDNS.addService("http", "tcp", LISTENING_PORT);
  Serial.println("mDNS responder started");

  //Restful API
  server.on("/", handle_index);
  server.on("/status", handle_status);
  server.on("/uuid", handle_uuid);
  server.on("/api/rgb", handle_setRGB);
  server.on("/api/temp", handle_setColorTemp);
  server.on("/api/off", handle_off);

  delay(100);
  server.begin();
  Serial.println("Restful API server started");
  Serial.print("Listening on port: ");
  Serial.println(LISTENING_PORT);
}
