#ifndef CLIENT_HPP
#define CLIENT_HPP

#include <string>
#include "Server.hpp"
#include "RequestFactory.hpp"

namespace mole {

  namespace ctx {
    
    /**
     * A client is bind to a server. This class allows to easily dialog
     * with a server.
     */
    class Client {
      
    public :
      /**
       * Constructs a client, and bind it to the server in parameter.
       * No connection is done at construction
       *
       * @param server The server
       */
      Client(Server * server);
      
      Client();

      ~Client();

      /**
       * Gets the server binded to the client.
       * @return The server
       */
      Server * getServer();


      /**
       * Gets a service from the server.
       *
       * @param service Service to get.
       */
      mole::serv::Service * getService(std::string);
      
      /**
       * Initialize the client and clear all connections.
       *
       * @return True if init was success, false else.
       */
      bool init();

      /**
       * Links client to a server.
       *
       * @param server Server wanted by the client.
       * @param co True if you want to connect immediately to the server, else false.
       * @return True if connected, false else.
       */
      bool bind(Server * server, bool co);
      
      /**
       * Connect the client to the server. If client is connected,
       * <b>init</b> is called before any reconnection.
       *
       * @return True if connected, false else.
       */
      bool connect();
      
      /**
       * Disconnect all existing connections between client and server.
       */
      void disconnect();
      
      /**
       * Execute a service, using this client.
       *
       * @param service Service to execute.
       */
      bool execute(mole::serv::Service * service);

      mole::ctx::Request * createRequest(std::string name);

      void addRequest(std::string name, mole::ctx::Request * req);

      void setId(std::string _id) { id = _id; };

      std::string getId() { return id; };
      
    protected :
      /** Flag about connection status. True <==> Connected. */
      bool connected;
      
      /** Server where the client is connected to */
      Server * server;
      
      /** A client use a request factory to facilitate services utilisation */
      RequestFactory * requestFactory;

      /** The client may use an id to be identified by the server. */
      std::string id;
     
    }; // end of class

  }; // end of serv

}; // end of mole

#endif
