#include "Stdafx.h"
#include "MTListCtrl.h"

#define COL_Read		0
#define COL_Format		1
#define COL_Storage		2
#define COL_Received	3
#define COL_From		4
#define COL_MsgBody		5

CMTListCtrl::CMTListCtrl()
{
}

CMTListCtrl::~CMTListCtrl(void)
{
}

BOOL CMTListCtrl::Initialize(CEdit* pMsgBody)
{
	if (!pMsgBody)
		return FALSE;

	// setup Sms list control column headings 
	InsertColumn(COL_Read, L"Tag", LVCFMT_CENTER, 50);
	InsertColumn(COL_Format, L"Format", LVCFMT_CENTER, 50);
	InsertColumn(COL_Storage, L"Storage", LVCFMT_CENTER, 50);
	InsertColumn(COL_Received, L"Received", LVCFMT_LEFT, 140);
	InsertColumn(COL_From, L"From", LVCFMT_LEFT, 100);
	InsertColumn(COL_MsgBody, L"Msg Body", LVCFMT_LEFT, 0);	// hidden column to cache msg body text

	SetExtendedStyle(LVS_EX_FULLROWSELECT);

	m_pMsgBody = pMsgBody;
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	GetSelection
//
//	Description	:	UI Interface to SMS engine. To obtain handle of item selected in list
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:   Supports obtaining of Handle of selected message in SMS 'Inbox', will support Drafts folder and 
//                  Sent folder in future
//
/////////////////////////////////////////////////////////////////////////////
ULONG CMTListCtrl::GetSelection()
{
	//find which row was clicked
	int iRow = GetSelectionMark();
	
    if (iRow > -1)
    {
        return GetItemData(iRow); 
    }
	
	return 0;  // no item is selected. 
}

/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	ClearAll
//
//	Description	:	UI Interface to SMS engine. To remove all messages from UI ListCtrl
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:   Supports Removing all messages from SMS 'Inbox', will support Drafts folder and 
//                  Sent folder in future
//
/////////////////////////////////////////////////////////////////////////////
void CMTListCtrl::ClearAll()
{
	DeleteAllItems();
	m_pMsgBody->SetWindowText(_T(""));
}

/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	Clear
//
//	Description	:	UI Interface to SMS engine. To remove a message from UI ListCtrl
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:   Supports Removing a message from SMS 'Inbox', will support Drafts folder and 
//                  Sent folder in future
//
/////////////////////////////////////////////////////////////////////////////
void CMTListCtrl::Clear(ULONG Handle)
{
	DeleteItem(FindMsg(Handle));
	m_pMsgBody->SetWindowText(_T(""));
}

/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	UpdateMessage
//
//	Description	:	Update status of a message in List ctrl 
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:	Check the msgtag, if it is in 'read' state, then simply unbold the selected item
//                  If msgtag is 'not read', we are here to concatenate a long msg.
//
/////////////////////////////////////////////////////////////////////////////
void CMTListCtrl::UpdateMessage(ULONG hMsgHdl, 
								 ULONG *uStorageType, 
								 ULONG *uMsgFormat, 
								 ULONG *uMsgTag, 
								 TCHAR *szFrom, 
								 TCHAR *szTime, 
								 TCHAR *szMsgBody)
{
	//find which row was clicked
	int index = FindMsg(hMsgHdl);

	if (-1 == index)
		return;

	//translate the Storage type (NV or SIM) and the MsgFormat (GSM/ CDMA) into the string equivalents
	switch (*uMsgFormat)
	{
		case 0:
			SetItemText(index, COL_Format, L"CDMA");
			break;
		case 6:
			SetItemText(index, COL_Format, L"GSM");
			break;
		default:
			SetItemText(index, COL_Format, L"Unknown");
			break;
	}
	
	switch (*uStorageType)	// SIM is 0 and NV is 1
	{
		case 0:
			SetItemText(index, COL_Storage, L"SIM");
			break;
		case 1:
			SetItemText(index, COL_Storage, L"NV");
			break;
		default:
			SetItemText(index, COL_Storage, L"Unknown");
			break;
	}

	// check the MsgTag, if set to 1, then msg not read yet so bold the list item text row
	switch (*uMsgTag)
	{
	case 0:	// Message is in 'read' state
		SetItemText(index, COL_Read, L"Read");
		break;
	case 1:	// msg is in 'not read' state 
		SetItemText(index, COL_Read, L"Unread");
		break;
	default:
		// could be in MO and sent state - 2, or MO but not yet sent - 3, or something went wrong
		break;
	}

	// Update the remaining items
	SetItemText(index, COL_From, szFrom);
	SetItemText(index, COL_Received, szTime);
	SetItemText(index, COL_MsgBody, szMsgBody);
}


/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	AddMessage
//
//	Description	:	UI Interface to SMS engine. To add a message to UI ListCtrl
//
//	Arguments	:	hMsgHdl  -  Handle assigned by calling routine
//                  uStorageType - pointer to storage type:  NV or SIM
//                  uMsgFormat - pointer to Message Format:  CDMA or GSM
//                  uMsgTag - pointer message status:  read or unread
//                  szFrom - pointer to string containing sender's phn num
//                  szTime - pointer to string containing date / time msg sent
//                  szMsgBody - pointer to string containing body of message 
//
//	Returns		:	None
//
//	Comments	:   Supports 'Inbox' for SMS messages, will support Drafts folder and 
//                  Sent folder in future
//
/////////////////////////////////////////////////////////////////////////////
void CMTListCtrl::AddMessage(ULONG hMsgHdl, 
							  ULONG *uStorageType, 
							  ULONG *uMsgFormat, 
							  ULONG *uMsgTag, 
							  TCHAR *szFrom,
							  TCHAR *szTime, 
							  TCHAR *szMsgBody)
{
	// Add item to bottom of list
	int index = GetItemCount(); 

	//Create dummy entry
	InsertItem(index, L"");	

	// Save handle
	SetItemData(index, (DWORD_PTR)hMsgHdl); 

	// Call UpdateMessage to update the rest of the fields
	UpdateMessage(hMsgHdl, uStorageType, uMsgFormat, uMsgTag, szFrom, szTime, szMsgBody);
}

void CMTListCtrl::OnUserClick()
{
	int nSelection = GetSelectionMark();

	if (nSelection > -1)
	{
		m_pMsgBody->SetWindowText(GetItemText(nSelection, COL_MsgBody));
	}
}

INT CMTListCtrl::FindMsg(ULONG hMsgHdl)
{
	for (int i=0; i<GetItemCount(); i++)
	{
		if (GetItemData(i) == hMsgHdl)
			return i;
	}

	return -1;
}