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

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

class DJCanvasView;
class DJGamePanel;
class DJGameUser;
class DJCanvasItem;
class DJCanvasTable;
class DJCanvasAvatar;
class DJCanvasText;
class DJGameController;

class DJ_BASE_EXPORT DJCell : public QObject
{
	Q_OBJECT
public:
	DJCell( quint8 tableId, const QPixmap& tablePixmap, const QFont& tableFont, const QColor& tableColor,
		const QList<QPixmap>& seatPixmaps, const QPixmap& playingPixmap, DJCanvasView* canvasView );
	virtual ~DJCell();

	void draw();
	//void arrange();
	void synchronize( quint8 tableStatus, const QList<DJGameUser*>& tableMapUsers = QList<DJGameUser*>() );
	bool isPlaying() const;
	bool isIdle() const;
	quint8 tableId() const;
	quint8 numberOfSeats() const;
	void addUser( DJGameUser* user );
	void removeUser( DJGameUser* user );
	DJGameUser* userAtSeat( quint8 seatId );
	void setStatus( quint8 tableStatus );
	DJGamePanel*	gamePanel() { return m_gamePanel; }
	void setGamePanel( DJGamePanel* gamePanel );
	void deleteGamePanel();
	QList<quint32> panelUserIds();
	//QList<quint8> playingSeatIds();
	QList<DJGameUser*> orderedUsers() const { return m_orderedUsers; }
	bool precede( quint32 firstUserId, quint32 secondUserId );
	void description() const;
private:	
	DJCanvasTable*			m_tableItem;
	QList<DJCanvasItem*>	m_seatItems;
	DJCanvasItem*			m_playingItem;
	QList<DJGameUser*>		m_orderedUsers;
	DJGamePanel*			m_gamePanel;
	DJCanvasView*			m_canvasView;
};
///////////////////////////////////////////////////////////////////////
class DJ_BASE_EXPORT DJPlayer : public QObject
{
	Q_OBJECT
public:
	DJPlayer( DJGameUser* user, const QPixmap& readyPixmap,
		const QFont& nameFont, const QColor& nameColor, DJCanvasView* canvasView );
	virtual ~DJPlayer();

	void reload();
public slots:
	void draw();
private:
	DJGameUser* 			m_user;
	DJCanvasAvatar*			m_avatarItem;
	DJCanvasText*			m_nameItem;
	DJCanvasView*			m_canvasView;
};
///////////////////////////////////////////////////////////////////////
class DJ_BASE_EXPORT DJCanvasView : public Q3CanvasView
{
    Q_OBJECT 
public:
    DJCanvasView( DJGameController* gameController, QWidget* parent );
    virtual ~DJCanvasView();

	void clearAllItems();
	DJCell* cell( quint8 tableIndex ) const;
	QList<DJGamePanel*> allPanels() const;
	QList<quint8>allPanelTables() const;
	void initialize( quint8 numberOfTables, const QPixmap& tablePixmap, const QFont& tableFont, const QColor& tableColor,
			const QList<QPixmap>& seatPixmaps, const QPixmap& selfReadyPixmap, const QPixmap& playingPixmap,
			const QSize& cellSize, const QList<QPoint>& seatOffsets, const QList<QPoint>& avatarOffsets );
	
	void addUser( DJGameUser* user, const QPixmap& readyPixmap,
				const QFont& nameFont, const QColor& nameColor );
	void removeUser( DJGameUser* user );
	
	DJGameController*	gameController() const { return m_gameController; }
	DJCanvasItem* selfReadyItem() { return m_selfReadyItem; }
	Q3CanvasRectangle* focusRectItem() { return m_focusRectItem; }
	const QSize& cellSize() const { return m_cellSize; }
	const QList<QPoint>& cellOrigins() const { return m_cellOrigins; }
	const QList<QPoint>& seatOffsets() const { return m_seatOffsets; }
	const QList<QPoint>& avatarOffsets() const { return m_avatarOffsets; }
	
	//void requestTableMap( quint8 minTableId, quint8 maxTableId );
	//void requestUserInfo( quint32 userId );
	
signals:
	void sitDown( quint8 tableId, quint8 seatId );
	void standUp();
	void ready();
	void leftClicked( DJGameUser* user );
	void rightClicked( DJGameUser* user );
	void leftDoubleClicked( DJGameUser* user );
	void viewScaleChanged( qreal scale );
	
public slots:
	void reloadUser( DJGameUser* user );
	void setScale( qreal scale );
	void setFocusTable( quint8 tableId, bool drawRect = true );
protected:   
	virtual void resizeEvent( QResizeEvent * event );
	virtual void contentsMousePressEvent( QMouseEvent * e );
	virtual void contentsMouseDoubleClickEvent( QMouseEvent * e );
	//virtual void contentsMouseReleaseEvent( QMouseEvent * e );
	//virtual void contentsMouseMoveEvent ( QMouseEvent * e );
private slots:
	void changeContents( DJGameUser* user, quint8 oldTableId = 0, quint8 oldSeatId = 0, quint8 oldGameStatus = 0 );
	void zoomIn();
	void zoomOut();
	void revert();
private:
	void popupCanvasContextMenu();
	
	QList<DJCell*>					m_cells;
	QHash<DJGameUser*,DJPlayer*>	m_players;
	
	DJCanvasItem					*m_selfReadyItem;
	Q3CanvasRectangle				*m_focusRectItem;
	QSize							m_cellSize;
	QList<QPoint>					m_cellOrigins;
	QList<QPoint>					m_seatOffsets;
	QList<QPoint>					m_avatarOffsets;
	
	QMenu*							m_canvasContextMenu;
	QAction*						m_actZoomIn;
	QAction*						m_actRevert;
	QAction*						m_actZoomOut;
	
	DJGameController*				m_gameController;
};

#endif
