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

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

#include "protocol.h"
#include "mjrule.h"
#define DJGAME_MJDESKTOP_COMPARE_RESUST_1		1
#define DJGAME_MJDESKTOP_COMPARE_RESUST_2		-1
#define DJGAME_MJDESKTOP_COMPARE_RESUST_E		0

#define MAHJONG_CARD_SPACE						0x3C

static const int djgame_mjitemRTTI = 98997432;

typedef struct __tagMahjongPoolCard
{
	quint8 chCard;
	quint8 chSeat;
}MahjongPoolCard,*PMahjongPoolCard;

#define DJGAME_DESKTOP_TYPE_WALL			0x80
#define DJGAME_DESKTOP_TYPE_POOL			0x81
#define DJGAME_DESKTOP_TYPE_HAND			0x82
#define DJGAME_DESKTOP_TYPE_STATIC			0x83
#define DJGAME_DESKTOP_TYPE_FLOWER			0x84

#define DJGAME_DESKTOP_TYPE_HU_TEXT			0x85
#define DJGAME_DESKTOP_TYPE_HU_CARD			0x86

#define DJGAME_MAHJONG_CHIMASK_12X			0x01
#define DJGAME_MAHJONG_CHIMASK_1X3			0x02
#define DJGAME_MAHJONG_CHIMASK_X23			0x04

class DJ_BASE_EXPORT DJGameMJDesktop : public DJGameDesktop
{
	Q_OBJECT
	public :
		DJGameMJDesktop(QWidget* parent,DJGamePanel *panel,quint32 w=900,quint32 h=900);
		virtual ~DJGameMJDesktop();
			
		void StaticInitDesktop();
		virtual void GetDesktopCenterPoint(int *x,int *y,int *iSelfHeight);	
		quint8 GetPlayerCurrentCards(quint8 seat,quint8 *buf,quint8 chLen);	
		void ResetPlayerCards(quint8 chSeat,quint8 *pbuf,quint8 chPages);
	
		void PlayerPickup(quint8 chSeat,quint8 card);
		void PlayerThrow(quint8 chSeat,quint8 chCard,bool b2Pool=true);
		void PlayerTing(quint8 chSeat);
		
		virtual void OrderCard(quint8 *pcards,quint8 chPages);
		virtual int Compare2Card(quint8 chCard1,quint8 chCard2);
		void PlayerCHI(quint8 chSeat,quint8 card1,quint8 card2,quint8 card3);
		void PlayerPENG(quint8 chSeat,quint8 card);
		void PlayerGANG(quint8 seat,quint8 card);
		
		bool playerTingStatus(quint8 chSeat);
		bool playerHuStatus(quint8 chSeat,bool bDIY, quint8 lastMGangCard = 0);
		bool playerPengStatus(quint8 chSeat);
		
		bool hasPlayerTing( quint8 seatId ) const;
		quint8 playerChiMask(quint8 chSeat);
		quint8 playerGangStatus(quint8 chSeat,quint8 chCard,quint8 *pchGangs,quint8 chLen);
		
		void SelectCard(DJGameImageItem *item);
		void SelectCard(quint8 chCard);
		
		void SetRule(MahjongRule *prule);
		
		void SetHuPages(quint8 pages);
		
		void RepaintPoolCards(quint8 seat);
		void RepaintView1PoolCards(quint8 seat);
		void RepaintView2PoolCards(quint8 seat);
		void RepaintView3PoolCards(quint8 seat);
		void RepaintView4PoolCards(quint8 seat);
		
		void RepaintPlayerCards(quint8 seat);
		void RepaintView1Cards(MahjongHUTypeWrap * ,quint8 *,quint8 *);
		void RepaintView2Cards(MahjongHUTypeWrap * ,quint8 *,quint8 *);
		void RepaintView3Cards(MahjongHUTypeWrap * ,quint8 *,quint8 *);
		void RepaintView4Cards(MahjongHUTypeWrap * ,quint8 *,quint8 *);
		
		void RepaintPlayerFlowers(quint8 seat);
		void RepaintView1Flowers(quint8 seat);
		void RepaintView2Flowers(quint8 seat);
		void RepaintView3Flowers(quint8 seat);
		void RepaintView4Flowers(quint8 seat);
		void AppendPlayerFlowerCard(quint8 seat,quint8 chCard);
		
		void RenewWall();
		void RepaintWallCards();
		void RepaintView1WallCards();
		void RepaintView2WallCards();
		void RepaintView3WallCards();
		void RepaintView4WallCards();
		
