// Implementation of the CPA_Meca_ControlView class
///////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "EMECVwMn.hpp"

#include "EMECFmMn.hpp"
#include "EMECVwMS.hpp"
#include "EMECDoc.hpp"
#include "EMECCnst.hpp"
#include "EMECDgNC.hpp"
#include "EMECDgSp.hpp"
#include "EMECDgQu.hpp"
#include "_MecCard.hpp"
#include "_Minterf.hpp"

#include "help_def.h"	//For Context help

//External Modules
#include "ErO.h"
#include "IncTUT.h"
//End of External Modules

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

/////////////////////////////////////////////////////////////////////////////
// CPA_Meca_ControlView

IMPLEMENT_DYNCREATE(CPA_Meca_ControlView, CFormView)

BEGIN_MESSAGE_MAP(CPA_Meca_ControlView, CFormView)
	//{{AFX_MSG_MAP(CPA_Meca_ControlView)
	ON_BN_CLICKED(IDC_BUTTON_CREATE, OnButtonCreate)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_RENAME, OnButtonRename)
	ON_CBN_SELCHANGE(IDC_LIST_CARD_TYPES, OnSelchangeComboCardTypes)
	ON_WM_HELPINFO()
	ON_WM_SIZE()
	ON_BN_CLICKED(IDC_BUTTON_COPY, OnButtonCopy)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP

	ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTipNotify )

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPA_Meca_ControlView construction/destruction

//***************************************************************************
CPA_Meca_ControlView::CPA_Meca_ControlView() : CFormView( IDD_MECA_DIALOG_CONTROL )
{

}

//***************************************************************************
CPA_Meca_ControlView::~CPA_Meca_ControlView()
{

}

/////////////////////////////////////////////////////////////////////////////
// CPA_Meca_ControlView message handlers

//***************************************************************************
void CPA_Meca_ControlView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();

	EnableToolTips(TRUE);

	m_fn_pclGetDocument()->m_pclControlView = this;

	CRect crClientRect;
	GetClientRect(crClientRect);
	CRect crLastRect;
	GetDlgItem(IDC_LIST_CARD_TYPES)->GetClientRect(crLastRect);
	crClientRect.bottom = crLastRect.bottom + 5;
	crClientRect.right = crLastRect.right + 5;
	MoveWindow(crClientRect);
	
	SetScrollSizes(MM_TEXT, CSize(crClientRect.Width() - 5, crClientRect.Height() - 5));

	//Sets Icons for Butttons
	HICON hIcon = AfxGetApp()->LoadIcon(IDI_ICON_BUTTON_CREATE);
	((CButton *)GetDlgItem(IDC_BUTTON_CREATE))->SetIcon(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_BUTTON_RENAME);
	((CButton *)GetDlgItem(IDC_BUTTON_RENAME))->SetIcon(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_BUTTON_DELETE);
	((CButton *)GetDlgItem(IDC_BUTTON_DELETE))->SetIcon(hIcon);
	hIcon = AfxGetApp()->LoadIcon(IDI_ICON_BUTTON_COPY);
	((CButton *)GetDlgItem(IDC_BUTTON_COPY))->SetIcon(hIcon);

	//Fills the list of cards types
	CListBox *pclTypeLB = (CListBox *)GetDlgItem(IDC_LIST_CARD_TYPES);
	pclTypeLB->ResetContent();

	CPA_Meca_CardType *pclCardType;
	short wIndex = 0;
	POSITION pos = m_fn_pclGetDocument()->m_clCardTypeList.GetHeadPosition();
	while ( pos != NULL )
	{
		pclCardType = m_fn_pclGetDocument()->m_clCardTypeList.GetNext(pos);

		wIndex = pclTypeLB->AddString(pclCardType->m_fn_csGetCardTypeName());
		pclTypeLB->SetItemDataPtr(wIndex, (void *)pclCardType);
	}

	//////////////
	//////////////
	//Registers for TUT Module
	TUT_M_vGetTutDll();
	
	TUT_M_vRegisterControlID(IDC_BUTTON_CREATE, "TMe_ControlView_CreateButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_RENAME, "TMe_ControlView_RenameButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_DELETE, "TMe_ControlView_DeleteButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_COPY, "TMe_ControlView_CopyButton", TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_LIST_CARD_TYPES, "TMe_ControlView_ListBox", TUT_e_ListBox);

	//End of Registers for TUT Module
	//////////////
}

