#include "calendarentityitem.h"

#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsLinearLayout>
#include <QGraphicsDropShadowEffect>

//#include "calendartableview.h"

CalendarEntityItem::CalendarEntityItem(QString strTitle, QString strLocation,
					QString strColor, QPixmap pixIcon, QString strOwner, QString strResource, QString strContact, QString strProjects, 
					QString strDescription, QHash<int, qreal> hshBrakeStart, QHash<int, qreal> hshBrakeEnd, int nBcep_Status, 
					int nBCEV_IS_INFORMATION, bool bIsProposed, QGraphicsView *pCalendarGraphicsView)
					: QGraphicsProxyWidget()
{
	m_nBcep_Status			= nBcep_Status;
	m_nBCEV_IS_INFORMATION	= nBCEV_IS_INFORMATION;
	m_bIsProposed		= bIsProposed;
	m_nWidthFactor			= 1;
	m_nColumnCount			= 1;
	m_nHorizOrder			= 0;
	m_nHorizPos				= 0;
	m_isHResizing			= false;
	m_isVResizing			= false;
	m_isDResizing			= false;
	m_bIsMoving				= false;
	m_pCalendarGraphicsView	= pCalendarGraphicsView;
	//setToolTip(strTitle);

	//QString s = startDateTime.toString();
	//QString ss = endDateTime.toString();

	m_DisplayWidget = new CalendarEntityDisplayWidget(strTitle, strLocation, strColor, pixIcon, strOwner, strResource, strContact, 
							strProjects, strDescription, hshBrakeStart, hshBrakeEnd, m_pCalendarGraphicsView, m_nBcep_Status, 
							m_nBCEV_IS_INFORMATION, m_bIsProposed);
	setWidget(m_DisplayWidget);
	setMinimumWidth(0);
	setMinimumHeight(0);
	setZValue(2);
	setAcceptHoverEvents(true);
	setFlag(QGraphicsItem::ItemIsSelectable);
	CreateContextMenuActions(m_lstActions);

	QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect();
	effect->setOffset(3,3);
	effect->setBlurRadius(3);
	setGraphicsEffect(effect);

//	if (nBCEV_IS_INFORMATION)
//		setOpacity(0.6);
}

CalendarEntityItem::~CalendarEntityItem()
{
	m_DisplayWidget = NULL;
}

void CalendarEntityItem::setHorFactor(qreal nWidthFactor)
{
	m_nWidthFactor	= nWidthFactor;
}

void CalendarEntityItem::setHorOrder(qreal nHorizOrder)
{
	m_nHorizOrder	= nHorizOrder;
}
void CalendarEntityItem::setHorPos(qreal nHorizPos)
{
	m_nHorizPos = nHorizPos;
}

/*
void CalendarEntityItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	QGraphicsProxyWidget::paint(painter, option, widget);
}
*/
void CalendarEntityItem::getStartEndDateTime(QDateTime	&startDateTime, QDateTime &endDateTime)
{
	startDateTime	= m_startDateTime;
	endDateTime		= m_endDateTime;
}

void CalendarEntityItem::SetSelected(bool bSelected /*= true*/)
{
	m_DisplayWidget->SetSelected(bSelected);
}

void CalendarEntityItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
	CalendarEntityItem::ResizeType type = isInResizeArea(event->pos());
	if (type == HORIZ_RESIZE)
		setCursor(Qt::SizeHorCursor);
	else if (type == VERT_RESIZE)
		setCursor(Qt::SizeVerCursor);
	else if (type == DIAG_RESIZE)
		setCursor(Qt::SizeFDiagCursor);
	else if (isInMoveArea(event->pos()))
		setCursor(Qt::OpenHandCursor);
	else
		setCursor(Qt::PointingHandCursor);
	
	QGraphicsProxyWidget::hoverMoveEvent(event);
}

void CalendarEntityItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
	if (m_isHResizing)
	{
		m_nWidth = event->pos().x();
		resize(m_nWidth, m_nHeight);
		emit ItemResizing(true);
		return;
	}
	else if(m_isVResizing)
	{
		m_nHeight = event->pos().y();
		resize(m_nWidth, m_nHeight);
		emit ItemResizing(true);
		return;
	}
	else if (m_isDResizing)
	{
		m_nWidth = event->pos().x();
		m_nHeight = event->pos().y();
		resize(m_nWidth, m_nHeight);
		emit ItemResizing(true);
		return;
	}
	else if (m_bIsMoving)
	{
		//QGraphicsProxyWidget::mouseMoveEvent(event);
		//QGraphicsItem::mouseMoveEvent(event);
		//setPos(event->pos());
		//dynamic_cast<CalendarGraphicsView*>(m_pCalendarGraphicsView)->CheckForMultipleItemsInColumn(this);
		/*
		QPointF mousePos = event->pos();
		QPointF scenePos = mapToScene(mousePos);
		QPointF tablePos = m_pCalendarGraphicsView->getTableView()->mapFromScene(scenePos);
		
		QModelIndex topLeftIndex = dynamic_cast<CalendarTableView*>(dynamic_cast<CalendarGraphicsView*>(m_pCalendarGraphicsView)->getTableView())->Table()->indexAt(tablePos.toPoint());
		setPos(scenePos);

		QItemSelectionModel *selModel = dynamic_cast<CalendarTableView*>(dynamic_cast<CalendarGraphicsView*>(m_pCalendarGraphicsView)->getTableView())->Table()->selectionModel();
		QItemSelection selection(topLeftIndex, topLeftIndex);
		selModel->clearSelection();
		selModel->select(selection, QItemSelectionModel::Select);
		*/
	}

	QGraphicsProxyWidget::mouseMoveEvent(event);
}

void CalendarEntityItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
	if (event->button() == Qt::LeftButton)
	{
		CalendarEntityItem::ResizeType type = isInResizeArea(event->pos());
		if (type == HORIZ_RESIZE)
		{
			m_isHResizing = true;
		}
		else if (type == VERT_RESIZE)
		{
			m_isVResizing = true;
		}
		else if (type == DIAG_RESIZE)
		{
			m_isDResizing = true;
		}
		else if (isInMoveArea(event->pos()))
		{
			//QItemSelectionModel *selModel = dynamic_cast<CalendarTableView*>(dynamic_cast<CalendarGraphicsView*>(m_pCalendarGraphicsView)->getTableView())->Table()->selectionModel();
			//selModel->clearSelection();

			setCursor(Qt::ClosedHandCursor);
			m_bIsMoving = true;
			//QGraphicsProxyWidget::mousePressEvent(event);
		}
	}
	else
		QGraphicsProxyWidget::mousePressEvent(event);
}

void CalendarEntityItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
	if (event->button() == Qt::LeftButton && (m_isDResizing || m_isHResizing || m_isVResizing))
	{
		m_isVResizing = false;
		m_isHResizing = false;
		m_isDResizing = false;
		emit ItemResized(this);
		return;
	}
	else if (m_bIsMoving)
	{
		m_bIsMoving = false;
		emit ItemMoved(this);
	}
	else if ((event->button() == Qt::LeftButton) && isSelected())
	{
		setSelected(false);
		m_DisplayWidget->SetSelected(false);
		update();
	}
	else if((event->button() == Qt::LeftButton) && !isSelected())
	{
		setSelected(true);
		m_DisplayWidget->SetSelected(true);
		update();
	}
	else
	{
		QGraphicsProxyWidget::mouseReleaseEvent(event);
	}
}

void CalendarEntityItem::keyPressEvent(QKeyEvent *event)
{
	if ((event->key() == Qt::Key_Escape) && m_bIsMoving)
	{
		m_bIsMoving = false;
		setCursor(Qt::ArrowCursor);
	}
}

void CalendarEntityItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
	if(m_lstActions.size()==0) 
	{
		event->ignore();	
		return;
	}

	QMenu CnxtMenu;

	int nSize=m_lstActions.size();
	for(int i=0;i<nSize;++i)
		CnxtMenu.addAction(m_lstActions.at(i));

	CnxtMenu.exec(event->screenPos());
}

void CalendarEntityItem::On_CnxtModify()
{
	emit ItemModify(this);
}
void CalendarEntityItem::On_CnxtDelete()
{
	emit ItemDelete(this);
}

CalendarEntityItem::ResizeType CalendarEntityItem::isInResizeArea(const QPointF &pos)
{
	int nWidth = this->widget()->width();
	int nHeight = this->widget()->height();
	
	if (((nWidth - 5) < pos.x()) && (pos.x() < nWidth))
	{
		if (((nHeight- 10) < pos.y()) && (pos.y() < nHeight))
			return DIAG_RESIZE;
		else
			return HORIZ_RESIZE;
	}
	else if (((nHeight- 5) < pos.y()) && (pos.y() < nHeight))
	{
		if (((nWidth - 10) < pos.x()) && (pos.x() < nWidth))
			return DIAG_RESIZE;
		else
			return VERT_RESIZE;
	}
	else
		return NO_RESIZE;
}

bool CalendarEntityItem::isInMoveArea(const QPointF &pos)
{
	if(pos.y()>0 && pos.y()<20)
		return true;
	else
		return false;
}

void CalendarEntityItem::CreateContextMenuActions(QList<QAction*> &lstActions)
{
	QAction* pAction = new QAction(tr("Modify"), this);
	connect(pAction, SIGNAL(triggered()), this, SLOT(On_CnxtModify()));
	lstActions.append(pAction);

	pAction = new QAction(tr("Delete"), this);
	connect(pAction, SIGNAL(triggered()), this, SLOT(On_CnxtDelete()));
	lstActions.append(pAction);


}

void CalendarEntityItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * event)
{
	On_CnxtModify();
}
