///////////////////////////////////////////////////////////////////////////////
//
// File   : Tools_UI_Overrides.h
// Author : Andy Southgate
// Date   : 09-Dec-2008
//
// Copyright (C) 2008 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

#ifdef ASURA_USE_PRAGMA_ONCE
#pragma once
#endif   // ifdef ASURA_USE_PRAGMA_ONCE

#ifndef __TOOLS_UI_OVERRIDES_H__
#define __TOOLS_UI_OVERRIDES_H__

#include <Interface/Asura_Table_Dialog.h>

class Asura_Overlay;
class Tools_UI_Overrides_FilterSet;

class Tools_UI_Overrides_Interface
{
public:
	virtual ~Tools_UI_Overrides_Interface	() {};
	virtual bool ActivateOverrideMap		(Asura_Hash_ID uOverrideMapHash ) = 0;
	virtual bool CreateNewOverrideMap		(const Asura_String &xOverrideMapName) = 0;
	virtual bool CopyOverrideMap			(Asura_Hash_ID uFrom, const Asura_String &xTo) = 0;
	virtual bool DeactivateOverrideMap		(Asura_Hash_ID uOverrideMapHash ) = 0;
	virtual bool DeleteOverrideMap			(Asura_Hash_ID uOverrideMapHash ) = 0;
	virtual bool RenameOverrideMap			(Asura_Hash_ID uFrom, const Asura_String &xTo ) = 0;

	virtual bool IsOverrideMapActive		(Asura_Hash_ID uOverrideMapHash) = 0;
	virtual bool IsItemOverriden			(Asura_Hash_ID uItemHash) = 0;

	virtual bool DoesOverrideMapExist		(Asura_Hash_ID uOverrideMapHash) = 0;

	virtual bool GetEntryNames				(Asura_String* &pxNameArray, int &iNumEntries) = 0;
	virtual bool GetOverrideNames			(Asura_String* &pxOverrideArray, int &iNumOverrides) = 0;
	virtual Asura_Hash_ID GetCurrentOverrideMap() = 0;

	virtual bool GetOverrideMapEntry		(Asura_Hash_ID uOverrideMapHash, Asura_Hash_ID uEntryHash, Asura_Hash_ID &uOverriddenEntryHash) = 0;
	virtual bool SetOverrideMapEntry		(Asura_Hash_ID uOverrideMapHash, const Asura_String &xFrom, const Asura_String &xTo) = 0;
	virtual bool RemoveOverrideMapEntry		(Asura_Hash_ID uOverrideMapHash, Asura_Hash_ID uEntryHash) = 0;

	virtual void RefreshAll					() = 0;
//	virtual void RefreshCell				(Asura_Hash_ID uOverrideMapHash, Asura_Hash_ID uEntryHash) = 0;

};


class Tools_UI_Overrides
{
	class TableCell_OverrideHash;
	class TableCell_OverrideHash_EntryDialog;
	friend class TableCell_OverrideHash;
private:
	Tools_UI_Overrides_Interface&		m_xInterface;


	Asura_String*						m_pxNameArray;					// Array of names.
	Asura_String*						m_pxHeadingArray;				// Array of headings.
	Asura_Hash_ID*						m_puHashArray;					// Array of material hash-IDs.
	int									m_iNumEntries;					// The number of entries present.
	Asura_String*						m_pxOverrideNameArray;			// Array of override names.
	Asura_String*						m_pxOverrideHeadingArray;		// Array of headings for the overrides.
	Asura_Hash_ID*						m_puOverrideHashArray;			// Array of override hash-IDs.
	Tools_UI_Overrides_FilterSet*		m_pxOverridesFilters;


	bool*								m_pbEnableTableRows;			
	int									m_iNumOverrides;				// The number of entries present.

	bool								m_bOnlyItemsWithAnOverride;		// Show only entries with an override
	bool								m_bLastOnlyShowItemsWithAnOverride;	// Show only entries with an override

