string BuildCreateGameBody(string gameName, string descrption)
{
    
    cJSON *bodyJSON = cJSON_CreateObject();
    cJSON *app42JSON = cJSON_CreateObject();
    cJSON *gameJSON = cJSON_CreateObject();
    
    // first construct the user
    cJSON_AddStringToObject(gameJSON, "name", gameName.c_str());
    cJSON_AddStringToObject(gameJSON, "description", descrption.c_str());
    
    // add user to app42
    cJSON_AddItemReferenceToObject(app42JSON, "game", gameJSON);
    
    // add app42 to body
    cJSON_AddItemReferenceToObject(bodyJSON, "app42", app42JSON);
    
    char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON);
    string bodyString = cptrFormatted;
    
    cJSON_Delete(gameJSON);
    cJSON_Delete(app42JSON);
    cJSON_Delete(bodyJSON);
    
    free(cptrFormatted);
    
    return bodyString;
    
}
string BuildCreateRewardBody(string rewardName, string descrption)
{
    
    cJSON *bodyJSON = cJSON_CreateObject();
    cJSON *app42JSON = cJSON_CreateObject();
    cJSON *rewardsJSON = cJSON_CreateObject();
    cJSON *rewardJSON = cJSON_CreateObject();
    
    cJSON_AddStringToObject(rewardJSON, "name", rewardName.c_str());
    cJSON_AddStringToObject(rewardJSON, "description", descrption.c_str());
    cJSON_AddItemReferenceToObject(rewardsJSON, "reward", rewardJSON);
    
    cJSON_AddItemReferenceToObject(app42JSON, "rewards", rewardsJSON);
    
    cJSON_AddItemReferenceToObject(bodyJSON, "app42", app42JSON);
    
    char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON);
    string bodyString = cptrFormatted;
    
    cJSON_Delete(rewardsJSON);
    cJSON_Delete(rewardJSON);
    cJSON_Delete(app42JSON);
    cJSON_Delete(bodyJSON);
    
    free(cptrFormatted);
    
    return bodyString;
    
}
string BuildRewardBody(string gameName, string userName, string rewardName, double rewardPoints)
{
    
    cJSON *bodyJSON = cJSON_CreateObject();
    cJSON *app42JSON = cJSON_CreateObject();
    cJSON *rewardsJSON = cJSON_CreateObject();
    cJSON *rewardJSON = cJSON_CreateObject();
    
    cJSON_AddStringToObject(rewardJSON, "gameName", gameName.c_str());
    cJSON_AddNumberToObject(rewardJSON, "points", rewardPoints);
    cJSON_AddStringToObject(rewardJSON, "name", rewardName.c_str());
    cJSON_AddStringToObject(rewardJSON, "userName", userName.c_str());
    cJSON_AddItemReferenceToObject(rewardsJSON, "reward", rewardJSON);
    
    cJSON_AddItemReferenceToObject(app42JSON, "rewards", rewardsJSON);
    
    cJSON_AddItemReferenceToObject(bodyJSON, "app42", app42JSON);
    
    char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON);
    string bodyString = cptrFormatted;
    
    cJSON_Delete(rewardsJSON);
    cJSON_Delete(rewardJSON);
    cJSON_Delete(app42JSON);
    cJSON_Delete(bodyJSON);
    
    free(cptrFormatted);
    
    return bodyString;
}
string BuildScoreBody(string gameName, string userName, double score)
{
    
    cJSON *bodyJSON = cJSON_CreateObject();
    cJSON *app42JSON = cJSON_CreateObject();
    cJSON *gameJSON = cJSON_CreateObject();
    cJSON *sJSON = cJSON_CreateObject();
    cJSON *scoreJSON = cJSON_CreateObject();
    
    cJSON_AddStringToObject(gameJSON, "name", gameName.c_str());
    cJSON_AddNumberToObject(scoreJSON, "value", score);
    cJSON_AddStringToObject(scoreJSON, "userName", userName.c_str());
    cJSON_AddItemReferenceToObject(sJSON, "score", scoreJSON);
    
    cJSON_AddItemReferenceToObject(app42JSON, "game", gameJSON);
    cJSON_AddItemReferenceToObject(gameJSON, "scores", sJSON);
    
    cJSON_AddItemReferenceToObject(bodyJSON, "app42", app42JSON);
    
    char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON);
    string bodyString = cptrFormatted;
    
    cJSON_Delete(gameJSON);
    cJSON_Delete(app42JSON);
    cJSON_Delete(bodyJSON);
    cJSON_Delete(sJSON);
    cJSON_Delete(scoreJSON);
    
    free(cptrFormatted);
    
    return bodyString;
    
}
string EmailService::buildcreateMailConfigurationBody(string emailHost, string emailPort,string emailId, string emailPassword, bool isSSL)
{
    
    cJSON *bodyJSON = cJSON_CreateObject();
    cJSON *app42JSON = cJSON_CreateObject();
    cJSON *emailJSON = cJSON_CreateObject();
    
    // first construct the user
    if (emailHost.length())
    {
        cJSON_AddStringToObject(emailJSON, "host", emailHost.c_str());
    }
    
    if (emailPort.length())
    {
        cJSON_AddStringToObject(emailJSON, "port", emailPort.c_str());
    }
    
    if (emailId.length())
    {
        cJSON_AddStringToObject(emailJSON, "emailId", emailId.c_str());
    }
    
    if (emailPassword.length())
    {
        cJSON_AddStringToObject(emailJSON, "password", emailPassword.c_str());
    }
    
    if(isSSL)
    {
        cJSON_AddStringToObject(emailJSON, "ssl", "true");
    }
    else
    {
       cJSON_AddStringToObject(emailJSON, "ssl", "false");
    }
    
    // add user to app42
    cJSON_AddItemReferenceToObject(app42JSON, "email", emailJSON);
    
    // add app42 to body
    cJSON_AddItemReferenceToObject(bodyJSON, "app42", app42JSON);
    
    char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON);
    string bodyString = cptrFormatted;
    
    cJSON_Delete(emailJSON);
    cJSON_Delete(app42JSON);
    cJSON_Delete(bodyJSON);
    
    free(cptrFormatted);
    
    return bodyString;
    
}
string EmailService::buildSendMailBody(string sendTo, string sendSubject, string sendMsg,string fromEmail, EmailMIME emailMIME)
{
    
    cJSON *bodyJSON = cJSON_CreateObject();
    cJSON *app42JSON = cJSON_CreateObject();
    cJSON *emailJSON = cJSON_CreateObject();
    
    // first construct the user
    if (sendTo.length())
    {
        cJSON_AddStringToObject(emailJSON, "to", sendTo.c_str());
    }
    
    if (sendSubject.length())
    {
        cJSON_AddStringToObject(emailJSON, "subject", sendSubject.c_str());
    }
    
    if (sendMsg.length())
    {
        cJSON_AddStringToObject(emailJSON, "msg", sendMsg.c_str());
    }
    
    if (fromEmail.length())
    {
        cJSON_AddStringToObject(emailJSON, "emailId", fromEmail.c_str());
    }
    
    cJSON_AddStringToObject(emailJSON, "mimeType", getEmailMIME(emailMIME));
    
    // add user to app42
    cJSON_AddItemReferenceToObject(app42JSON, "email", emailJSON);
    
    // add app42 to body
    cJSON_AddItemReferenceToObject(bodyJSON, "app42", app42JSON);
    
    char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON);
    string bodyString = cptrFormatted;
    
    cJSON_Delete(emailJSON);
    cJSON_Delete(app42JSON);
    cJSON_Delete(bodyJSON);
    
    free(cptrFormatted);
    
    return bodyString;
    
}
const char* App42Service::getJsonStringFromVector(const char* key,vector<string>userList)
{
    cJSON *bodyJSON = cJSON_CreateObject();
    cJSON *userJSON = cJSON_CreateArray();
    
    std::vector<string>::iterator it;
    
    for(it=userList.begin(); it!=userList.end(); ++it)
    {
        cJSON *jsonString = cJSON_CreateString(it->c_str());
        cJSON_AddItemToArray(userJSON, jsonString);
        //cJSON_Delete(jsonString);
    }
    cJSON_AddItemReferenceToObject(bodyJSON, key, userJSON);
    
    char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON);
    string bodyString = cptrFormatted;
    
    cJSON_Delete(userJSON);
    cJSON_Delete(bodyJSON);
    
    free(cptrFormatted);
    
    return bodyString.c_str();
    
}
	void Client::invokeRoomRPC(std::string roomId,std::string name, Arguments *args)
	{
		int byteLen;
		byte *req;
        
		std::string payload;
		cJSON *payloadJSON;
		payloadJSON = cJSON_CreateObject();
		cJSON_AddStringToObject(payloadJSON, "function", name.c_str());
		cJSON_AddItemReferenceToObject(payloadJSON, "args", args->get());
		cJSON_AddStringToObject(payloadJSON, "roomId", roomId.c_str());
		char *cRet =  cJSON_PrintUnformatted(payloadJSON);
		payload = cRet;
        
		req = buildWarpRequest(RequestType::room_rpc, payload, byteLen);
        
		char *data = new char[byteLen];
		for(int i=0; i< byteLen; ++i)
		{
			data[i] = req[i];
		}

		_socket->sockSend(data, byteLen);

		delete[] data;
		delete[] req;
		cJSON_Delete(payloadJSON);
		free(cRet);
	}
