/////////////////////////////////////////////////////////////////////////////
// Name:        No names yet.
// Purpose:     Contrib. demo
// Author:      Aleksandras Gluchovas
// Modified by:
// Created:     2001/03/28
// RCS-ID:      $Id: indexing_svc.h,v 1.3 2001/04/16 15:36:50 aleksandras Exp $
// Copyright:   (c) Aleksandras Gluchovas (alex@soften.ktu.lt)
// Licence:   	GPL (version 2.0 or later)
/////////////////////////////////////////////////////////////////////////////

#ifndef __INDEXING_SVC_G__
#define __INDEXING_SVC_G__

#include "wx/pmf/pm.h"
#include "wx/list.h"
#include "wx/dialog.h"			
#include "wx/dynarray.h"
#include "wx/datetime.h"

class wxPMIndexingService;
class DbTableBase;
class IndexTable;
class FileTable;
class ItemTable;

class I_IndexHandler
{
public:
	virtual const char* GetIndexHandlerName() = 0;

	virtual bool OnClearIndexItems( int indexId ) = 0;
	virtual bool OnBeginBuild( int indexId ) = 0;
	virtual bool OnIndexFile( const wxPMFileInfo& file, int fileId, int indexId ) = 0;
	virtual bool OnEndBuild( int indexId ) = 0;
};

WX_DEFINE_ARRAY( I_IndexHandler*, IndexHandlerListT );

class wxPMIndexingService : public wxPMSimpleService
{
public:
	wxPMIndexingService();
	~wxPMIndexingService();

	enum COMMAND_IDS
	{
		ID_INDECIES,
		ID_REBUILD_ALL_INDECIES,
		ID_CREATE_TEST_DATA
	};

	// overriden methods of wxPMService

	virtual bool Start();
	virtual bool Stop();

	virtual wxPMMergableMenuChain* GetMergableMenuChain();

	IndexTable& GetIndexTable();
	FileTable& GetFileTable();
	ItemTable& GetItemTable();

	void FlushAll();

	virtual void* GetInterface( const char* infName, const char* version = NULL );

	// command handlers

	void OnIndecies( wxCommandEvent& event );
	void OnRebuildAllIndecies( wxCommandEvent& event );
	void OnCreateTestData( wxCommandEvent& event );

	// other methods

	virtual bool RegisterIndexHandler( I_IndexHandler* handler );
	virtual bool UnregisterIndexHandler( I_IndexHandler* handler );
	IndexHandlerListT& GetIndexHandlers();

	bool RebuildIndex();

	void TestIt();
	void DropAllData();
	void CreateTestData();

	DECLARE_EVENT_TABLE()
	DECLARE_DYNAMIC_CLASS( wxPMIndexingService )

protected:
	void CleanUp();
	void BuildIndexForFile( const wxString& file, I_IndexHandler* handler );
	void BuildIndexRecursively( const wxString& dir, I_IndexHandler* handler, 
								const wxString& fileMask );

	void GetDirEntries( wxStringList& toLst, const wxString& mask, int flags );

protected:
	IndexTable* mpIndexTbl;
	FileTable*  mpFileTbl;
	ItemTable*  mpItemTbl;
	int	mFilesProcessed;

	IndexHandlerListT mHandlers;
};

class IndeciesDlg : public wxDialog
{
public:
	IndeciesDlg();

	void Setup( wxPMIndexingService* svc );

	void OnNew( wxCommandEvent& event );
	void OnEdit( wxCommandEvent& event );
	void OnRebuild( wxCommandEvent& event );
	void OnRebuildAll( wxCommandEvent& event );
	void OnRemove( wxCommandEvent& event );

	void OnAddSingle( wxCommandEvent& event );
	void OnAddAll( wxCommandEvent& event );
	void OnRemoveSingle( wxCommandEvent& event );
	void OnRemoveAll( wxCommandEvent& event );

