#include "JoinChannelRequest.hpp"
#include "../../include/XmlRpc.h"
#include "../../responses/JoinChannelResponse.hpp"
#include "../../XmlRpcppClient.hpp"
#include <iostream>

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

xmlrpcpp::JoinChannelRequest::JoinChannelRequest(){}

xmlrpcpp::JoinChannelRequest::~JoinChannelRequest(){}

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

mole::serv::Response *
xmlrpcpp::JoinChannelRequest::execute(Client * c){
    XmlRpcppClient * client = static_cast <XmlRpcppClient *> (c);
    XmlRpcClient * rpcClient = client->getRealClient();
    XmlRpcValue args, result;

    args[0] = c->getId();
    args[1] = channelName;
    
    if (rpcClient->execute(name().c_str(), args, result)){
	serv::JoinChannelResponse * resp = new mole::serv::JoinChannelResponse();
	if (result.hasMember("faultCode"))
	    resp->error(result["faultCode"], result["faultString"]);
	else
	    resp->setResult("ok");
	return resp;
    }
    return NULL;
}
