#ifndef FBRSAccess__H
#define FBRSAccess__H

#ifndef FBVersion
#define FBVersion 7
#elif FBVersion != 7
#error "The headers for FBCAccess version have been mixed"
#endif

/*!
@header FBRSAccess
@discussion The module provides an API for the FrontBase replication server, and provides functions for creation and removal of clients. Furthermore functions for client status reporting is provided.
 */
 
typedef struct FBRSAccess FBRSAccess;

#ifdef __cplusplus
extern "C" {
#endif

/*!
@function fbrsaCreate
 @discussion Create and initialize an FBRSAccess object to access the replication server on the named host replicating the named database.
 */
FBRSAccess* fbrsaCreate(const char* hostName, const char* databaseName);

/*!
@function fbrsaRelease
 @discussion Release the FBRSAccess object and all related resources.
 */
void fbrsaRelease(FBRSAccess* access);

/*!
@function fbrsaIsConnected
 @discussion Check the connection to the server.
 */
int fbrsaIsConnected(FBRSAccess* access);

/*!
@function fbrsaNeedsUpdate
 @discussion Poll the server for any changes to the status of clients.
 */
int fbrsaNeedsUpdate(FBRSAccess* access);

/*!
@function fbrsaIsReplicatorRunning
 @discussion Check if the replicator is running for the database.
 */
int fbrsaIsReplicatorRunning(const char* hostName, const char* databaseName);

/*!
@function fbrsaStartReplicator
 @discussion Start the replicator.  Returns NULL on ok, otherwise an error message which should be freed.
 */
char* fbrsaStartReplicator(const char* hostName, const char* options, const char* databaseName, const char* passwd);

/*!
@function fbrsaStopReplicator
 @discussion Stop the replicator.  Returns NULL on ok, otherwise an error message which should be freed.
 */
char* fbrsaStopReplicator(FBRSAccess* access);

/*!
@function fbrsaAddClient
 @discussion Add a new client. Clients are persistent, once created they will survive server restarts until removed.  Returns NULL on ok, otherwise an error message which should be freed.
 */
char* fbrsaAddClient(FBRSAccess* access, const char* hostname, const char* databaseName, const char* databasePassword, const char* password);

/*!
@function fbrsaCreateClient
 @discussion Create a new client. Clients are persistent, once created they will survive server restarts until removed.  Returns NULL on ok, otherwise an error message which should be freed.
 */
char* fbrsaCreateClient(FBRSAccess* access, const char* hostname, const char* databaseName, const char* databasePassword, const char* password);

/*!
@function fbrsaRemoveClient
 @discussion Remove a client. Returns NULL on ok, otherwise an error message which should be freed.
 */
char* fbrsaRemoveClient(FBRSAccess* access, const char* hostname, const char* databaseName);

typedef struct FBRSAStatus FBRSAStatus;
   // The FBRSAStatus object provides a handle for obtaining the status for a server. 

/*!
@function fbrsaStatus
 @discussion Obtain the status for the clients.
 */
FBRSAStatus* fbrsaStatus(FBRSAccess* access);

/*!
@function fbrsaRetainStatus
 @discussion Increment the retain count of the given FBRSAStatus instance.
 */
FBRSAStatus* fbrsaRetainStatus(FBRSAStatus* access);

/*!
@function fbrsaReleaseStatus
 @discussion Decrement the retain count of the given FBRSAStatus instance.
 */
void fbrsaReleaseStatus(FBRSAStatus* access);

/*!
@function fbrsaMinVersion
 @discussion The minimum version number available from the master database.
 */
unsigned long long fbrsaMinVersion ( FBRSAStatus* status );

/*!
@function fbrsaMaxVersion
 @discussion The maximum version number available from the master database.
 */
unsigned long long  fbrsaMaxVersion ( FBRSAStatus* status );

/*!
@function fbrsaDiscontinued
 @discussion Has the master transaction log been discontinued?
 */
int fbrsaDiscontinued ( FBRSAStatus* status );

/*!
@function fbrsaReaderStatus
 @discussion Error messages associated with transaction log reading?
 */
char* fbrsaReaderStatus ( FBRSAStatus* status );

/*!
@function fbrsaHostName
 @discussion The hostName of the server host.
 */
char* fbrsaHostName(FBRSAStatus* status);

/*!
@function fbrsaDatabaseName
 @discussion The databaseName of the master database.
 */
char* fbrsaDatabaseName(FBRSAStatus* status);

/*!
@function fbrsaClientCount
 @discussion The number of clients.
 */
unsigned int fbrsaClientCount(FBRSAStatus* status);

/*!
@function fbrsaClientHostName
 @discussion Database name of the client at the given index.
 */
char* fbrsaClientHostName(FBRSAStatus* status, unsigned int client);

/*!
@function fbrsaClientDatabaseName
 @discussion Database name of the client at the given index.
 */
char* fbrsaClientDatabaseName(FBRSAStatus* status, unsigned int client);

/*!
@function fbrsaClientCurrentVersion
 @discussion Current version of the client at the given index.
 */
unsigned long long fbrsaClientCurrentVersion(FBRSAStatus* status, unsigned int client);

/*!
@function fbrsaClientStatus
 @discussion Status of the client at the given index.
 */
char* fbrsaClientStatus(FBRSAStatus* status, unsigned int client);

#ifdef __cplusplus
}
#endif

#endif /* FBRSAccess__H */
