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

#include<QtGui>
#include <Qt3Support>

#include "DJGamePanel.h"
#include "DJTopWidget.h"

#include "protocol.h"
#include "xqrule.h"
#include "xqprofile.h"
#include "panelbase.h"

class XiangqiPanel;
class DJTreeWidget;
class XiangqiDesktop;
class DJGamePlayerItem;
	
class XiangqiRuleItem:public QObject
{
	Q_OBJECT 
	public :
		XiangqiRuleItem(Q3Canvas * canvas, QWidget* parent,XiangqiRule *prule);
		~XiangqiRuleItem();
		void move(int x,int y);
		void setRule(XiangqiRule *prule);
		void startTimer();
		void stopTimer();
		void show();
		void hide();
	
	private slots :
		void HandleTimeout();
	private :
		void CreateItems();
	
		XiangqiDesktop		*c_Desktop;
		Q3Canvas			*c_Canvas;
	
		XiangqiRule rule;
		Q3CanvasText		*c_basictext;
		Q3CanvasText		*c_basictime;
		Q3CanvasText		*c_dumiaotext;
		Q3CanvasText		*c_dumiaotime;
		Q3CanvasText		*c_timestext;
		Q3CanvasText		*c_times;
		QTimer				*c_timer;
	
	int c_iStart,c_iElps;
	int c_X,c_Y;
};

class XiangqiDesktop:public Q3CanvasView
{
        Q_OBJECT
public :
        XiangqiDesktop(Q3Canvas * canvas, QWidget* parent,XiangqiPanel *panel);
        ~XiangqiDesktop();
		void RepaintBoard();
		void RepaintAvatar();

		void DrawChip(unsigned char x,unsigned char y);
		void ClearChip(unsigned char x,unsigned char y);
		void ClearAllChips();
		void WaitMove();
		void GameWait();
		void PlayerMove(unsigned char chSite,unsigned char chStart,unsigned char chEnd);
		void RequestDuels(unsigned char chSite);
		void AdjustBoard(const GeneralGameTrace2Head *gameTrace);
		void WaitStart();
		bool hasStarted();
		
		CChessImageItem * GetChipOfNode(unsigned char x,unsigned char y);
		void GetNodeXY(unsigned char x,unsigned char y,int *px,int *py);
		
		void SetRule(unsigned char chSite,XiangqiRule *prule);
		int  c_X,c_Y,c_Fx;
		bool c_bRequestDuce[5],c_bWaitACL,c_bMove;
		
		QString c_strSaveFile;
		XiangqiPanel *c_Panel;
protected :
	void resizeEvent( QResizeEvent * );
	void contentsMouseReleaseEvent(QMouseEvent* e);
	void contentsDragMoveEvent ( QDragMoveEvent * e ) ;

private slots :
	void ClickStart();
	void ClickSurrender();
	void ClickDues();

	void HandleTimeout();
	void HandleMoveTimeout();
private :
	bool	IsJiangJun(unsigned char chEnd);
	void GetAvatarPosition(unsigned char chSite,int *x,int *y);
	void GetTimerPosition(unsigned char chSite,int *x,int *y);
	void GetUserNamePosition(unsigned char chSite,Q3CanvasText *item,int *x,int *y);

	
	Q3Canvas 		*c_Canvas;
	QPushButton		*c_pbStart,*c_pbSur,*c_pbDues;
	CChessImageItem  *c_itemAvatar[5],*c_itemBoard,*c_itemFocus,*c_itemStartPos;
     Q3CanvasText				*c_itemName[5];

	Q3CanvasPixmapArray *c_paTimer;
	Q3CanvasSprite *itemTimer;
	QTimer			*c_timer,*c_MoveTimer;
	
	XiangQiBoard		c_Board;
	quint16				c_shSkin;
	quint8				c_chStartPoint;
	XiangqiRuleItem		*c_selfRule,*c_comRule;
};



class  XiangqiPanel : public DJGamePanel
{
	Q_OBJECT
	public :
        XiangqiPanel( DJGameController *gameController, DJGameRoom *room, const QList<quint32>& userIds, quint8 tableId, quint8 seatId, QWidget * parent = 0, Qt::WFlags f = Qt::Window);
        ~XiangqiPanel();
		
		virtual void initialize();
	 	
	 	virtual DJGamePlayerItem* addPlayer( quint32 userId, bool isPlayer = true );
		
        //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 );
		bool IsWaitMove();
		bool WaittingForMe();
		XiangqiCurrent	c_Current;
	
	protected :
		//void resizeEvent( QResizeEvent * );	
		void closeEvent( QCloseEvent *e);	
	private :
		//DJTreeWidget		*c_PlayerList;
		QSplitter 		*m_leftSplitter;
		XiangqiDesktop	*c_Desktop;
		Q3Canvas			*c_DesktopCanvas;
		DJTopWidget *c_Ad;
		quint8			c_chTableStatus;
		quint16 c_shWaitMask;
		int				c_iTimeLin;

};



#endif
