#include <boost/python.hpp>
#include <boost/python/list.hpp>
#include <boost/python/extract.hpp>

#include <string>
#include <list>
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>

//#include <sstream>
//#include <vector>

struct RTService
{
private:
     /* data */
    const std::string RTSCOMMANDQUEUE = "RTSSERVICECOMMANDQUEUE";
    const int MAXMSGSIZE = 250;
    const int MAXMSGCOUNT = 25;
    boost::interprocess::message_queue m_mq;
public:
    RTService(/* args */) :
        m_mq(boost::interprocess::open_or_create, RTSCOMMANDQUEUE.c_str(), MAXMSGCOUNT, MAXMSGSIZE)        
    {
    }

    ~RTService() {}

    bool createMonitoring(std::string id, int samplerate, int sampleperiod, int downtime, std::string status) { 
        //TODO ML: add this to the M4Core
        boost::property_tree::ptree pt;
        pt.put("id", id);
        pt.put("samplerate", samplerate);
        pt.put("sampleperiod", sampleperiod);
        pt.put("downtime", downtime);
        pt.put("status", status);        
        return executeCmd(pt); 
    }

    bool deleteMonitoring(std::string id) { 
        //TODO ML: add this to the M4Core
        return true; 
    }

    std::string getMonitoringState(std::string id) {
        //TODO ask the M4Core for the Monitpring Status
        return "Start";
    }

    boost::python::list getMonitoringStates() {
        //TODO ask the M4Core for the Monitpring Status
        boost::python::list list;        
        return list;
    }

    bool setMonitoringStatus(std::string id, std::string status) {
        //set the Status
        return true;
    }

private:
    bool executeCmd(boost::property_tree::ptree pt) {
        BOOST_TRY{

            bool bCmdQueueRun = true;
            unsigned int priority;
            boost::interprocess::message_queue::size_type recvd_size;

            while (bCmdQueueRun) {
                char rtsCmdbuffer [MAXMSGSIZE];
                boost::property_tree::write_json(rtsCmdbuffer,pt);

                m_mq.send(rtsCmdbuffer, MAXMSGSIZE, 0);

                m_mq.receive(rtsCmdbuffer, sizeof(rtsCmdbuffer), recvd_size, priority);
                //read json and go to cmd execution
                boost::property_tree::read_json(rtsCmdbuffer,pt);

            }


            // //Send 100 numbers
            // for(int i = 0; i < 100; ++i){
            //     mq.send(&i, sizeof(i), 0);
            // }
        }
        BOOST_CATCH(interprocess_exception &ex){
            std::cout << ex.what() << std::endl;
        } BOOST_CATCH_END                
    } 
};



using namespace boost::python;

BOOST_PYTHON_MODULE(rt_service)
{
    class_<RTService>("RT_Service")
        .def("createMonitoring", &RTService::createMonitoring)
        .def("getMonitoringStatus", &RTService::getMonitoringState)
        .def("getAllMonitoringStat", &RTService::getMonitoringStates)
        .def("setMonitoringStatus", &RTService::setMonitoringStatus)
    ;
};



//examples
        // long l = len(msgs);
        // std::stringstream ss;
        // for (long i = 0; i<l; ++i) {
        //     if (i>0) ss << ", ";
        //     std::string s = boost::python::extract<std::string>(msgs[i]);
        //     ss << s;
        // }
        // mMsg = ss.str();