	Asura_Table_Dialog*					m_pxTableDialog;					// A pointer to the current table dialog.

	Asura_StringDisplay_Dialog*			m_pxNoOverridesDialog;
	Asura_Dialog*						m_pxSelectCurrentOverrideDialog;


	void ApplyFilters( void );
	static Asura_TableCell_Dialog* CreateCellDialog( Asura_Table_Dialog& xTableDialog, const Asura_Vector_2& xOffset, const Asura_Vector_2& xMinCellSize, const u_int uRow, const u_int uColumn, void* const pxUserData );

	static void CreateHashArray(const Asura_String* pszNameArray, Asura_Hash_ID* &pxHashArray, int iNum);
	static void CreateHeadingArray(const Asura_String* pszNameArray, Asura_String* &pxHeadingArray, int iNum);

	Asura_String GetOverrideName();

	static void FiltersChanged( void* const pxUserData );


protected:
	static void OnlyShowItemsWithAnOverrideClicked	( int iUserData ) { return ( ( Tools_UI_Overrides* ) iUserData )->OnlyShowItemsWithAnOverrideClicked(); }
	static bool AreAnyOverridesPresent	( int iUserData ) { return ( ( Tools_UI_Overrides* ) iUserData )->AreAnyOverridesPresent(); }
	static bool	IsCurrentOverrideMapValid	( int iUserData );
	static bool IsTableRowEnabled(const u_int uRow, void* const pxUserData);
	static unsigned TableGetColourCallBack( const Asura_Table_Dialog& xTableDialog, void* const pxUserData, const Asura_Dialog* const pxDialog, const unsigned uColour, const u_int uRow, const u_int uColumn )
	{
		Tools_UI_Overrides* pxThis = (Tools_UI_Overrides*)pxUserData;
		return( pxThis->ChangeTableColumnColour( uColour, uColumn ) );
	}
	unsigned ChangeTableColumnColour( const unsigned uColour, const u_int uColumn );


	static void	CreateNewOverrideMap	( int iUserData ) { ( ( Tools_UI_Overrides* ) iUserData )->CreateNewOverrideMap(); }
	static void	SelectCurrentOverrideMap( int iUserData ) { ( ( Tools_UI_Overrides* ) iUserData )->SelectCurrentOverrideMap(); }
	static void	DeleteCurrentOverrideMap( int iUserData ) { ( ( Tools_UI_Overrides* ) iUserData )->DeleteCurrentOverrideMap(); }
	static void	CopyCurrentOverrideMap	( int iUserData ) { ( ( Tools_UI_Overrides* ) iUserData )->CopyCurrentOverrideMap(); }
	static void	RenameCurrentOverrideMap( int iUserData ) { ( ( Tools_UI_Overrides* ) iUserData )->RenameCurrentOverrideMap(); }


	bool AreAnyOverridesPresent() { return m_iNumOverrides > 0;}
	void OnlyShowItemsWithAnOverrideClicked();

	void CreateNewOverrideMap();
	void SelectCurrentOverrideMap();
	void DeleteCurrentOverrideMap();
	void CopyCurrentOverrideMap();
	void RenameCurrentOverrideMap();

	bool DoesItemHaveOverride( const Asura_Hash_ID uHash );


public:
	Tools_UI_Overrides(Tools_UI_Overrides_Interface &xInterface);

	~Tools_UI_Overrides();

	// Accessor functions...
	const Asura_String*		GetNameArray	( void ) const	{ return( m_pxNameArray ); }
	const Asura_String*		GetHeadingArray	( void ) const	{ return( m_pxHeadingArray ); }
	const Asura_Hash_ID*	GetHashArray	( void ) const	{ return( m_puHashArray ); }
	int						GetNumEntries	( void ) const	{ return( m_iNumEntries ); }

