#include "slp.hpp"
#include "slp_meta.hpp"
#include "slp_server.hpp"
#include "sock_channel.hpp"

#include <algorithm>
#include <iomanip>

/* Call Back for the sd event loop */
static int requestHandler(sd_event_source* /*es*/, int fd, uint32_t /*revents*/,
                          void* /*userdata*/)
{
    int rc = slp::SUCCESS;
    timeval tv{slp::TIMEOUT, 0};
    udpsocket::Channel channel(fd, tv);
    std::vector<uint8_t> recvBuff;
    slp::Message req;
    std::vector<uint8_t> resp;
    // Read the packet
    std::tie(rc, recvBuff) = channel.read();

    if (rc < 0)
    {
        std::cerr << "SLP Error in Read : " << std::hex << rc << "\n";
        return rc;
    }

    // This code currently assume a maximum of 255 bytes in a receive
    // or response message. Enforce that here.
    if (recvBuff.size() > slp::MAX_LEN)
    {
        std::cerr << "Message size exceeds maximum allowed: " << recvBuff.size()
                  << " / " << slp::MAX_LEN << std::endl;

        rc = static_cast<uint8_t>(slp::Error::PARSE_ERROR);
    }
    else
    {
        switch (recvBuff[0])
        {
            case slp::VERSION_2:
            {
                // Parse the buffer and construct the req object
                std::tie(rc, req) = slp::parser::parseBuffer(recvBuff);
                if (!rc)
                {
                    // Passing the req object to handler to serve it
                    std::tie(rc, resp) = slp::handler::processRequest(req);
                }
                break;
            }
            default:
                std::cout << "SLP Unsupported Request Version="
                          << (int)recvBuff[0] << "\n";

                rc = static_cast<uint8_t>(slp::Error::VER_NOT_SUPPORTED);
                break;
        }
    }

    // if there was error during Parsing of request
    // or processing of request then handle the error.
    if (rc)
    {
        resp = slp::handler::processError(req, rc);
    }

    channel.write(resp);
    return slp::SUCCESS;
}

int main()
{
    slp::udp::Server svr(slp::PORT, requestHandler);
    return svr.run();
}