string BuildStorageBody(string json)
{
	cJSON *bodyJSON = cJSON_CreateObject();
	cJSON *app42JSON = cJSON_CreateObject();
	cJSON *storageJSON = cJSON_CreateObject();
    
	cJSON_AddStringToObject(storageJSON, "jsonDoc", json.c_str());
	cJSON_AddItemReferenceToObject(app42JSON, "storage", storageJSON);
    
	cJSON_AddItemReferenceToObject(bodyJSON, "app42", app42JSON);
	char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON);
	string bodyString = cptrFormatted;
    
	cJSON_Delete(storageJSON);
	cJSON_Delete(app42JSON);
	cJSON_Delete(bodyJSON);
	free(cptrFormatted);
	return bodyString;
}
string App42Service::getJsonStringFromApp42ACLList(string key,vector<App42ACL> &App42ACLObjectArray)
{
    cJSON *bodyJSON = cJSON_CreateObject();
    cJSON *jsonArray = cJSON_CreateArray();
    
    for (vector<App42ACL>::iterator it = App42ACLObjectArray.begin(); it!= App42ACLObjectArray.end(); it++)
    {
        cJSON *jsonObject = cJSON_CreateObject();
        cJSON_AddStringToObject(jsonObject, "user", it->getUserName().c_str());
        cJSON_AddStringToObject(jsonObject, "permission", it->getPermission().c_str());
        cJSON_AddItemToArray(jsonArray, jsonObject);
        //cJSON_Delete(jsonObject);
    }
    cJSON_AddItemReferenceToObject(bodyJSON, key.c_str(), jsonArray);

    char *cptrFormatted = cJSON_PrintUnformatted(bodyJSON);
    string jsonString = cptrFormatted;
    free(cptrFormatted);
    cJSON_Delete(jsonArray);
    cJSON_Delete(bodyJSON);
    return jsonString;
}