//*******************************************************************************
void CPA_Meca_ControlView::OnDestroy() 
{
	//////////////
	//////////////
	//UnRegisters for TUT Module
	TUT_M_vGetTutDll();
	
	TUT_M_vUnregisterControlID(IDC_BUTTON_CREATE);
	TUT_M_vUnregisterControlID(IDC_BUTTON_RENAME);
	TUT_M_vUnregisterControlID(IDC_BUTTON_DELETE);
	TUT_M_vUnregisterControlID(IDC_BUTTON_COPY);
	TUT_M_vUnregisterControlID(IDC_LIST_CARD_TYPES);
	
	//End of UnRegisters for TUT Module
	//////////////

	CFormView::OnDestroy();
}

//*******************************************************************************
void CPA_Meca_ControlView::OnButtonCreate() 
{
	if ( m_fn_pclGetDocument()->m_pclCurrentCardType != NULL )
	{
		CString csNewCardProposedName = m_fn_pclGetDocument()->m_pclCurrentCardType->m_fn_csGetCardTypeName();
		//Erases 's' at the End
		if ( csNewCardProposedName.Right(1).CompareNoCase("s") == 0 )
			csNewCardProposedName = csNewCardProposedName.Left(csNewCardProposedName.GetLength() - 1);
		CString csTrueNewCardProposedName;
		csTrueNewCardProposedName.Format("%s_%i", csNewCardProposedName,
																				m_fn_pclGetDocument()->m_pclCurrentCardType->m_fn_ulGetCardsNumber() + 1);

		m_fn_pclGetDocument()->m_fn_vCheckSpacesInString(csTrueNewCardProposedName);
		
		CPA_Meca_NewCardDialog dial(csTrueNewCardProposedName, this);					

		if ( dial.DoModal() == IDOK )
		{
			m_fn_pclGetDocument()->m_fn_vCheckSpacesInString(dial.m_csName);

			//Adds to Card Type's list of Cards
			CPA_Meca_Card *pclNewCard = m_fn_pclGetDocument()->m_pclCurrentCardType->m_fn_pclGetCardsList()->m_fn_pclAddElement(dial.m_csName, 
									m_fn_pclGetDocument()->m_pclCurrentCardType,
									NULL,
									m_fn_pclGetDocument()->m_pclCurrentCardType->m_fn_csGetFileName(),
									m_fn_pclGetDocument()->m_pclCurrentCardType->m_fn_csGetActionName());

			//Adds to the Main list
			m_fn_pclGetDocument()->m_clListOfCards.fn_bAddObject(pclNewCard);

			//Sets the text for the number of Cards
			m_fn_vUpdateCardsNumber(pclNewCard->m_fn_pclGetCardType());

			//Updates Dialog List
			m_fn_pclGetDocument()->m_pclDialogList->fn_vUpdateSelection(E_lrm_ReinitList);

			//Edits new Card
			m_fn_pclGetDocument()->m_fn_vEditCard(pclNewCard);
		}
	}
}

//*******************************************************************************
// Deletes a Card
void CPA_Meca_ControlView::OnButtonDelete() 
{
	CPA_Meca_Card *pclCurrentCard = m_fn_pclGetDocument()->m_pclCurrentCard;
	CPA_Meca_CardType *pclCurrentCardType = pclCurrentCard->m_fn_pclGetCardType();

	ERROR_ASSERT( pclCurrentCard != NULL );
	ERROR_ASSERT( pclCurrentCardType != NULL );

	CString csQuestion = "BE CAREFUL !\nYou are going to destroy a Mechanical Card !";
	csQuestion += "Name is '" + pclCurrentCard->GetName() + "', of type '" + pclCurrentCardType->m_fn_csGetCardTypeName() + "'\n";
	csQuestion += "Do you really want to continue ?";
	CPA_Meca_QuestionDialog dial(this, csQuestion);

	if ( dial.DoModal() == IDYES )
	{
		//Adds to the Main list
		m_fn_pclGetDocument()->m_clListOfCards.fn_bRemoveObject(pclCurrentCard);

		//Destroys the Card ...
		//...from the list
		m_fn_pclGetDocument()->m_fn_vRemoveCard(pclCurrentCard);


		//Sets the text for the number of Cards
		m_fn_vUpdateCardsNumber(pclCurrentCardType);

		//Updates Dialog List
		m_fn_pclGetDocument()->m_pclDialogList->fn_vUpdateSelection(E_lrm_ReinitList);
	}
}

