//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//	$Revision: 3 $																//
//																				//
//	Name       : SendSMSDlg.cpp													//
//                                                                              //
//	Purpose    : Main dialog implementation for SendSMSDlg						//
//																				//
//	Copyright (c) 2002-2010 Sierra Wireless, Inc.  All rights reserved 			//
//																				//
//////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SMSSample.h"
#include "SendSMSDlg.h"
#include <SwiWwanCmApi.h>
#include "GobiConnectionMgmtAPI.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSendSMSDlg dialog

CSendSMSDlg::CSendSMSDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSendSMSDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSendSMSDlg)
	m_szMessageBody = _T("");
	m_szSmsSendTo = _T("");
	//}}AFX_DATA_INIT
}

void CSendSMSDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSendSMSDlg)
	DDX_Control(pDX, IDC_COMPOSEMSGBODY, m_editCompose);
	DDX_Control(pDX, IDC_CMB_Format, m_cmbFormat);
	DDX_Text(pDX, IDC_COMPOSEMSGBODY, m_szMessageBody);
	DDV_MaxChars(pDX, m_szMessageBody, 240);
	DDX_Text(pDX, IDC_SMSSENDTO, m_szSmsSendTo);
	DDV_MaxChars(pDX, m_szSmsSendTo, 50);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSendSMSDlg, CDialog)
	//{{AFX_MSG_MAP(CSendSMSDlg)
	ON_BN_CLICKED(IDC_SENDSMS, OnSendSms)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSendSMSDlg message handlers

BOOL CSendSMSDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_cmbFormat.SetItemData(m_cmbFormat.AddString(L"CDMA"), 0);
	m_cmbFormat.SetItemData(m_cmbFormat.AddString(L"WCDMA"), 6);
	m_cmbFormat.SetCurSel(0);

	// Set maximum composition size
	m_editCompose.SetLimitText(m_nMaxMoSmsLen);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSendSMSDlg::OnOK() 
{
	OnSendSms();
}

/////////////////////////////////////////////////////////////////////////////
//
//	Function	:	OnSendSMS
//
//	Description	:	Send the composed message
//
//	Arguments	:	None
//
//	Returns		:	None
//
//	Comments	:	None
//
/////////////////////////////////////////////////////////////////////////////
void CSendSMSDlg::OnSendSms() 
{
	//read the Data from the screen
	UpdateData(TRUE);

	//check for Blank Entries
	if(m_szMessageBody.IsEmpty())
	{
		MessageBox(_T("Cannot Send Blank Message"), _T("Error"), MB_OK);
	}
	else if (m_szSmsSendTo.IsEmpty())
	{
		MessageBox(_T("Enter an Address to send to"), _T("Error"), MB_OK);
	}
	else		
	{
		ULONG nLenMsgBody = m_szMessageBody.GetLength();
		UINT nLenDest = m_szSmsSendTo.GetLength();
		UINT nType = m_cmbFormat.GetItemData(m_cmbFormat.GetCurSel());

		CHAR szAddress[14] = "";
		WideCharToMultiByte(CP_ACP, 0, m_szSmsSendTo, -1, szAddress, 14, 0, 0);

		ULONG error;
		ULONG ret;

		if(nType == 6)
		{
			BYTE szMessage[531];
			BYTE szMessageSize = 3;
			
			ret = WCDMAEncodeMOTextMsg(&szMessageSize, &szMessage[0], 1, &szAddress[0], nLenMsgBody, m_szMessageBody.GetBuffer(), NULL, NULL, NULL, NULL, NULL, NULL, NULL);

            // BZ34902: We need to set the SMS Protocol here as the API will need 
            // this information to send/read SMS.  If we don't set the protocol, 
            // the default value would be CDMA which might not work for WCDMA networks.
            ret = SetSMSProtocol( 1 );
            if (ret)
            {
                CString strTemp;
                strTemp.Format( L"Set SMS protocol failed\n %d\n", ret );
                MessageBox( strTemp.GetBuffer(), NULL, MB_OK );
            }

			for(int i = 0; i < szMessageSize; i++)
			{                
				ret = SendSMS(nType, szMessage[0 + 177*i], &szMessage[1 + 177*i], 0, &error); //not for multi-SMS

				if(ret)
				{
					CString strTemp;
					strTemp.Format(L" SEND failed \n  %d\n", ret);

					MessageBox(strTemp.GetBuffer(), NULL,MB_OK);
					break;
				}
				else if (error)
				{
					CString strTemp;
					strTemp.Format(L" SEND error\n  %d\n", error);

					MessageBox(strTemp.GetBuffer(), NULL,MB_OK);
					break;
				}
			}
		}
		// CDMA send
		else if (nType == 0)
		{
			BYTE szMessage[240];
			BYTE szMessageSize = 240;
			BYTE encAlpha = 0; //7-bit ASCII
			BYTE priority = 0;

			ret = CDMAEncodeMOTextMsg(&szMessageSize, szMessage, 1, szAddress, NULL, nLenMsgBody, m_szMessageBody.GetBuffer(), &priority, &encAlpha);
			if (ret)
			{
				CString strTemp;
                strTemp.Format( L"CDMAEncodeMOTextMsg failed\n %d\n", ret );
                MessageBox( strTemp.GetBuffer(), NULL, MB_OK );
			}
            // BZ34902: We need to set the SMS Protocol here as the API will need 
            // this information to send/read SMS.
            ret = SetSMSProtocol( 0 );
            if (ret)
            {
                CString strTemp;
                strTemp.Format( L"Set SMS protocol failed\n %d\n", ret );
                MessageBox( strTemp.GetBuffer(), NULL, MB_OK );
            }

			ret = SendSMS(nType, szMessageSize, szMessage, 0, &error); // Not for multi-SMS

			if(ret)
			{
				CString strTemp;
				strTemp.Format(L" SEND failed \n  %d\n", ret);

				MessageBox(strTemp.GetBuffer(), NULL,MB_OK);
			}
			else if (error)
			{
				CString strTemp;
				strTemp.Format(L" SEND error\n  %d\n", error);

				MessageBox(strTemp.GetBuffer(), NULL,MB_OK);
			}
		}

		EndDialog(IDOK);
	}
}
 

