//****************************************************************************
//Copyright (C) 2005-2006 Beijing BlueDJ Technology Co.,Ltd. All rights reserved.

//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 2
//of the License, 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 (in the file LICENSE.GPL); if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

//Please visit http://www.bluedj.com for more infomation about us.
//Contact us at ggwizard@gmail.com or darkdong@gmail.com.
//****************************************************************************/

#ifndef	DJGAMEPANEL_H
#define DJGAMEPANEL_H

#include <QtGui>
#include "DJBaseGlobal.h"
#include "protocol.h"

class DJGameController;
class DJClientRequest;
class DJGameRoom;
class DJGameUser;
class DJGroupChat;
class DJTreeWidget;
class DJGamePlayerItem;
class DJGameDesktop;
class DJShowView;
class DJToolButton;

class DJ_BASE_EXPORT DJGamePanel : public QMainWindow
{
	Q_OBJECT
public :
	enum PlayerItemSectionId{
    	UserName,
    	NickName,
    	Seat,
    	PanelScore,
    	Score,
    	PanelChips,
    	Chips,
    	DoorWind,
    	RingWind,
    	LastSectionId
    };

	DJGamePanel( DJGameController* gameController, DJGameRoom *gameRoom, const QList<quint32>& tableUserIds, quint8 tableId, quint8 seatId, QWidget * parent = 0, Qt::WFlags f = Qt::Window );
	virtual ~DJGamePanel();
	
	//methods for panel 
	void closeChildDialog();
	void removePendingAcl();
	DJGameDesktop* gameDesktop() const { return m_gameDesktop; }
	void setGameDesktop( DJGameDesktop* gameDesktop );
	virtual void createAccessoryWidgets();
	QSplitter* accessorySplitter() const { return m_accessorySplitter; }
	void setAccessorySplitter( QSplitter* splitter );
	void createPanelBar( QWidget* parent );
	int layoutMode() const { return m_layoutMode; }
	//methods for gamecontroller
	virtual void lookerIn( quint32 userId, bool in );
	virtual void userArrived( quint32 userId, quint8 seatId );
	virtual void gameInfo( const QByteArray& buf );
	virtual void gameWait( quint16 mask, quint8 status ,quint16 timeout );
	virtual void gameTrace( const GeneralGameTrace2Head *gameTrace );
	virtual void magicResult( const MagicResult* magicResult );
	virtual void gameOver( const DJGameOver* gameOver );
	virtual void bonusCondition( const DJGameBonus* condition );
	virtual void bonusFulfilled( const DJGameBonusFulfill* fulfill );
	
	DJGameController*		gameController() const { return m_gameController; }
	void playWave( const QString& waveName, const QString& gameName = QString() ) const;
	bool sendRequest( const DJClientRequest& request, bool isDiscardable = false );
	DJGameUser*	gameUser( quint32 userId ) const;
	DJGameUser*	userAtSeat( quint8 seatId ) const;
	DJGameRoom* gameRoom() const { return m_gameRoom; }
	QByteArray	makeGameData( quint8 subType, const QByteArray& gameData = QByteArray(), quint8 type = DJGAME_PROTOCOL_TYPE_SUBMIT_GAMEDATA );
	
	QList<quint32> panelUserIds() const { return m_tableUserIds; }
	quint32 selfUserId() const { return m_tableUserIds.at(0); }
	quint8 tableId() const { return m_tableId; }
	quint8 selfSeatId() const { return m_selfSeatId; }
	bool isLookingOn() const;
	bool isUserPlaying( quint32 userId ) const;
	quint8 seatIdOfUser( quint32 userId ) const;
	quint32	userIdOfSeat( quint8 seatId ) const;
	void setUserIdOfSeat( quint8 seatId, quint32 userId );
	void resetPlayers( const QList<quint32> tableUserIds );
	quint8 numberOfSeats() const;
	//methods for chat widget
	DJGroupChat* createChatWidget( QWidget* parent );
	DJGroupChat* chatWidget() const { return m_chatWidget; }
	void receiveChat( const ChatContent	*chatContent );
	void insertText2Editor( const QString& text );
	void insertText2Browser( const QString& text );
	void insertGameResult2Browser( const QString& text );
	void deltaUserData( DJGameUser *user, int value );
	//methods for players tree widget
	DJTreeWidget* createPlayersWidget( QWidget* parent );
	DJTreeWidget* playersTreeWidget() const { return m_playersTreeWidget; }
	virtual QString	playerItemNameSuffix( DJGameUser* user );
	void repaintAllPlayerItems();
	virtual DJGamePlayerItem* addPlayerItem( quint32 userId, bool isPlayer );
	void removePlayerItem( quint32 userId );
	DJGamePlayerItem*	playerItem( quint32 userId );
	int 				indexOfSection( int sectionId ) const;
	QString				sectionName( int sectionId ) const;
	QString				baseSectionName( int sectionId );
	virtual DJGamePlayerItem* createPlayerItem( DJGameUser *user, QTreeWidget* parent, int type = QTreeWidgetItem::Type );
	virtual void createPlayerItemHeader( QTreeWidget* treeWidget );
	virtual void playerItemHeaderSections( QList<int> &sectionIds, QStringList &sectionNames );
	
	//methods for card
	void cardUsed( const ReqUseCard *useCard );
	//void dropMsg();
	
	DJToolButton*	logo() const { return m_logo; }
	DJToolButton*	color() const { return m_color; }
	DJToolButton*	help() const { return m_help; }
signals:
	void changeBackgroundColor( const QColor& color );
public slots:
	virtual void showPlayerContextMenu( quint32 userId );
	void playerItemDoubleClicked( QTreeWidgetItem * item, int column );
	void playerItemRightClicked( const QPoint& point );
protected :
	virtual void closeEvent( QCloseEvent *e );
private slots:	
	void acceptPrivateView( quint32 lookerId );
	void toggleShowHide();
	void handleLogoClicked();
	void handleColorClicked();
	void handleHelpClicked();
private:
	void requestPrivateView( quint32 hostId );
	void closePrivateView( quint32 lookerId );
	
	DJGameController		*m_gameController;
	DJGameRoom				*m_gameRoom;
	QList<quint32>			m_tableUserIds;
	quint8					m_tableId;
	quint8					m_selfSeatId;
	DJGroupChat				*m_chatWidget;
	DJTreeWidget			*m_playersTreeWidget;
	QMenu					*m_playerContextMenu;
	QAction					*m_actRequestPrivateView,*m_actAcceptPrivateView,*m_actClosePrivateView;
	QList<void*>			m_acls;
	DJGameDesktop			*m_gameDesktop;
	QSplitter				*m_accessorySplitter;
	DJShowView				*m_selfShowView;
	QList<int>				m_sectionIds;
	QStringList				m_sectionNames;
	
	DJToolButton			*m_logo,*m_color,*m_help;
	int						m_layoutMode;
};
#endif
