/******************************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. All rights 
** reserved.
**
** File   : SelectCmd.cpp
**
** Author : Edwin Chan (with help from MFC)
**
** Description:
**    This file provides implementation for CSelectCmd class.
**
** History:
** 24-Nov-01 Edwin Chan
**     File Created, separated from SelectCmd.cpp
** 26-Nov-01 Edwin Chan
**     *Added method OnEditProperties
**     *Updated methods
**     *Added support for SHIFT key when select objects
** 27-Nov-01 Edwin Chan
**     Updated LButtonDown
** 28-Nov-01 Satish Hota
**     Made the CDrawable class CDrawableElement and created a CDrawable
**     super-class.  Hence, replaced usage of CDrawable with CDrawableElement.
** 30-Nov-01 Satish Hota
**     Calling ObjectAt on the view rather than the document in OnLButtonDown.
******************************************************************************/

#include "stdafx.h"
#include "ConGraT.h"
#include "SelectCmd.h"
#include "DrawableElement.h"
#include "ConGraTDoc.h"
#include "ConGraTView.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSelectCmd::CSelectCmd()
{

}

CSelectCmd::~CSelectCmd()
{

}



void CSelectCmd::OnLButtonDown(CConGraTView *pView, UINT nFlags, const CPoint &point)
{
	CPoint local = point;
	CDrawableElement *pObj;
	m_eSelectMode = SelectMode_None;

    CMFCDevConHelper devConHelper((CView *)pView, NULL);
    CDrawableElement::SetDevConHelper(&devConHelper);

	if (m_eSelectMode == SelectMode_None)
	{
		pObj = pView->ObjectAt(local);

		if (pObj != NULL)
		{
			m_eSelectMode = SelectMode_Move;

            // check if the pObj is selected, if not, selected it.
			// if with SHIFT, keep the selected object
			if (!pView->IsSelected(pObj))
				pView->Select(pObj, (nFlags & MK_SHIFT) != 0);
		}
        else 
        {
            // no object at "point" == unselect
            pView->Select(NULL, false);
        }
	}

	CDrawableElement::SetDevConHelper(NULL);
	CCmdTool::OnLButtonDown(pView, nFlags, point); 
}

void CSelectCmd::OnLButtonUp(CConGraTView *pView, UINT nFlags, const CPoint &point)
{
	/*
	if (pView->GetCapture() == pView)
	{
		if (m_eSelectMode != SelectMode_None)
		{
			pView->GetDocument()->UpdateAllViews(pView);
		}
	}
	*/

    CCmdTool::OnLButtonUp(pView, nFlags, point);
}

void CSelectCmd::OnMouseMove(CConGraTView *pView, UINT nFlags, const CPoint &point)
{
	if (pView->GetCapture() != pView)
	{
		CCmdTool::OnMouseMove(pView, nFlags, point);
		return;
	}

	CPoint local = point;
	CPoint delta = (CPoint)(local - m_LastPoint);
    
    CMFCDevConHelper devConHelper((CView *)pView, NULL);

    CDrawableElement::SetDevConHelper(&devConHelper);

    std::vector<CDrawableElement*>::iterator vi;

    VDrawableElement vSelect = pView->GetSelectSet();

	for(vi=vSelect.begin();vi!=vSelect.end();vi++)
	{
        CDrawableElement* pObj = *vi;

		if (m_eSelectMode == SelectMode_Move)
		{
			pObj->TranslateBy(delta.x, delta.y);
		}
	}

	CDrawableElement::SetDevConHelper(NULL);

    CCmdTool::OnMouseMove(pView, nFlags, point);
}

void CSelectCmd::OnCancel()
{

}

void CSelectCmd::OnEditProperties(CConGraTView *pView)
{
    VDrawableElement vSelect = pView->GetSelectSet();
	CDrawableElement *pObj = *(vSelect.begin());
	//check the type of object, return if
	//the object is a relation type
    CmdType eCmd = pView->GetCurrentCmd();
	pObj->OnEditProperties(eCmd);
}
