#include <iostream>

#include <stdio.h>
#include <axiom.h>
#include <axis2_util.h>
#include <axiom_soap.h>
#include <axis2_client.h>

#include <rampart_constants.h>
#include <neethi_util.h>
#include <neethi_policy.h>

//#include "axis2_stub_DataService.h"
//#include "adb_EndpointReferenceType.h"

int main(int argc, char** argv)
{
    const axutil_env_t *axisEnv = NULL;
    axis2_char_t *address = NULL;
    axis2_char_t *clientHome = NULL;

    // Setup the environment with a trace level log
	axisEnv = axutil_env_create_all( "debug.log", AXIS2_LOG_LEVEL_TRACE );
	if( axisEnv == NULL ) { std::cout << "axisEnv is null" << std::endl; }

    // Initialize EPR address
    //address = "https://fiuza.it-innovation.soton.ac.uk:8443/gria-basic-app-services/services/DataService";
	address = "http://fiuza.it-innovation.soton.ac.uk:9090/gria-basic-app-services/services/DataService";

    // Set clientHome to the location where the axis2.xml/policy.xml and modules are stored 
    clientHome = "C:/wsfc_gria";

	/*
	axis2_stub* stub = axis2_stub_DataService_create( axisEnv, clientHome, address );
	if( stub == NULL ) { std::cout << "stub is null" << std::endl; }

	axis2_char_t *fileName = axutil_stracat(axisEnv, clientHome, AXIS2_PATH_SEP_STR);
	fileName = axutil_stracat(axisEnv, fileName, "policy.xml" );

	neethi_policy_t* policy = neethi_util_create_policy_from_file(axisEnv, fileName);
	if( policy == NULL ) { std::cout << "policy is null" << std::endl; }

	axis2_svc_client_t* client = axis2_stub_get_svc_client( stub, axisEnv );

	axis2_svc_client_set_policy( client, axisEnv, policy );
	
	axiom_node_t* node = axiom_node_create( axisEnv );
	// If I don't run this line, then the getResources in the stub fails
	axis2_svc_client_send_receive( client, axisEnv, node );

	adb_getResources_t* r = adb_getResources_create( axisEnv );

	// This line only works if a message is sent with axis2_svc_client_send_recieve before it is run
	// is run before it, otherwise nothing will get signed -- I believe this bug is known however,
	// and is a problem with the WSDL2C, it sounds like the problem has been found and should be resolved
	// soon -- http://www.nabble.com/rampart-c-not-working-with-stubs-generated-using-wsdl2c-tf4125131.html
	adb_getResourcesResponse* rr = axis2_stub_DataService_getResources( stub, axisEnv, r );

	if( stub ) {
		axis2_stub_free( stub, axisEnv );
		stub = NULL;
	}
	*/

	axis2_endpoint_ref_t* endpointRef = axis2_endpoint_ref_create( axisEnv, address );

	axis2_options_t* options = axis2_options_create( axisEnv );
	axis2_options_set_to( options, axisEnv, endpointRef );
	axis2_options_set_action( options, axisEnv, "" );

	axis2_svc_client_t* client = axis2_svc_client_create( axisEnv, clientHome );
    axis2_svc_client_set_options( client, axisEnv, options);

	axis2_char_t *fileName = axutil_stracat(axisEnv, clientHome, AXIS2_PATH_SEP_STR);
	fileName = axutil_stracat(axisEnv, fileName, "policy.xml" );

    neethi_policy_t* policy = neethi_util_create_policy_from_file(axisEnv, fileName);
	if( policy == NULL ) { std::cout << "policy is null" << std::endl; }

	axis2_svc_client_set_policy( client, axisEnv, policy );

	axiom_node_t* node = axiom_node_create( axisEnv );
	axis2_svc_client_send_receive( client, axisEnv, node );

	// Free axisEnv
    if( axisEnv ) {
        axutil_env_free( (axutil_env_t *) axisEnv );
        axisEnv = NULL;
    }

    return 0;
}
