#include <mpg123.h>
#include <portaudio.h>
#include <stdexcept>
#include "argparse/argparse.hpp"
#include "managers/Manager.hpp"


int main(const int argc, char* argv[]) {
    // initialize the mpg123 library
    if (mpg123_init() != MPG123_OK)
        throw std::runtime_error("Error while initializing mpg123.");

    // initialize the PortAudio library
    if (Pa_Initialize() != paNoError)
        throw std::runtime_error("Could not initialize PortAudio.");

    argparse::ArgumentParser parser("Program");
    parser.add_argument("-h", "--host").help("Host address").default_value(std::string("127.0.0.1"));
    parser.add_argument("-p", "--port").help("Port").default_value(std::string("15650"));
    parser.add_argument("-6", "--ipv6").help("Use IPv6").flag();

    try {
        parser.parse_args(argc, argv);
    } catch (const std::exception& exception) {
        std::cerr << exception.what() << std::endl;
        return EXIT_FAILURE;
    }

    auto manager = drp::managers::Manager(
            parser.get<std::string>("--host"),
            parser.get<std::string>("--port"),
            parser.get<bool>("-6")
    );
    manager.loop();

    // close the libraries
    Pa_Terminate();
    mpg123_exit();

    return EXIT_SUCCESS;
}
