/* 
ZLib license:
Copyright (c) 2011 Dirk de la Hunt

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef __MONKEYBOT_H_
#define __MONKEYBOT_H_

#ifndef _WIN32
	#include <sys/time.h>
#endif
#include "chatengine.h"
#include "utils.h"
#include "monkeyoptions.h"
#include "monkeydb.h"

typedef enum Action
{
	/* these actions have their contents stored */
	Action_Store = 0,
	Action_Download,
	Action_Todo,
	Action_Idea,
	Action_Buy,
	Action_Remind,

	/* these do not */
	Action_Delete,
	Action_Search,
	Action_SearchUrls,
	Action_Show,
	Action_ChangeDB,

	Action_Count,

	Action_Invalid = 65536
}
Action;

#ifndef __DEBUG
class MonkeyBot : public ChatEngine
#else
class MonkeyBot
#endif
{
protected:
	time_t          handshakeTime;
	char           *previousMessage;
	char            previousLength;
	int             previousId;
	bool            askedForAction;

	OSMutex         mutex;
	MonkeyOptions  *options;
	MonkeyDatabase  db;

protected:
	static THREADPROC(downloadThread);
	static int searchCallback(void *Param, int argc, char *argv[]);
	static int showCallback(void *Param, int argc, char *argv[]);

	int doDb(const char *sqlString, const char *successPhrase, const char *failPhrase, bool showId = false);
	int storeMessage(const char *Message);
	void updateMessage(int MessageId, const char *Action);
	void deleteMessage(int MessageId);
	void handleNoAction();
	void storeUrl(const char *Url, int MessageId);
	void searchFor(const char *Message, bool OnlyDisplayUrls = false);
	void showMessage(const char *Action);
	void processUrls(const char *Message, int MessageId, int ActionId);
//gloox overrides:
#ifndef __DEBUG
	void handleMessage( const Message& msg, MessageSession* session = 0 );
#endif

public:
	MonkeyBot();
	~MonkeyBot();

	bool Create(MonkeyOptions *Options); /* takes ownership of Options */
	void Run(); /* blocks until told to quit over IM */
	bool Destroy();

	void SendText(const char *Message);
	bool Process(const char *Message);

#ifdef __DEBUG
	void start();
#endif
};


#endif