	const Asura_String *	GetOverrideNameArray	( void ) const	{ return( m_pxOverrideNameArray ); }
	const Asura_String*		GetOverrideHeadingArray	( void ) const	{ return( m_pxOverrideHeadingArray ); }
	const Asura_Hash_ID*	GetOverrideHashArray	( void ) const	{ return( m_puOverrideHashArray ); }
	int						GetOverrideNumEntries	( void ) const	{ return( m_iNumOverrides ); }


	void					SetNameArray(Asura_String* pxNameArray, int iNumEntries);
	void					SetOverrideNameArray(Asura_String* pxNameArray, int iNumEntries);

	int						GetIndexForEntry( const Asura_Hash_ID uHash, const int iDefaultIndex = -1 ) const;
	int						GetIndexForOverride( const Asura_Hash_ID uHash, const int iDefaultIndex = -1 ) const;

	Asura_Table_Dialog*		GetTableDialog	( void ) const	{ return( m_pxTableDialog ); }

	void					AddDialogs ( Asura_Overlay *pxOverlay, Asura_Vector_2& xOffset, const Asura_Vector_2& xTableSize );
	void					RemoveDialogs () { m_pxTableDialog = NULL; }

	void Refresh( void );
	void RefreshCell( Asura_Hash_ID uOverrideMapHash, Asura_Hash_ID uEntryHash );

	void CreateTable();
	void DestroyTable(void);

	Tools_UI_Overrides_Interface&		GetInterface() {return m_xInterface;}

	const Tools_UI_Overrides_FilterSet*	GetOverridesFilters() const {return m_pxOverridesFilters;}
	Tools_UI_Overrides_FilterSet*		GetOverridesFilters() {return m_pxOverridesFilters;}

};

class Tools_UI_Overrides::TableCell_OverrideHash : public Asura_TableCell_Dialog
{
public:
	// Public functions...
	TableCell_OverrideHash( Asura_Table_Dialog& xTableDialogue, Tools_UI_Overrides* const pxParent, const Asura_Vector_2& xOffset, const Asura_Vector_2& xMinCellSize, const u_int uRow, const u_int uColumn );
	//	virtual ~Tools_TableCell_MaterialResponseOverride_Dialog ();

	// Overridden functions...
	virtual int		Compare	(const Asura_TableCell_Dialog& xOther) const;
	virtual void	Update	(const Asura_Vector_2& xOrigin) { PARENT::Update(xOrigin); }

	void	RefreshDialog			( void );

protected:
	// Protected variables...

	// Protected functions...
	static void	SelectNew	( int iUserData )	{ ( ( TableCell_OverrideHash* ) iUserData )->SelectNew(); }
	void	SelectNew		( void );

private:


	// Private typedefs...
	typedef Asura_TableCell_Dialog PARENT;

	// Private variables...
	TableCell_OverrideHash_EntryDialog*	m_pxDialog;	// A pointer to the dialog used.

	// Private functions...
	TableCell_OverrideHash  (void);
	TableCell_OverrideHash  (const TableCell_OverrideHash& xOther);

	TableCell_OverrideHash&	operator=	(const TableCell_OverrideHash& xOther);

	Tools_UI_Overrides* m_pxParent;

};   // End of class 'Tools_TableCell_MaterialResponseOverride_Dialog'.


class Tools_UI_Overrides::TableCell_OverrideHash_EntryDialog : public Asura_Function_Dialog
{
public:
	TableCell_OverrideHash_EntryDialog( 
		Tools_UI_Overrides* const pxParent,
		const u_int uColumn,
		const char* szName,
		const Asura_Vector_2& xOffset,
		void(*pfnCallback)(int),
		int iUserData)
		: PARENT( szName, xOffset, pfnCallback, iUserData )
		, m_pxParent( pxParent )
		, m_iColumn( uColumn )
	{
	}

	virtual unsigned GetColour( void )			const	{ return( m_pxParent->ChangeTableColumnColour( PARENT::GetColour(), m_iColumn ) ); }

private:
	typedef Asura_Function_Dialog PARENT;
	Tools_UI_Overrides* const m_pxParent;
	const u_int m_iColumn;
};

#endif