		void SetWallStartPoint(quint8);
		void RemoveWallCard(quint8 chPages,bool bFormEnd=false);
		void ClearPoolCards(quint8 seat);
		void ClearHandCards(quint8 seat);
		void ClearFlowerCards(quint8 seat);
		virtual QImage GetWallCard(quint8 chView,int *h,qreal scale = 0);
		virtual QImage GetPoolCard(quint8 chView,quint8 chCard,int *h, qreal scale = 1, bool isFlower = false );
		virtual QImage GetView1Card(quint8 chCard,bool bStatic,bool bUnknown,int *h);
		virtual QImage GetView2Card(quint8 chCard,bool bStatic,bool bUnknown,int *h);
		virtual QImage GetView3Card(quint8 chCard,bool bStatic,bool bUnknown,int *h);
		virtual QImage GetView4Card(quint8 chCard,bool bStatic,bool bUnknown,int *h);
		
		virtual quint8 GetMahjongThrowTraceID(quint8 card);
		
		int GetCardIndex(quint8 chCard);
		quint8 GetTotalCardPages();
		quint8 GetWallLength(quint8 chView);
		void SetSeatPrivate(quint8 seat);
		bool GetSeatPrivate(quint8 seat);
		qreal GetFx() const { return c_Fx; }
		void SetFx(qreal fx);
		void SetThrowFlag(bool b);
		bool GetThrowFlag();
		quint8 GetLastThrowCard();
		quint8 GetLastThrowSeat();
		
		quint16 supportHuStyle() const { return c_shHuMask; }
		quint16 setSupportHuStyle(quint16 shMask);
		void AdjustPlayerCards(quint8 seat,quint8 *pcards);
		int styleOfPoolCards() const { return m_styleOfPoolCards; }
		void setStyleOfPoolCards( int style ) { m_styleOfPoolCards = style; }
	protected :
		virtual bool handleItemClicked( Qt::MouseButton btn,const QPoint& pos, Q3CanvasItem *item );
		virtual bool handleItemDoubleClicked( Qt::MouseButton btn,const QPoint& pos, Q3CanvasItem *item );
	private :
		MahjongRule			c_Rule;
		quint8 				c_chPlayerCards[5][20];
		quint8 				c_chPlayerFlower[5][10];
	
		MahjongPoolCard		c_PoolCards[150];
		quint8				c_chPoolPages;
	
		quint8				c_chWallStartSeat,c_chWallStartPos;
		quint8				c_chWallRemoves,c_chWallEndRemoves;
		quint8				c_chWallRemoved,c_chWallEndRemoved;
	 	
		MahjongHUTypeWrap			c_playerNormalHu[5];
		quint8 						c_chHuPages;
	
		
	
		DJGamePanel				*c_Panel;
		quint8				c_chTotalSeats;
	
		qreal c_Fx;
		qreal	m_scaleOfWall,m_scaleOfPool;
		
		QList<qreal>		m_scaleOfHand;//index is view
		QList<qreal>		m_scaleOfBack;//index is view
		QList<qreal>		m_scaleOfAnGang;//index is view
		
		QList<int>			m_heightOfLyingCard;//index is view
		QList<int>			m_heightOfStandingCard;//index is view
		QList<int>			m_heightOfWallCard;//index is view
		int					m_styleOfPoolCards;
		
		bool				c_bPrivate[5];
		bool				c_bPlayerTing[5];
		bool 				c_bGang;
		
		bool 				c_bThrowFlag,c_bHUStatus;
		quint16				c_shHuMask;
		
};

class DJ_BASE_EXPORT DJGameMahjongItem: public DJGameImageItem
{
	public:
    	DJGameMahjongItem( QImage img, Q3Canvas *cv,QMatrix &m,unsigned short tid,unsigned short sid,unsigned short type,bool bMovable);
		~DJGameMahjongItem();
		void setSelected(bool b);
		bool selected() const { return c_bSelected; }
		void move(double x, double y);
		double	x() const { return c_x; }
		double 	y() const { return c_y; }
		void setdy(int d);
	private :
		double  c_x,c_y;
		int		c_dy;
		bool 	c_bSelected;
};

#define DJGAME_MAHJONG_DESKTOP_AVATAR_MAXHEIGHT			122//100
#define DJGAME_MAHJONG_DESKTOP_AVATAR_MAXWIDTH			72//90
#endif
