//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//	$Revision: 2 $																//
//																				//
//	Name       : SwiSMSSampleDlg.cpp											//
//                                                                              //
//	Purpose    : Main dialog implementation for SwiSMSSampleDlg					//
//																				//
//				 This sample demonstrates sending and receiving SMS Messages	//
//																				//
//	Copyright (c) 2002-2003 Sierra Wireless, Inc.  All rights reserved	 		//
//																				//
//////////////////////////////////////////////////////////////////////////////////
 
#include "stdafx.h" 
#include <GobiAPI.h>
#include "SMSSample.h"
#include "SMSSampleDlg.h"

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

#define MAX_PHN_NUM_SIZE 15
#define MAX_MSG_SIZE 200
#define MAX_TIME_SIZE 25

// global pointer to self
CSMSSampleDlg* g_This = NULL;

#define TIMER_DeviceSearch				WM_USER + 0x1000
#define WM_USER_NOTIFY_DEVICE_REMOVAL	WM_USER + 0x1001
#define WM_USER_NOTIFY_NEW_SMS          WM_USER + 0x1002


/////////////////////////////////////////////////////////////////////////////
// CSMSSampleDlg dialog

CSMSSampleDlg::CSMSSampleDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSMSSampleDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSMSSampleDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CSMSSampleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSMSSampleDlg)
	DDX_Control(pDX, IDC_SENDSMS, m_btnSend);
	DDX_Control(pDX, IDC_MESSAGEBODY, m_editMesgBody);
	DDX_Control(pDX, IDC_DELETE_SMS, m_btnDelete);
	DDX_Control(pDX, IDC_DELETEALL_SMS, m_btnDeleteAll);
	DDX_Control(pDX, IDC_CONTEXT, m_editStatus);
	DDX_Control(pDX, IDC_SMSINBOXLIST, m_InboxUI);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSMSSampleDlg, CDialog)
	//{{AFX_MSG_MAP(CSMSSampleDlg)
	ON_BN_CLICKED(IDC_SENDSMS, OnSendsms)
	ON_NOTIFY(NM_CLICK, IDC_SMSINBOXLIST, OnClickSmsinboxlist)
	ON_BN_CLICKED(IDC_DELETEALL_SMS, OnDeleteAllSms)
	ON_BN_CLICKED(IDC_DELETE_SMS, OnDeleteSms)
	//}}AFX_MSG_MAP

	ON_WM_TIMER()
	ON_MESSAGE(WM_USER_NOTIFY_DEVICE_REMOVAL, OnNotifDeviceRemoval)
    ON_MESSAGE(WM_USER_NOTIFY_NEW_SMS, OnNotifNewSMS)
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CSMSSampleDlg message handlers

BOOL CSMSSampleDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

    // cache global pointer to self
	g_This = this;	
    SetTimer(TIMER_DeviceSearch, 1000, NULL);
	CenterWindow(GetDesktopWindow());	// center to the hpc screen
    m_InboxUI.Initialize(&m_editMesgBody);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// Search for available device
BOOL CSMSSampleDlg::DeviceSearch()
{
	// Attempt to connect to first available device
	if (!QCWWANConnect())
	{
        m_editStatus.SetWindowText(_T("Connected to Device"));
        m_btnSend.EnableWindow(TRUE);
        m_btnDelete.EnableWindow(TRUE); 
        m_btnDeleteAll.EnableWindow(TRUE); 
        UpdateData(TRUE);

		m_SmsEngine.StartupEngine(&m_InboxUI);
		return TRUE;
	}
	
	return FALSE;
}


/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	OnCancel
//
//	Description	:	Handler for IDCANCEL
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:	
//
/////////////////////////////////////////////////////////////////////////////
void CSMSSampleDlg::OnCancel() 
{
	// clear callback handlers
	SwiSetDeviceRemovalCallback(NULL);
    SetNewSMSCallback(NULL);

	// disconnect from device
	QCWWANDisconnect();
    g_This = NULL;
	
	CDialog::OnCancel();
}

