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

#include<QtGui>
#include <Qt3Support>

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

#include "protocol.h"
#include "4country.h"
#include "panelbase.h"

class LandBattlePanel;
class DJTreeWidget;
class DJMatrixCanvasTimer;

#define JUNQI_CHESSMAP_RANGE 16

class LandBattleDesktop:public Q3CanvasView
{
        Q_OBJECT
public :
        LandBattleDesktop(Q3Canvas * canvas, QWidget* parent,LandBattlePanel *panel);
        ~LandBattleDesktop();
		void WaitStart();
		void WaitArrange(bool bIncludeMe);
		void WaitMove(bool bIncludeMe);
		void PlayerMoveChip( const GeneralGameTrace2Head *gameTrace );
		void SetPlayerChip(PJunqiSetChip psetchip);

		void DrawPath(PMapNode pcurnode,unsigned char *path);
		void RepaintPlayer();
		void RepaintChessmap(int iContext);
		void Initchip(PJunqiTraceInitChip psetchips);
		void ClearNodeChip(PMapNode pnode);
		void CreateNodeChip(PMapNode pnode,bool bBig=false);
		char GetPhysicalBoard(PMapNode pnode);

		LandBattleImageItem * GetChipOfNode(PMapNode pnode);
		PMapNode GetNodeByPoint(int x,int y);
		void GetCurrentLayout(PGeneralGameTrace2Head ptrace,unsigned char chMapSite);
		
		//void SetTimerStatus(quint16 mask,int iTimeoutLine);
		void gameWait( quint16 mask, quint8 status ,quint16 timeout);
		
		//void CreateTimer();
		void ClearAllChips();
		void ClearAllLine();
		bool hasStarted();
		QString c_strSaveFile,c_strSaveLayoutFile;
		
		int  c_X,c_Y;
		bool c_bWaitACL,c_bArrange,c_bMove;
		bool c_bRequestDuce[8];
protected :
	void resizeEvent( QResizeEvent * );
	void contentsMouseReleaseEvent(QMouseEvent* e);
	void contentsDragMoveEvent ( QDragMoveEvent * e ) ;
	PMapNode c_pMoveTempEnd;

private slots :
	void ClickStart();
	void ClickSave();
	void ClickLoad();
	void ClickSend();
	void ClickSurrender();
	void ClickDuce();
	void HandleExchangeTimeout();
	//void HandleTimeout();
	void HandleMoveTimeout();
private :
	
	void GetAvatorPosition(quint8 chMapSite,LandBattleImageItem *item,int *x,int *y);
	void GetTimerPosition(quint8 chMapSite,int *x,int *y);

	LandBattlePanel *c_Panel;
	Q3Canvas 		*c_Canvas;
	QPushButton		*c_pbStart;
	LandBattleImageItem  *c_itemProtrait[8],*c_itemBoard,*c_itemDuce[8];
     Q3CanvasText				*c_itemName[8],*c_itemUnionSign[8];
	QPushButton			*c_pbSave,*c_pbLoad,*c_pbSend,*c_pbDuce,*c_pbSurrender;
	PMapNode 			c_pArrangeNode1,c_pArrangeNode2;
	PMapNode			c_pMoveChipNode;
	QTimer				*c_timerExchange;

	//Q3CanvasPixmapArray *c_paTimer;
	//Q3CanvasSprite *itemTimer;
	//Q3CanvasText	*itemTimerText;
	//QTimer			*c_timer;
	//int 			c_timeline;
	//quint16			c_shTimerMask;
	
	PMapNode		c_PlayerMoveNodeStart,c_PlayerMoveNodeEnd;
	unsigned char   c_MoveResult;
	QTimer			*c_MoveTimer;
	
	DJMatrixCanvasTimer	*m_clockItem;
	
};

#define LANDBATTLEPANEL_CHESSMAP_MAXSIZE		65535

class  LandBattlePanel : public DJGamePanel
{
	Q_OBJECT
	public :
        LandBattlePanel( DJGameController *gameController, DJGameRoom *room, const QList<quint32>& userIds, quint8 tableId, quint8 seatId, QWidget * parent = 0, Qt::WFlags f = Qt::Window);
        ~LandBattlePanel();
		
		virtual void createAccessoryWidgets();
		
        //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 );
	
		unsigned char GetMapSiteOfSeat(unsigned char seat);
		unsigned char GetSeatOfMapSite(unsigned char chMapSite);
	
		unsigned char GetCurrentPlayers();
		bool 			WaittingForMe();		 
		DJGameController 	*c_Ctrl;
		DJGameRoom 		*c_Room;
		quint8				c_Table,c_Seat,c_chTableStatus,c_MapSite,c_LastTraceType;
		quint16				c_shWaitMask;
		int 				c_iTimeoutLine;
	
		JunqiCurrent		c_Current;
		char 				c_Chessmap[LANDBATTLEPANEL_CHESSMAP_MAXSIZE];
		unsigned char 		c_chUnion[5],c_chUnions;
	protected :
		//void resizeEvent( QResizeEvent * );	
		void closeEvent( QCloseEvent *e);	
	private :
		//DJTreeWidget		*c_PlayerList;
		//QSplitter 			*m_leftSplitter;
		LandBattleDesktop	*c_Desktop;
		Q3Canvas			*c_DesktopCanvas;
		DJTopWidget *c_Ad;

};
#endif
