#ifndef __Socket_h__
#define __Socket_h__

#include "includes"
#include "Object.h"
#include "ZString.h"

namespace core {

   class EPoll;

   ///
   /// Socket
   ///
   /// The core component to managing a socket.
   ///
   /// Hooks into the EPoll through the registration and unregistration process and provides a
   /// communication socket of the specified protocol type. This object provides for all receiving
   /// data threading through use of the EPoll object and also provides buffering for output data
   /// requests to the socket.
   ///
   /// A program using a socket object can request to open a socket (network or device) and
   /// communicate through the streambuffer interface of the socket object.
   ///
   /// The socket side of the Socket accepts EPOLLIN event and will maintain the data in a buffer
   /// for the stream readers to read. A onDataReceived event is then sent with the data received in
   /// the buffer that can be read through the stream. Only sockets that send events to epoll can be
   /// used with this object.
   ///
   /// When writing to the stream the data is written into a buffer and a EPOLLOUT is scheduled. Upon
   /// receiving the EPOLLOUT event then the buffer is written to the socket output.
   ///

   class Socket {

    public:

      ///
      /// Constructor
      ///
      /// @param ePoll The EPoll socket descriptor.
      /// @param text A title for this socket.
      ///

      Socket(EPoll &ePoll, std::string text = "");

      ///
      /// Destructor
      ///

      virtual ~Socket();

      ///
      /// Use the shutdown() method to terminate the socket connection and remove resources.
      /// This method is provided to ensure that all destructors are called for all inherited
      /// objects with a virtual destructor.
      ///

      void shutdown(std::string text = "unknown");

      ///
      /// setDescriptor establishes the file descriptor for the socket and registers the socket
      /// on the EPoll controller. setDescriptor will invoke the onRegister() event.
      ///

      void setDescriptor(int descriptor); ///<Set the descriptor for the socket.

      int getDescriptor(); ///< Get the descriptor for the socket.

      ///
      /// The event received from epoll is sent through the eventReceived
      /// method which will parse the event and call the read and write
      /// callbacks on the socket.
      ///
      /// This method is called by the BMAEPoll object and should not be called
      /// from any user extended classes unless an epoll event is being
      /// simulated.
      ///
      /// The return value of false will delete the socket object causing the destructors to run.
      /// The return value of true will enable the socket on ePoll to receive more events.
      ///

      bool eventReceived(struct epoll_event event, long long eventId); ///< Parse epoll event and call specified callbacks.

      ///
      /// Write data to the socket.
      ///

      int write(std::string data);
      void write(char *buffer, int length);

      void output(std::stringstream &out);

      ///
      /// The onRegister method is called before the socket is registered with
      /// ePoll so objects extending the Socket definition can initialize the socket
      /// before receiving events. Evoked when the
      /// descriptor is set using setDescriptor for the socket.
      ///

      virtual void onRegister(); ///< Called before the socket has registered with the epoll processing.
      virtual void onRegistered(); ///< Called after the socket has been registered with epoll processing.

      virtual void onUnregister();

      ///
      /// ePoll and socket communcation events will be stopped. The default method will
      /// close the socket and clean up the connection. If this is overridden by an
      /// extended object then the object should call this method to clean the socket up.
      ///

      virtual void onUnregistered(); ///< Called when the socket has finished unregistering for the epoll processing.

      bool needsToWrite();

      bool reset = false;

      volatile bool shutDown = false;

      void enableSocket();
      void disableSocket();

    protected:

      EPoll &ePoll;           // The EPoll control object.

      ///
      /// The onConnected method is called when the socket is ready to communicate. 
      /// Writing to the socket can begin on this call to initiate a contact with the
      /// remote device.
      ///

//      virtual void onConnected(); ///< Called when socket is open and ready to communicate.

      ///
      ///
      ///

//   virtual void onDisconnected(); ///< Called when socket is closing and no longer ready to communicate.

      ///
      /// This method is called when data is ready to be read from the socket.
      ///

      virtual void readDataNotification();

    private:

      std::string text;
      int descriptor = -1;
      std::mutex outlock;
      bool readHangup = false;
      volatile bool inHandler = false;

      //-------------------------------------------------------------------------------------
      // the writeSocket is called when epoll has received a write request for a socket.
      // Writing data to this socket is queued in the streambuf and permission is requested
      // to write to the socket. This routine handles the writing of the streambuf data
      // buffer to the socket.
      //-------------------------------------------------------------------------------------

      void writeSocket();

 //  int_type underflow();
//   int_type uflow();
//   int_type pbackfail(int_type ch);
//   streamsize showmanyc();

      std::queue<std::string> fifo;

      void resetSocket();

      std::mutex lock;

   };

}

#endif

