#ifndef __SendMail2_h__
#define __SendMail2_h__

#include "EPoll.h"
#include "TCPSession2.h"
#include "MString.h"
#include "Timer.h"
#include <mutex>

#define CRLF "\r\n"
#ifndef ___state_defined___
#define ___state_defined___
typedef enum {CONNECT, READY, READYX, MAIL, RCPT, DATA, SENT} State;
#endif

namespace mail {

   class SendMail2 : public core::TCPSession2, private core::Timer {

    public:
      SendMail2(core::EPoll &ePoll, coreutils::MString from, coreutils::MString recipient, coreutils::MString message);
      SendMail2(const SendMail2 &copy);
      ~SendMail2();

      void protocol(coreutils::ZString &data) override;

      int send();
      coreutils::MString wait();
      coreutils::MString returnValue;

    private:
      std::vector<coreutils::MString>& extractRecipientList();
      void onTimeout() override;

      core::EPoll &ePoll;

      coreutils::MString from;
      coreutils::MString recipient;
      coreutils::MString buffer;
      coreutils::MString message;
      std::vector<coreutils::MString> recipientList;

      State state = CONNECT;
      State stateOnTimeout = CONNECT;

      std::mutex waiter;
   };

}

#endif