/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	OnOK
//
//	Description	:	Handler for IDOK
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:	
//
/////////////////////////////////////////////////////////////////////////////
void CSMSSampleDlg::OnOK() 
{
	OnCancel();
}

// Session state callback handler
static void GOBICALLBACK DeviceRemovalCallback(ULONG nStatus)
{
	g_This->PostMessage(WM_USER_NOTIFY_DEVICE_REMOVAL, (WPARAM)nStatus, NULL);
}


static void GOBICALLBACK NewSmsCallback(ULONG nStorageType, ULONG nMsgIndex)
{
	g_This->PostMessage(WM_USER_NOTIFY_NEW_SMS, (WPARAM)nStorageType, (LPARAM)nMsgIndex);
}


void CSMSSampleDlg::OnTimer(UINT_PTR nIDEvent)
{
	switch (nIDEvent)
	{
		// search for available device
		case TIMER_DeviceSearch:
			KillTimer(nIDEvent);
			if (DeviceSearch())
			{
				// device found

				// Assign device removal callback handler
				SwiSetDeviceRemovalCallback(DeviceRemovalCallback);

                SetNewSMSCallback(NewSmsCallback);

    	        m_editStatus.SetWindowText(_T("Connected to Device"));


			}
			else
			{
				// Device not found
    	        m_editStatus.SetWindowText(_T("Unable find device"));
                m_btnSend.EnableWindow(FALSE);
				SetTimer(TIMER_DeviceSearch, 1000, NULL);
            }
			break;
		default:
			break;
	}

	CDialog::OnTimer(nIDEvent);
}

LRESULT CSMSSampleDlg::OnNotifDeviceRemoval(WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);

	// Shutdown API
	QCWWANDisconnect();
	// start timer to search for available device
	SetTimer(TIMER_DeviceSearch, 1000, NULL);
	UpdateData(FALSE);	// update screen

	return 0;
}

LRESULT CSMSSampleDlg::OnNotifNewSMS(WPARAM wParam, LPARAM lParam)
{
    // callback params:  
    // ULONG storageType - either UIM or NV  (wParam) 
    // ULONG messageIndex - index of the new msg (lParam)

    // read in the new message
	m_SmsEngine.GetNewMessage((ULONG)wParam, (ULONG)lParam);

	return 0;
}

/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	OnDeleteAllSms
//
//	Description	:	Handler for Delete All Sms message in the List
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:	
//
/////////////////////////////////////////////////////////////////////////////
void CSMSSampleDlg::OnDeleteAllSms() 
{
	m_SmsEngine.DeleteAllMessages();
	m_InboxUI.ClearAll();

}

/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	OnDeleteSms
//
//	Description	:	Handler for Delete Sms message in the list
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:	
//
/////////////////////////////////////////////////////////////////////////////
void CSMSSampleDlg::OnDeleteSms() 
{
    ULONG handle = (ULONG)m_InboxUI.GetSelection();

    if (handle != NULL)
    {
        m_SmsEngine.DeleteMessage(handle);
        m_InboxUI.Clear(handle);
    }
}


/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	OnClickSmsinboxlist
//
//	Description	:	Handler for when the SMS inbox List Control is clicked
//
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:	The SetEvent causes SMS engine to Update the message.
//                  If been clicked, then status of msg must go from not read to read
//                  (if not already done so) and message item must be updated to the view 
//
/////////////////////////////////////////////////////////////////////////////
void CSMSSampleDlg::OnClickSmsinboxlist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	ULONG handle = (ULONG)m_InboxUI.GetSelection();
    if (handle != NULL)  //because clicking in inbox list, this handle should never be NULL
    {
        m_SmsEngine.ChangeMessageStatus(handle);
		m_InboxUI.OnUserClick();
    }
}


/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	OnSendsms
//
//	Description	:	Send SMS... button pressed
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:	None
//
/////////////////////////////////////////////////////////////////////////////
void CSMSSampleDlg::OnSendsms() 
{
	CSendSMSDlg dlgSendSMS;
	//open the Send SMS window
    dlgSendSMS.DoModal();
}
