#pragma once

void setRelayDurations(JsonDocument& doc);
void updateRelayTimings();

// - - - - - - - - - - - - - - - - - - - - - - - - - - -

// Handles the incoming payload. Returns it into a "JsonDocument" format.
JsonDocument handleJSON(char * payload) {
	JsonDocument doc;
	DeserializationError error = deserializeJson(doc, payload);
	if (error) {
		Serial.print("deserializeJson() failed: ");
		Serial.println(error.c_str());
	}
	return doc;
}

// Subscribes to certain topics on the MQTT Server.
void SuscribeMqtt() {

	  String topicPlayback = String("vesper/") + DEV_ID + "/control/playback";
    String topicSetMelody = String("vesper/") + DEV_ID + "/control/setMelody";
    String topicAddMelody = String("vesper/") + DEV_ID + "/control/addMelody";
		String topicAddSchedule = String("vesper/") + DEV_ID + "/control/addSchedule";
		String topicRelayTimers = String("vesper/") + DEV_ID + "/control/settings/relayTimers";

    uint16_t control_id = mqttClient.subscribe(topicPlayback.c_str(), 2);
    Serial.print("Subscribing to Playback Control topic, QoS 2, packetId: ");
    Serial.println(control_id);

		uint16_t set_melody_id = mqttClient.subscribe(topicSetMelody.c_str(), 2);
    Serial.print("Subscribing to Set-Melody topic, QoS 2, packetId: ");
    Serial.println(set_melody_id);

	// doesn't work yet:
    uint16_t add_melody_id = mqttClient.subscribe(topicAddMelody.c_str(), 2);
    Serial.print("Subscribing to Add-Melody topic, QoS 2, packetId: ");
    Serial.println(add_melody_id);

		uint16_t add_schedule_id = mqttClient.subscribe(topicAddSchedule.c_str(), 2);
		Serial.print("Subscribing to Add-Schedule topic, QoS 2, packetId: ");
		Serial.println(add_schedule_id);

		uint16_t relay_timers_id = mqttClient.subscribe(topicRelayTimers.c_str(), 2);
		Serial.print("Subscribing to Relay-Timers topic, QoS 2, packetId: ");
		Serial.println(relay_timers_id);

}

// Handles incoming MQTT messages on subscribed topics.
// Could move logic out of this into a dedicated function.
void OnMqttReceived(char * topic, char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {

	String topicPlayback = String("vesper/") + DEV_ID + "/control/playback";
  String topicSetMelody = String("vesper/") + DEV_ID + "/control/setMelody";
  String topicAddMelody = String("vesper/") + DEV_ID + "/control/addMelody";
	String topicAddSchedule = String("vesper/") + DEV_ID + "/control/addSchedule";
	String topicRelayTimers = String("vesper/") + DEV_ID + "/control/settings/relayTimers";

	// Don't know what this is. Check it out later.
	//String payloadContent = String(payload).substring(0, len);

	if (String(topic) == topicPlayback){
		player.command(payload);
	}

	else if (String(topic) == topicSetMelody) {
		player.setMelodyAttributes(handleJSON(payload));
		player.loadMelodyInRAM(melody_steps);
	}

	else if (String(topic) == topicAddMelody) {
		// Handle adding melody
		Serial.println("Adding melody...");
		// You can call a function here to handle adding the melody
	}

	else if (String(topic) == topicAddSchedule) {
		updateSchedule(handleJSON(payload));
	}

	else if (String(topic) == topicRelayTimers) {
		updateRelayTimings(handleJSON(payload));
	}

	else {
		// Handle unknown topics
		Serial.println("Unknown topic received.");
	}
}

// Publishes a message on the MQTT server. Message passed as an argument.
void PublishMqtt(const char * data) {
    String topicData = String("vesper/") + DEV_ID + "/data";
    mqttClient.publish(topicData.c_str(), 0, true, data);
}
