/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.

** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * Author : Jerzy Kaczorowski <kaczoroj@hotmail.com> (Originally: Sebastien Abras <sabdev@ibelgique.com>) --- July 2002
 */

#if !defined(AFX_ITEMLISTDLG_H__E6D16D27_1A84_4E8A_A926_3B76728E0F73__INCLUDED_)
#define AFX_ITEMLISTDLG_H__E6D16D27_1A84_4E8A_A926_3B76728E0F73__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ItemListDlg.h : header file
//

#include "MultiFiles.h"
#include "AppGlue.h"
#include "SmartComboBox.h"
#ifdef WIN32
#include "SortHeaderCtrl.h"
#endif

#include <string>
#include <sstream>
#include <set>
#include <list>
#include <map>
#include <algorithm>

using namespace std;

bool CompatGetTagListItem(const MultiFiles* mf, CPStr& tagName);
bool CompatGetModuleListItem(const MultiFiles* mf, CPStr& moduleName);

// persistent default values for the dates, tags...
extern CMString gRevNames;
extern CMString gDateNames;

/// Enum types of list dialog
typedef enum
{
	kItemListTag,	/*!< List branches/tags */
	kItemListModule	/*!< List modules/directories&files*/
} kItemListDlg;

/// Item's description container
typedef std::set<string> ItemDescription;

/// Structure to hold item's row information
typedef struct
{
	ItemDescription m_description;		/*!< Item's description */
	ItemDescription m_descriptionEx;	/*!< Item's extended description */
} ItemRowInfo;

/// Item's rows container
typedef std::map<string, ItemRowInfo> ItemList;

/// Path to the list location
typedef std::list<string> ListPath;


/// Base class for item list console
class CItemListConsole : public CCvsConsole
{
public:
	CItemListConsole();
	virtual ~CItemListConsole();

	virtual long cvs_out(char* txt, long len);
	virtual long cvs_err(char* txt, long len);

	virtual void Parse() = 0L;

protected:
	ItemList m_itemList;		/*!< Parsing result */
	stringstream m_cvsResult;	/*!< Cvs command result used for parsing */

public:
	const ItemList& GetItemList() const;
};

///	Class to parse tags/branches (obtained using <b>cvs log</b> or <b>cvs rlog</b>)
class CTagParserConsole : public CItemListConsole
{
public:
	CTagParserConsole();
	virtual ~CTagParserConsole();

	virtual void Parse();
};

/// Class to parse modules (obtained using <b>cvs co -c</b>)
class CModuleParserConsole : public CItemListConsole
{
public:
	CModuleParserConsole();
	virtual ~CModuleParserConsole();

	virtual void Parse();
};

/// Class to parse directories/files (obtained using <b>cvs ls</b>)
class CListParserConsole : public CItemListConsole
{
public:
	CListParserConsole();
	virtual ~CListParserConsole();

	virtual void Parse();
};


#ifdef WIN32

/// Class to hold and support data display (e.g. sorting or storing the result for next display)
class ItemDisplayInfo
{
private:
	// Data members
	string m_item;			/*!< Item text */
	string m_description;	/*!< Description text */
	string m_descriptionEx;	/*!< Extended description text */

	ItemRowInfo m_rowInfo;	/*!< Original row data after parsing */

public:
	// Interface
	void Create(const string& item, const ItemRowInfo& rowInfo);

	const string& GetItem() const;
	const string& GetDescription() const;
	const string& GetDescriptionEx() const;

	const ItemRowInfo& GetRowInfo() const;
};

/////////////////////////////////////////////////////////////////////////////
// CItemListDlg dialog

class CItemListDlg : public CDialog
{
// Construction
public:
	CItemListDlg(const MultiFiles* mf,
		const char* item, kItemListDlg type, ItemList& itemList, const ListPath* listPath,
		bool forceCvsroot, const char* rev, const char* date,
		CWnd* pParent = NULL);   // standard constructor

// Dialog Data
	//{{AFX_DATA(CItemListDlg)
	enum { IDD = IDD_ITEM_LIST };
	CButton	m_hasDateCheck;
	CButton	m_hasRevCheck;
	CButton	m_forceCvsrootCheck;
	CSmartComboBox	m_dateCombo;
	CSmartComboBox	m_revCombo;
	CButton	m_optionCheck;
	CListCtrl	m_itemListCtrl;
	CSmartComboBox	m_readFromCombo;
	CString	m_readFrom;
	CString	m_item;
	BOOL	m_option;
	BOOL	m_forceCvsroot;
	BOOL	m_hasRev;
	CString	m_rev;
	CString	m_date;
	BOOL	m_hasDate;
	//}}AFX_DATA


// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CItemListDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:

	// Generated message map functions
	//{{AFX_MSG(CItemListDlg)
	afx_msg void OnRefresh();
	virtual BOOL OnInitDialog();
	virtual void OnCancel();
	afx_msg void OnOption();
	afx_msg void OnCheckrev();
	afx_msg void OnCheckdate();
	afx_msg void OnBrowseTag();
	virtual void OnOK();
	afx_msg void OnDblclkItemlist(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnItemchangedItemlist(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnColumnclickItemlist(NMHDR* pNMHDR, LRESULT* pResult);
	afx_msg void OnDestroy();
	afx_msg void OnForceroot();
	afx_msg void OnBrowseReadfrom();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
public:
	ListPath m_listPath;	/*!< Path to the current list location */

private:
	const MultiFiles* m_mf;
	
	kItemListDlg m_type;	/*!< Dialog type */
	ItemList& m_itemList;	/*!< Item's container */

	void InitListCtrl(BOOL readingData = FALSE);
	void InitOptionCtrls();
	void EnableControls(BOOL enable);
	bool EnableDateRev();

	bool CheckSettings();

	// Items management

	/// Images enum
	enum
	{
		kItemImgBlank,				/*!< Blank */
		kItemImgUpDir,				/*!< Go one directory up */
		kItemImgDir,				/*!< Directory */
		kItemImgFile,				/*!< File */
		kItemImgModule,				/*!< Module */
		kItemImgRevision,			/*!< Revision */
		kItemImgBranch,				/*!< Branch */
		kItemImgBranchAndRevision,	/*!< Branch and revision */
	};

	CImageList m_ItemsImageList;

	bool SelectItem(const char* item);
	void DisplayItems(const ItemList& itemList, bool showUpDir = true);
	int GetImageForItem(kItemListDlg type, bool option, const ItemDisplayInfo* displayInfo);
	void DeleteAllItems();

	// Sorting

	/// Structure to hold the sorting settings
	typedef struct
	{
		int m_column;			/*!< Sorting column */
		bool m_asc;				/*!< Sort ascending or descending */
		kItemListDlg m_type;	/*!< Dialog type */
		bool m_option;			/*!< Option state */
	} CompareInfo;

	bool m_sortAsc;		/*!< Current ascending/descending sorting */
	int m_sortColumn;	/*!< Current sort column */
	CSortHeaderCtrl m_headerCtrl;

	void SortItems(int column, bool asc, kItemListDlg type, bool option);
	static int CALLBACK CompareItems(LPARAM itemData1, LPARAM itemData2, LPARAM lParam);
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_ITEMLISTDLG_H__E6D16D27_1A84_4E8A_A926_3B76728E0F73__INCLUDED_)

#endif /* WIN32 */
