//****************************************************************************
//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 __DJGAME_GAMEPANEL_DESKTOP_H__
#define __DJGAME_GAMEPANEL_DESKTOP_H__

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

#include "DJGamePanel.h"
#include "DJTreeWidget.h"
#include "protocol.h"
#include "DJGameImage.h"
#include "DJGameTraceManager.h"

		
/*
			avatar, view 3

avatar,view2					view4, avatar


avatar,view1( self )
*/

#define WAIT_TIMER_WIDTH		60
#define WAIT_TIMER_HEIGHT		40
#define TEXT_ITEM_FONT_SIZE		16

class DJGameChessTimerItem;
class DJGameDesktopWaitTimer;
class DJWidget;
class DJShowView;
class DJMatrixCanvasText;
class DJMatrixCanvasTimer;
class DJMatrixCanvasImage;
class DJMatrixCanvasSprite;

class DJ_BASE_EXPORT DJGameDesktop:public Q3CanvasView
{
	Q_OBJECT
public :
    DJGameDesktop(QWidget* parent,DJGamePanel *panel,quint32 w,quint32 h);
    virtual ~DJGameDesktop();
    
    virtual QString	playerItemNameSuffix( DJGameUser* user );
    virtual void lookerIn( quint32 userId, bool in );
    virtual void userArrived( quint32 userId, quint8 seatId );
    virtual void gameInfo( const unsigned char *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 QString bonusTarget( const DJGameBonus* bonus );
    virtual void bonusFulfilled( const DJGameBonusFulfill* fulfill );
	virtual bool isCloseable();
    virtual void RepaintCurrentStatus();
    
    virtual bool SupportResearch();
    virtual void StaticInitDesktop();
    virtual void StaticGameTrace(GeneralGameTrace2Head *gameTrace);
    	
    virtual void MoveItem(Q3CanvasItem* item,QPoint &ps,QPoint &pe);
    
    virtual quint8 MapSeat2View(quint8 seat);
    virtual quint8 MapView2Seat(quint8 view);
    
    virtual quint8	numberOfPlayers() const;
    virtual QList<quint8> seatsOfPlayers() const;
    quint8 numberOfSeatUsers() const;
    
    void relocateAndRepaintAll();
    void repaintAllPlayers( bool reload = true );
    void repaintPlayer( quint8 seat, bool reload = false );
    void repaintShowView( quint8 seat, bool reload = false );
    void repaintNameItem( quint8 seat, bool reload = false );
    void repaintClock();
    void setClockTimeout( quint16 mask, int seconds );
    virtual void repaintHost();
    void repaintHostItem( quint8 seat );
    void setHostEnabled( bool enabled );
	void hideHost();
	
	//show,name,clock,host, accessor
    DJShowView *showView( quint8 seat );
    DJMatrixCanvasText* nameItem( quint8 seat );
    DJMatrixCanvasTimer* clockItem() const { return m_clockItem; }
    DJMatrixCanvasImage* hostItem() const { return m_hostItem; }
    
    QRect idealRectOfShow( quint8 seat ) const;
    QRect idealRectOfName( quint8 seat ) const;
    //QRect idealAvailableRect();
    
    virtual void GetDesktopCenterPoint(int *x,int *y,int *selfHeight);
    
    virtual void locateShowPositions( QList<QPoint>& points, QList<Qt::Alignment>& aligns, QList<int> &modes );
    virtual void locateNamePositions( QList<QPoint>& points, QList<Qt::Alignment>& aligns, QList<int> &modes );
    virtual void locateClockPosition( QPoint& point, Qt::Alignment& align, int &mode );
    virtual void locateHostPositions( QList<QPoint>& points, QList<Qt::Alignment>& aligns, QList<int> &modes );
	
    QList<quint8> mask2Seat(quint16 mask);
    bool IsWaittingForMe();	
    void SetSavable(bool b);
    	
    quint8 prevSeatId( quint8 seatId, bool isClockwise = false ) const;
    quint8 nextSeatId( quint8 seatId, bool isClockwise = false ) const;
    	
    DJGameImageItem * FindImageItem(quint16 tid,quint16 sid,quint16 type);
    DJGameTextItem * FindTextItem(quint16 tid,quint16 sid,quint16 type);
    DJGameLineItem * FindLineItem(quint16 tid,quint16 sid,quint16 type);
    quint16 ClearLine(quint16 tid,quint16 sid,quint16 type);
    quint16 ClearText(quint16 tid,quint16 sid,quint16 type);
    void ClearAutoDisappearText();
    void ShowAutoDisappearText( const QString& text, int second = 10 ) const;
    quint16 ClearImage(quint16 tid,quint16 sid,quint16 type);
    	
    DJGameTraceManager& GetTraceManager() { return c_GameTraces; }
    void SetTraceManager(DJGameTraceManager &traces);
    DJGamePanel * GetGamePanel() const { return c_Panel; }
    	
    quint32 GetRealHeight() const { return c_RealHeight; }
    quint32 GetRealWidth() const { return c_RealWidth; }
    quint8	GetTableStatus() const { return m_gameWaitStatus; }
    quint16 GetTotalGames() const { return c_TotalGames; }
    void    SetTotalGames(quint16 sh);
    	
    QMatrix 	GetCurrentMatrix() const { return c_matrix; }
    void calculateAndSetCurrentMatrix();
    void setRealSize( quint32 width, quint32 height );
    quint16		GetTextItemHeight(DJGameTextItem *t);
    quint16		GetTextItemWidth(DJGameTextItem *t);
    
    QPushButton* startButton() const { return m_start; }
	void SendGameTrace(quint8 trace,char *data = 0,quint8 chLen = 0,AclProc aclProc = 0, const QVariant& parameters = QVariant() );
	
	DJWidget* bonusWidget() const { return m_bonusWidget; }
	QLabel* bonusTargetWidget() const { return m_bonusTargetWidget; }
	QLabel* bonusResultWidget() const { return m_bonusResultWidget; }
	
public slots:
	void changeBackgroundColor( const QColor& color );
	void ClickStart();
signals:
	void gameStarted();
protected :
	virtual void resizeEvent( QResizeEvent * );
	virtual void contentsMousePressEvent(QMouseEvent* e);
	virtual void contentsMouseDoubleClickEvent(QMouseEvent* e);
		
	virtual bool handleItemClicked( Qt::MouseButton btn, const QPoint& pos, Q3CanvasItem *item );
	virtual bool handleItemDoubleClicked( Qt::MouseButton btn, const QPoint& pos, Q3CanvasItem *item );
		
	virtual void handleMatrixChanged( const QMatrix& matrix );
		
	DJGameTraceManager c_GameTraces;
	
	
private slots :
	void HandleResizeTimeout();
private :
	DJGamePanel *c_Panel;
	
	QTimer			*c_resizeTimer;
	bool			c_bSavable;
	QPushButton		*m_start;
	
	quint8			m_gameWaitStatus;
	quint16			m_gameWaitMask,m_gameWaitTimeout;
	quint32 		c_RealWidth,c_RealHeight;
	quint16			c_TotalGames;
		
	Q3CanvasText *c_forTestStringWidthHeight;
	
	QMatrix c_matrix;
	
	QList<DJShowView*>			m_showViews;
	QList<QPoint>				m_showPoints;
	QList<Qt::Alignment>		m_showAligns;
	QList<int>					m_showModes;
		
	QList<DJMatrixCanvasText*>	m_nameItems;
	QList<QPoint>				m_namePoints;
	QList<Qt::Alignment>		m_nameAligns;
	QList<int>					m_nameModes;
		
	DJMatrixCanvasTimer*		m_clockItem;
	QPoint						m_clockPoint;
	Qt::Alignment				m_clockAlign;
	int							m_clockMode;
	
	DJMatrixCanvasImage*		m_hostItem;
	QList<QPoint>				m_hostPoints;
	QList<Qt::Alignment>		m_hostAligns;
	QList<int>					m_hostModes;
	bool						m_hostEnabled;
	
	DJWidget					*m_bonusWidget;
	QLabel						*m_bonusTargetWidget,*m_bonusResultWidget;	
};


class DJ_BASE_EXPORT DJGameChessTimerItem:public QObject
{
	Q_OBJECT 
	public :
		DJGameChessTimerItem(Q3Canvas * cv,DJGameDesktop *,PDJGameChessTimer pchesstimer=NULL);
		virtual ~DJGameChessTimerItem();
		void setTimer(PDJGameChessTimer pchesstimer);
		void startTimer();
		void pauseTimer();
		void show();
		void hide();
		
		virtual void ModifyTimeText();
		virtual quint16 move(QRect & rt,bool b2Down=true);
	private slots :
		void HandleTimeout();
	signals :
		void Last10Launch(unsigned char chLast);
	private :
	
		Q3Canvas			*c_Canvas;
	
		DJGameChessTimer c_chessTimer;

		DJGameTextItem		*c_basictimetext;
		DJGameTextItem		*c_basictime;
		DJGameTextItem		*c_dumiaotext;
		DJGameTextItem		*c_dumiaotime;
		DJGameTextItem		*c_timestext;
		DJGameTextItem		*c_times;
		QTimer				*c_timer;
	
		int c_iStart,c_iElps;
	
	
	DJGameDesktop *c_Desktop;
};


#endif
