#pragma once

#include <cstdint>
#include <memory>
#include <string>
#include <vector>

#include <unitree/robot/r1/audio/audio_client.hpp>

namespace r1_voice {

struct TtsConfig {
    std::string piper_command = "python3 -m piper";

    std::string model =
        "/home/jvizner/esarobotech/piper/cs_medium.onnx";

    std::string output_wav =
        "/tmp/r1_tts.wav";

    int volume = 100;

    std::string app_name = "r1_voice";
};

class TextToSpeech {
public:
    explicit TextToSpeech(TtsConfig config);

    bool init(
        const std::string& network_interface
    );

    bool speak(
        const std::string& text
    );

private:
    TtsConfig config_;

    std::unique_ptr<
        unitree::robot::r1::AudioClient
    > client_;

    bool initialized_ = false;

    bool synthesize(
        const std::string& text,
        const std::string& wav_path
    );

    bool readWavPcm(
        const std::string& wav_path,
        std::vector<uint8_t>& pcm,
        uint32_t& sample_rate,
        uint16_t& channels,
        uint16_t& bits_per_sample
    );

    bool playPcm(
        const std::vector<uint8_t>& pcm
    );
};

} // namespace r1_voice
