#include "Mole_v0.h"

#include "services/HelloService.hpp"
#include "services/ConnectionService.hpp"
#include "services/ListChannelsService.hpp"
#include "requests/HelloRequest.hpp"
#include "requests/ConnectionRequest.hpp"
#include "requests/xmlrpcpp/HelloRequest.hpp"
#include "requests/xmlrpcpp/ConnectionRequest.hpp"
#include "requests/xmlrpcpp/ListChannelsRequest.hpp"
#include "responses/HelloResponse.hpp"
#include "responses/ConnectionResponse.hpp"
#include "responses/ListChannelsResponse.hpp"

#include "Request.hpp"
#include "include/XmlRpc.h"
#include "Server.hpp"
#include "XmlRpcppClient.hpp"
#include "ServiceManager.hpp"

#include "include/Mole.h"

NS_IMPL_ISUPPORTS1(Mole_v0, IMole_v0)

using namespace mole;
using namespace XmlRpc;

Mole_v0::Mole_v0()
{
  /* member initializers and constructor code */
    client = new ctx::xmlrpcpp::XmlRpcppClient(DEFAULT_IP, DEFAULT_PORT,DEFAULT_PATH);
    manager = new serv::ServiceManager(client);
    manager->loadServices();
}

Mole_v0::~Mole_v0()
{
 /* destructor code */
    delete client;
    delete manager;
}

/* string service (in string entry); */
NS_IMETHODIMP Mole_v0::Service(const char *entry, char **_retval)
{     
#if 0
//Version simple sans appel vers le serveur
  char *info = "[C++] ";
  char *tmp = new char[strlen(info)+strlen(entry)]; 

  strcpy(tmp,info);

 _retval = tmp;
  return NS_OK;

#endif 

    serv::Service * service = client->getService(std::string(NAME_SERV_HELLO));
    if (service){
	ctx::HelloRequest * request = static_cast <ctx::HelloRequest *> (service->getRequest());
	request->init(std::string(entry));
	if (client->execute(service)){
	    serv::HelloResponse * resp = static_cast <serv::HelloResponse *> (service->getResponse());
	    if (resp && (resp->errorCode() == 0)){
		
		size_t size = resp->getText().size() + 1;
		char * buffer = new char[size];
		strncpy(buffer, resp->getText().c_str(), size);
	
		*_retval = buffer;
	    }
	}
	delete service;
    }

    return NS_OK;
}

/* void getList (out PRUint32 count, [array, size_is (count), retval] out string str); */
NS_IMETHODIMP Mole_v0::GetList(PRUint32 *count, char ***str)
{

#if 1

   serv::Service * service = client->getService(std::string(NAME_SERV_LISTCHANNELS));
    if (service){
	ctx::ListChannelsRequest * request = static_cast <ctx::ListChannelsRequest *> (service->getRequest());
	if (client->execute(service)){
	    serv::ListChannelsResponse * resp = static_cast <serv::ListChannelsResponse *> (service->getResponse());
	    if (resp && (resp->errorCode() == 0)){
		
		const static PRUint32 scount = resp->channelsCount();
		char** out = (char**) malloc (scount * sizeof(char*));
		size_t size;

		for(PRUint32 i = 0; i < scount; ++i)
		{		        
		    size = resp->getChannel(i).size() + 1;
		    out[i] = (char*) malloc (size);
		    strncpy(out[i], resp->getChannel(i).c_str(), size);
		}
		*count = scount;
		*str = out;
	    }
	}
	delete service;
    }
#endif
#if 0 
    const static char *strings[] = {"one", "two", "three", "four"};
    const static PRUint32 scount = sizeof(strings)/sizeof(strings[0]);
    char** out = (char**) malloc (scount * sizeof(char*));

    for(PRUint32 i = 0; i < scount; ++i)
    {
	out[i] = (char*) malloc (strlen(strings[i])+1);
	strcpy(out[i], strings[i]);
    }

    *count = scount;
    *str = out;
#endif

    return NS_OK;

}