	void OnInitDialog( wxInitDialogEvent& event );

	DECLARE_EVENT_TABLE()
	DECLARE_DYNAMIC_CLASS( IndeciesDlg )
	
protected:
	wxPMIndexingService* mpIndexSvc;
};

class IndexPropsDlg : public wxDialog
{
public:
	IndexPropsDlg();

	void Setup( wxPMIndexingService* svc );

	void OnBrowse( wxCommandEvent& event );
	void OnDel( wxCommandEvent& event );
	void OnAdd( wxCommandEvent& event );

	void OnInitDialog( wxInitDialogEvent& event );

	DECLARE_EVENT_TABLE()
	DECLARE_DYNAMIC_CLASS( IndexPropsDlg )
	
protected:
	wxPMIndexingService* mpIndexSvc;
};

class AddFilesDlg : public wxDialog
{
public:
	AddFilesDlg();

	void Setup( wxPMIndexingService* svc );

	void OnSelect( wxCommandEvent& event );

	void OnInitDialog( wxInitDialogEvent& event );

	DECLARE_EVENT_TABLE()
	DECLARE_DYNAMIC_CLASS( AddFilesDlg )
	
protected:
	wxPMIndexingService* mpIndexSvc;
};

class diaRel;

class DbTableBase : public wxObject
{
public:
	DbTableBase();
	virtual ~DbTableBase();

	virtual bool Begin( int indexId = -1 );
	virtual bool End();
	virtual bool First();
	virtual bool Next();
	virtual bool Ok();
	virtual bool Add();
	virtual bool Delete();
	virtual bool Seek();

	virtual bool Extract();
	virtual bool Write();

	virtual bool RemoveAll();
	virtual bool Flush();

	virtual void Setup( diaRel* rel, wxPMIndexingService* svc );

	virtual void SetId( int id ) = 0;

protected:
	diaRel* mpRel;
	wxPMIndexingService* mpSvc;
};

class IndexTable : public DbTableBase
{
public:
	const char* GetName();
	const char* GetLanguage();
	const char* GetHandlerName();
	const char* GetBoundProject();
	const char* GetContentRoot();
	const char* GetPaths();
	int GetId();

	void SetName( const wxString& name );
	void SetLanguage( const wxString& lang );
	void SetHandlerName( const wxString& handler );
	void SetBoundProject( const wxString& project );
	void SetContentRoot( const wxString& root );
	void SetPaths( const wxString& paths );

	virtual void SetId( int id );
};

class FileTable : public DbTableBase
{
public:
	const char* GetName();
	wxDateTime GetLastIndexingDate();
	int GetId();
	int GetIndexId();

	void SetName( const wxString& name );
	void SetLastIndexingDate( const wxDateTime& date );
	void SetIndexId( int indexId );

	void DeleteItemsByIndex( int indexId );

	bool FindById( int fileId );

	virtual void SetId( int id );
};

class ItemTable : public DbTableBase
{
public:
	enum INDEXES
	{
		BY_NAME,
		BY_INDEX,
		BY_FILE
	};

	virtual bool Begin( int indexId = -1 );

	const char* GetName();
	const char* GetContext();
	wxString GetDataStr();
	int GetLineNo();
	int GetLineCount();
	int GetType();

	int GetId();
	int GetFileId();
	int GetIndexId();

	void SetName( const wxString& name );
	void SetContext( const wxString& context );
	void SetDateStr( const wxString& dataStr );
	void SetLineNo( int lineNo );
	void SetLineCount( int count );
	void SetType( int type );
	void SetFileId( int fileId );
	void SetIndexId( int indexId );
	
	virtual void SetId( int id );

	bool FindByName( const wxString& _name, const wxString& _context, int _type, 
					 bool matchSubstring = TRUE );

	bool FindByType( int _type );

	void DeleteItemsByType( int type, int indexId );
};

#endif // __INDEXING_SVC_G__