//*******************************************************************************
void CPA_Meca_ControlView::OnButtonRename() 
{
	CPA_Meca_NewCardDialog dial(m_fn_pclGetDocument()->m_pclCurrentCard->m_fn_csGetCardName(),
															this);					

	if ( dial.DoModal() == IDOK )
	{
		m_fn_pclGetDocument()->m_pclCurrentCard->fn_eRename(dial.m_csName);

		//Updates Dialog List
		m_fn_pclGetDocument()->m_pclDialogList->fn_vUpdateSelection(E_lrm_ReinitList);
	}
}

//*******************************************************************************
void CPA_Meca_ControlView::OnButtonCopy() 
{
	//Creates a copy of the currently selected Card
	if ( m_fn_pclGetDocument()->m_pclCurrentCard != NULL )
	{
		CPA_Meca_Card *pclCopyCard = m_fn_pclGetDocument()->m_pub_fn_vCreateCopyOfCard(m_fn_pclGetDocument()->m_pclCurrentCard);

		if (pclCopyCard == NULL)
			return;
		//ERROR_ASSERT( pclCopyCard != NULL );

		//Adds to the Main list
		m_fn_pclGetDocument()->m_clListOfCards.fn_bAddObject(pclCopyCard);

		//Sets the text for the number of Cards
		m_fn_vUpdateCardsNumber(pclCopyCard->m_fn_pclGetCardType());

		//Updates Dialog List
		m_fn_pclGetDocument()->m_pclDialogList->fn_vUpdateSelection(E_lrm_ReinitList);

		//Edits new Card
		m_fn_pclGetDocument()->m_fn_vEditCard(pclCopyCard);
	}
}

//*******************************************************************************
void CPA_Meca_ControlView::OnSelchangeComboCardTypes() 
{
	//Gets the new selected type		
	CPA_Meca_CardType *pclCardType = NULL;
	CListBox *pclTypeLB = (CListBox *)GetDlgItem(IDC_LIST_CARD_TYPES);
	short wIndex = pclTypeLB->GetCurSel();
	if ( wIndex != CB_ERR )
	{
		pclCardType = (CPA_Meca_CardType *)pclTypeLB->GetItemDataPtr(wIndex);

		//Sets document's pointer
		m_fn_pclGetDocument()->m_fn_vDisplayCardType(pclCardType);

		//Sets the text for the number of Cards
		m_fn_vUpdateCardsNumber(pclCardType);
		
		//Sets document's pointer
		if ( pclCardType->m_fn_pclGetEditedCard() != NULL )
			m_fn_pclGetDocument()->m_fn_vEditCard(pclCardType->m_fn_pclGetEditedCard());
	}

	//Refreshes controls
	((CButton *)GetDlgItem(IDC_BUTTON_CREATE))->EnableWindow( wIndex != CB_ERR );

	//Updates Dialog List
	if ( pclCardType != NULL )
	{
		CString csType = "Cards of type '" + pclCardType->m_fn_csGetCardTypeName() + "'";
		m_fn_pclGetDocument()->m_pclDialogList->fn_vInitButtonTest(	csType,
																																TRUE,
																																TRUE);

		m_fn_pclGetDocument()->m_pclDialogList->fn_vUpdateSelection(E_lrm_ReinitList);
	}
}

//*******************************************************************************
void CPA_Meca_ControlView::m_fn_vUpdateCardsNumber(CPA_Meca_CardType *_pclCardType)
{
	if ( _pclCardType != NULL )
	{
		CString csNumberOfCards;
		CString csCard = (_pclCardType->m_fn_pclGetCardsList()->GetCount() > 1) ? "Cards" : "Card";
		csNumberOfCards.Format("%i %s", _pclCardType->m_fn_pclGetCardsList()->GetCount(),
										LPCTSTR(csCard));
		
		GetDlgItem(IDC_STATIC_CARD_NUMBER)->SetWindowText(csNumberOfCards);
	}
}

