#include "ListUsersRequest.hpp"
#include "../../include/XmlRpc.h"
#include "../../responses/ListUsersResponse.hpp"
#include "../../XmlRpcppClient.hpp"
#include <iostream>

using namespace mole;
using namespace ctx;
using namespace xmlrpcpp;
using namespace XmlRpc;

xmlrpcpp::ListUsersRequest::ListUsersRequest(){}

xmlrpcpp::ListUsersRequest::~ListUsersRequest(){}

xmlrpcpp::ListUsersRequest *
xmlrpcpp::ListUsersRequest::newRequest(){
    return new xmlrpcpp::ListUsersRequest();
}

mole::serv::Response *
xmlrpcpp::ListUsersRequest::execute(Client * c){
    XmlRpcppClient * client = static_cast <XmlRpcppClient *> (c);
    XmlRpcClient * rpcClient = client->getRealClient();

    XmlRpcValue args, result;
    args[0] = c->getId();
    args[1] = channelName; // The chan to look at
    if (rpcClient->execute(name().c_str(), args, result)){
	serv::ListUsersResponse * resp = new serv::ListUsersResponse();
	if (result.hasMember("faultcode"))
	    resp->error(result["faultCode"], result["faultString"]);
	else {
	    for (int i = 0; i < result.size(); i++){
		resp->addUser(result[i]);
	    }
	}
	return resp;
    }
    return NULL;
}
