
/*---------------------------------------------------------------------------*/
/* whatsappclient.h                                                          */
/* copyright (c) innovaphone 2024                                            */
/*                                                                           */
/*---------------------------------------------------------------------------*/

typedef enum {
	WHATSAPP_ERR_OK = 0,
	WHATSAPP_ERR_MISSING_TOKEN,
	WHATSAPP_ERR_MISSING_VERSION,
	WHATSAPP_ERR_NOT_FOUND,
	WHATSAPP_ERR_BAD_REQUEST,
	WHATSAPP_ERR_AUTHENTICATION_FAILED,
	WHATSAPP_ERR_OTHER_HTTP,
	WHATSAPP_ERR_CLOSING
} whatsapp_error_t;

inline const char* WhatsAppErrorToStr(whatsapp_error_t error)
{
	const char* result = "<unkown reason id>";
	switch (error) {
	case WHATSAPP_ERR_OK: result = "WHATSAPP_ERR_OK"; break;
	case WHATSAPP_ERR_MISSING_TOKEN: result = "WHATSAPP_ERR_MISSING_TOKEN"; break;
	case WHATSAPP_ERR_MISSING_VERSION: result = "WHATSAPP_ERR_MISSING_VERSION"; break;
	case WHATSAPP_ERR_NOT_FOUND: result = "WHATSAPP_ERR_NOT_FOUND"; break;
	case WHATSAPP_ERR_BAD_REQUEST: result = "WHATSAPP_ERR_BAD_REQUEST"; break;
	case WHATSAPP_ERR_AUTHENTICATION_FAILED: result = "WHATSAPP_ERR_AUTHENTICATION_FAILED"; break;
	case WHATSAPP_ERR_OTHER_HTTP: result = "WHATSAPP_ERR_OTHER_HTTP"; break;
	case WHATSAPP_ERR_CLOSING: result = "WHATSAPP_ERR_CLOSING"; break;
	}
	return result;
}

class IWhatsAppClient {
public:
	static IWhatsAppClient * Create(class IIoMux * const iomux,
		class UWhatsAppClient * const user,
		class IWebserverPlugin * const webserver,
		class ISocketProvider * const tcpSocketProvider,
		class ISocketProvider * const tlsSocketProvider,
		class IInstanceLog * const log,
		class IDns * const dns = nullptr,
		class ISocketContext * const socketContext = nullptr);

	virtual ~IWhatsAppClient() {};
	virtual void Close() = 0;
	virtual void SetConfig(const char * token, const char * version, const char * webhookToken, const char * secret) = 0;
	virtual void SendMessage(json_io value, word base, const char* tel, const char* type, UWhatsAppClient* context, const char* channel, const char* callback_info = 0, const char* refersToID = 0) = 0;
	virtual void Subscribe(UWhatsAppClient * subscriber, const char * tel = 0) = 0;
	virtual void Unsubscribe(UWhatsAppClient * unsubscriber, const char * tel = 0) = 0;
	virtual void SetPhones(const char* phones) = 0;
	virtual char* GetPhonesConfig() = 0;
};

class UWhatsAppClient {
public:
	virtual ~UWhatsAppClient() {};

	virtual void WhatsAppClientCloseComplete(IWhatsAppClient * const whatsappClient) = 0;
	virtual void WhatsAppClientSendMessageResult(IWhatsAppClient * const whatsappClient, whatsapp_error_t error, const char * callback_info = 0, const char * content = 0) = 0;
	virtual void WhatsAppClientRecvMessage(IWhatsAppClient* const whatsappClient, json_io value, const char* tel, const char* channel, const char* type, const char* id, const char* refersToID = 0) = 0;
	virtual void WhatsAppClientForwardMessage(IWhatsAppClient* const whatsappClient, json_io value, const char* tel, const char* channel, const char* type, const char* id, const char* refersToID = 0) = 0;
};