//*******************************************************************************
void CPA_Meca_ControlView::m_fn_vUpdateCardsName()
{
	if ( m_fn_pclGetDocument()->m_pclCurrentCard != NULL )
	{
		//Displays Card's Name in View's title bar
		CString csCardName = m_fn_pclGetDocument()->m_pclCurrentCard->GetName();

		if ( m_fn_pclGetDocument()->m_pclCurrentCard->m_pub_fn_bIsNotified() )
			csCardName += " (** Modified **)";

		GetDlgItem(IDC_TEXT_CARD_NAME)->SetWindowText(csCardName);
	}
}

//*******************************************************************************
CPA_Meca_MyDocument* CPA_Meca_ControlView::m_fn_pclGetDocument()
{
	return g_pclInterface->m_fn_pclGetDocument();
}

//*******************************************************************************
BOOL CPA_Meca_ControlView::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	CString csFileAndWindow = g_pclInterface->m_clDocument.m_csHelpFileNameAndPath;

	::WinHelp(m_hWnd,
						LPCTSTR(csFileAndWindow),
						HELP_CONTEXT,
						IDH_GENERAL);

	return FALSE;
}

//*************************************************************************
//Definition of texts for Tool Tips
BOOL CPA_Meca_ControlView::OnToolTipNotify( UINT id, NMHDR * pNMHDR, LRESULT * pResult )
{
	TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
	UINT nID =pNMHDR->idFrom;
	nID = ::GetDlgCtrlID((HWND)nID);
    
	BOOL bReturn = FALSE;

	switch ( nID )
	{
	case IDC_BUTTON_CREATE:
 		strcpy(pTTT->szText,"Creates a Mechanics Card");
		break;
	case IDC_BUTTON_RENAME:
 		strcpy(pTTT->szText,"Renames the current Card");
		break;
	case IDC_BUTTON_DELETE:
 		strcpy(pTTT->szText,"Deletes the current Card");
		break;
	case IDC_BUTTON_COPY:
 		strcpy(pTTT->szText,"Creates a copy of the current Card");
		break;
	case IDC_LIST_CARD_TYPES:
		strcpy(pTTT->szText,"All types of Mechanics Card");
		break;
	case IDC_STATIC_CARD_NUMBER:
		strcpy(pTTT->szText,"Number of Cards for the current Type");
		break;
	default:
 		strcpy(pTTT->szText,"");
		break;
	};

	return (TRUE);
}

#define EdMeca_C_ControlsSpacing	2
#define EdMeca_C_EditHeight				15

//*************************************************************************
void CPA_Meca_ControlView::OnSize(UINT nType, int cx, int cy) 
{
	CFormView::OnSize(nType, cx, cy);
	
	//Adjusts controls
	CRect crCurrentRect;
	long lBottom;

	//ListBox
	if ( GetDlgItem(IDC_LIST_CARD_TYPES) != NULL )
	{
		GetDlgItem(IDC_LIST_CARD_TYPES)->GetWindowRect(crCurrentRect);
		ScreenToClient(crCurrentRect);
		lBottom = cy - EdMeca_C_EditHeight - 2*EdMeca_C_ControlsSpacing;
		crCurrentRect.bottom = lBottom;
		GetDlgItem(IDC_LIST_CARD_TYPES)->MoveWindow(crCurrentRect);
	}

	//Edit (for the current Card's name)
	if ( GetDlgItem(IDC_TEXT_CARD_NAME) != NULL )
	{
		GetDlgItem(IDC_TEXT_CARD_NAME)->GetWindowRect(crCurrentRect);
		ScreenToClient(crCurrentRect);
		crCurrentRect.top = lBottom + EdMeca_C_ControlsSpacing;
		crCurrentRect.bottom = crCurrentRect.top + EdMeca_C_EditHeight;
		GetDlgItem(IDC_TEXT_CARD_NAME)->MoveWindow(crCurrentRect);
	}
}

//*******************************************************************************
//*******************************************************************************
/////////////////////////////////////////////////////////////////////////////
// CPA_Meca_ControlView diagnostics

#ifdef _DEBUG
void CPA_Meca_ControlView::AssertValid() const
{
	CFormView::AssertValid();
}

void CPA_Meca_ControlView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG


