// PhoneAddrDlg.cpp : implementation file
//

#include "stdafx.h"
#include "palmtool.h"
#include "UdpSocket.h"
#include "TcpSocket.h"
#include "PhoneAddrDlg.h"
#include "sdram.h"
#include "PhoneSettingsDlg.h"

int SpeedDialIndex2Pos(int iIndex);
CString ReadSpeedDial(int iIndex);
BOOL WriteSpeedDial(CString str, int iIndex);

char * pStrAddress;

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

/////////////////////////////////////////////////////////////////////////////
// CPhoneAddrDlg dialog


CPhoneAddrDlg::CPhoneAddrDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPhoneAddrDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPhoneAddrDlg)
	//}}AFX_DATA_INIT
}


void CPhoneAddrDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPhoneAddrDlg)
	DDX_Control(pDX, IDC_SPEEDDIAL_EDIT, m_ctlSpeedDialEdit);
	DDX_Control(pDX, IDC_PHONE_ADDR, m_ctlPhoneAddr);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPhoneAddrDlg, CDialog)
	//{{AFX_MSG_MAP(CPhoneAddrDlg)
	ON_WM_DESTROY()
	ON_NOTIFY(NM_DBLCLK, IDC_PHONE_ADDR, OnDblclkPhoneAddr)
	ON_EN_CHANGE(IDC_SPEEDDIAL_EDIT, OnChangeSpeedDialEdit)
	ON_BN_CLICKED(IDC_SPEED_CLEAR, OnSpeedClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPhoneAddrDlg message handlers

BOOL CPhoneAddrDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	DWORD dwStyle;

	dwStyle = m_ctlPhoneAddr.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EDITLABELS;
	m_ctlPhoneAddr.SetExtendedStyle(dwStyle);
	m_ctlPhoneAddr.InsertColumn(0, "Number", LVCFMT_CENTER, 70, 0);
	m_ctlPhoneAddr.InsertColumn(1, "Name", LVCFMT_CENTER, 140, 1);
	m_ctlPhoneAddr.InsertColumn(2, "Phone Number", LVCFMT_CENTER, 140, 2);
	m_ctlPhoneAddr.InsertColumn(3, "Number", LVCFMT_CENTER, 70, 3);
	m_ctlPhoneAddr.InsertColumn(4, "Name", LVCFMT_CENTER, 140, 4);
	m_ctlPhoneAddr.InsertColumn(5, "Phone Number", LVCFMT_CENTER, 140, 5);

	pStrAddress = m_pInfo;
	AddSpeedDial();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPhoneAddrDlg::OnDestroy() 
{
	CDialog::OnDestroy();
}

int SpeedDialIndex2Pos(int iIndex)
{
	int iPos;

	iPos = iIndex;
	iPos <<= 4;
	iPos += SPEED_DIAL_BEGIN;
	return iPos;
}

BOOL WriteSpeedDial(CString str, int iIndex)
{
	int iOffset;
	int iPos;
	CString iVal;

	iPos = SpeedDialIndex2Pos(iIndex);
	iOffset = iPos;

	if (strlen(str) > PHONE_NUMBER_LEN)
	{
		iVal = "Parameter is not the right length!";
		AfxMessageBox(iVal);
		return FALSE;
	}
	
	memcpy(pStrAddress+iOffset, str, PHONE_NUMBER_LEN);
	return TRUE;
}

CString ReadSpeedDial(int iIndex)
{
	int iOffset;
	int iPos;
	char cStr[PHONE_NUMBER_LEN+1];
	CString str;

	iPos = SpeedDialIndex2Pos(iIndex);
	iOffset = iPos;
	memcpy(cStr, pStrAddress+iOffset, PHONE_NUMBER_LEN);
	cStr[PHONE_NUMBER_LEN] = 0;
	str = cStr;
	return str;
}

void CPhoneAddrDlg::AddSpeedDial()
{
	int iIndex;
	int iTotal, iItem;
	LV_ITEM	lv_item;
	char cNumber[4];

	for (iIndex = 0; iIndex < SPEED_DIAL_NUM; iIndex += 2)
	{
		iTotal = m_ctlPhoneAddr.GetItemCount();
		lv_item.mask = LVIF_TEXT;
		lv_item.iItem = iTotal;
		lv_item.iSubItem = 0;
		itoa(iIndex+1, cNumber, 10);
		lv_item.pszText = cNumber;
		iItem = m_ctlPhoneAddr.InsertItem(&lv_item); // insert new lv_item
		m_ctlPhoneAddr.SetItemText(iItem, 1, ReadSpeedDial(iIndex*2+1));
		m_ctlPhoneAddr.SetItemText(iItem, 2, ReadSpeedDial(iIndex*2));
		itoa(iIndex+2, cNumber, 10);
		m_ctlPhoneAddr.SetItemText(iItem, 3, cNumber);
		m_ctlPhoneAddr.SetItemText(iItem, 4, ReadSpeedDial((iIndex+1)*2+1));
		m_ctlPhoneAddr.SetItemText(iItem, 5, ReadSpeedDial((iIndex+1)*2));
	}
}

void CPhoneAddrDlg::OnDblclkPhoneAddr(NMHDR* pNMHDR, LRESULT* pResult)
{
	POINT pt;
    RECT rect; 
	int x = 0;
	int col = -1;
	int total_col = 6;
   
    GetCursorPos(&pt); 
    m_ctlPhoneAddr.GetWindowRect(&rect); 
    pt.x-=rect.left; 
    pt.y-=rect.top; 
	for (int i = 0; i < total_col; i++)
	{
		x += m_ctlPhoneAddr.GetColumnWidth(i) ;
		if (pt.x < x)
		{
			col = i;
			break; 
		}
	}

	int index=-1;
	int count = m_ctlPhoneAddr.GetItemCount();
	for (i = m_ctlPhoneAddr.GetTopIndex(); i < count; i++)
	{
		m_ctlPhoneAddr.GetItemRect(i, &rect, LVIR_BOUNDS);
		if (PtInRect(&rect, pt))
		{
			index = i;
			break;
		}
	}

    if (col != -1 && index != -1 && col != 0 && col != 3)
	{
		RECT rc ;
		POINT pt ;
		int col_offset = 0;
		CString cStr;

		m_ctlPhoneAddr.GetItemRect(index, &rect, LVIR_BOUNDS) ;
		m_ctlPhoneAddr.GetWindowRect(&rc) ;
		pt.x = rc.left;
		pt.y = rc.top;
		ScreenToClient(&pt);
		for (int i = 0; i < col; i++)
			col_offset += m_ctlPhoneAddr.GetColumnWidth(i);

		iSelectCol = col ;
		iSelectIndex = index ;

		m_ctlSpeedDialEdit.MoveWindow(rect.left+pt.x + col_offset + 2, rect.top+pt.y+2, m_ctlPhoneAddr.GetColumnWidth(col)-1, rect.bottom-rect.top-1) ;
		m_ctlSpeedDialEdit.BringWindowToTop() ;
		m_ctlSpeedDialEdit.ShowWindow(SW_SHOW) ;
		cStr = m_ctlPhoneAddr.GetItemText(iSelectIndex, iSelectCol);
		if(cStr != "")
			m_ctlSpeedDialEdit.SetWindowText(cStr);
		else
			m_ctlSpeedDialEdit.SetWindowText("");
		m_ctlSpeedDialEdit.SetFocus();
	}

	*pResult = 0;
}

void CPhoneAddrDlg::OnChangeSpeedDialEdit() 
{
	m_ctlSpeedDialEdit.GetWindowText(cStrEdit);
	m_ctlPhoneAddr.SetItemText(iSelectIndex, iSelectCol, cStrEdit);
	
}

void CPhoneAddrDlg::OnOK() 
{
	int iIndex = 0;
	CString cStr;
	int count = 0;

	UpdateData();

	while(iIndex < SPEED_DIAL_NUM)
	{
		cStr = m_ctlPhoneAddr.GetItemText(count, 1);
		if (!WriteSpeedDial(cStr, iIndex*2+1))
			return;
		cStr = m_ctlPhoneAddr.GetItemText(count, 2);
		if (!WriteSpeedDial(cStr, iIndex*2))
			return;
		cStr = m_ctlPhoneAddr.GetItemText(count, 4);
		if (!WriteSpeedDial(cStr, (iIndex+1)*2+1))
			return;
		cStr = m_ctlPhoneAddr.GetItemText(count, 5);
		if (!WriteSpeedDial(cStr, (iIndex+1)*2))
			return;
		iIndex += 2;
		count += 1;
	}
	
	memcpy(m_pInfo, pStrAddress, TOTAL_SETTINGS_SIZE);

	CDialog::OnOK();
}

void CPhoneAddrDlg::OnSpeedClear() 
{
	int iItem;

	for (iItem = 0; iItem < SPEED_DIAL_NUM/2; iItem ++)
	{
		m_ctlPhoneAddr.SetItemText(iItem, 1, "");
		m_ctlPhoneAddr.SetItemText(iItem, 2, "");
		m_ctlPhoneAddr.SetItemText(iItem, 4, "");
		m_ctlPhoneAddr.SetItemText(iItem, 5, "");
	}
}

void CPhoneAddrDlg::ConvertAddress(BYTE * pSettings, CString& strwrite)
{
	char buffer[4];
	int iIndex = 0;
	char cStr[TOTAL_SETTINGS_SIZE];

	memcpy(cStr, pSettings, TOTAL_SETTINGS_SIZE);
	pStrAddress = cStr;

	for (iIndex = 0; iIndex < SPEED_DIAL_NUM; iIndex++)
	{
		itoa(iIndex+1, buffer, 10);
		strwrite += "memory";
		strwrite += buffer;
		strwrite += ":";

		strwrite += ReadSpeedDial(iIndex*2+1);
		strwrite += "\n";
		itoa(iIndex+1, buffer, 10);
		strwrite += "number";
		strwrite += buffer;
		strwrite += ":";

		strwrite += ReadSpeedDial(iIndex*2);
		strwrite += "\n";
	}
}

BOOL CPhoneAddrDlg::WrietAddressBook(BYTE * pSettings, CString str, int iIndex, bool bName)
{
	char cStr[TOTAL_SETTINGS_SIZE];

	memcpy(cStr, pSettings, TOTAL_SETTINGS_SIZE);
	pStrAddress = cStr;

	if(bName)
	{
		if(!WriteSpeedDial(str.Right(str.GetLength() - str.Find(':') - 1), iIndex*2+1))
			return FALSE;
	}
	else
	{
		if(!WriteSpeedDial(str.Right(str.GetLength() - str.Find(':') - 1), iIndex*2))
			return FALSE;
	}

	memcpy(cStr, pStrAddress, TOTAL_SETTINGS_SIZE);
	memcpy(pSettings, cStr, TOTAL_SETTINGS_SIZE);

	return TRUE;
}
