// FlashOptPage.cpp : implementation file
//

#include "stdafx.h"
#include "dloader.h"
#include "FlashOptPage.h"
#include "EdtBtnCtrl.h"
#include "BMAGlobal.h"
#include "SettingsSheet.h"

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


static _TCHAR g_sz_SEC_READFLASH[]		= _T("ReadFlash");
static _TCHAR g_sz_KEY_FLASH[]          = _T("flash");

/////////////////////////////////////////////////////////////////////////////
// CFlashOptPage property page

IMPLEMENT_DYNCREATE(CFlashOptPage, CPropertyPage)

CFlashOptPage::CFlashOptPage() : CPropertyPage(CFlashOptPage::IDD)
{
	//{{AFX_DATA_INIT(CFlashOptPage)
	m_bActiveRead = FALSE;
	//}}AFX_DATA_INIT
	m_agReadFlash.RemoveAll();
}

CFlashOptPage::~CFlashOptPage()
{
	Clear();
	m_agReadFlash.RemoveAll();
}

void CFlashOptPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFlashOptPage)
	DDX_Control(pDX, IDC_FOD_LST_READ, m_lstRead);
	DDX_Check(pDX, IDC_FOD_CHK_ACTIVE_READ_FLASH, m_bActiveRead);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFlashOptPage, CPropertyPage)
	//{{AFX_MSG_MAP(CFlashOptPage)
	ON_NOTIFY(NM_CLICK, IDC_FOD_LST_READ, OnClickLstRead)
	ON_NOTIFY(LVN_ENDLABELEDIT, IDC_FOD_LST_READ, OnEndlabeleditLstRead)
	ON_BN_CLICKED(IDC_FOD_BTN_DEL, OnBtnClear)
	ON_BN_CLICKED(IDC_FOD_CHK_ACTIVE_READ_FLASH, OnChkActiveReadFlash)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFlashOptPage message handlers

BOOL CFlashOptPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here

		//m_lstProduct
	m_lstRead.SetBkColor(RGB(232, 232, 232));	
	m_lstRead.ModifyStyle(0, LVS_SHOWSELALWAYS);
	
	DWORD dwExStyle = m_lstRead.GetExtendedStyle();
	dwExStyle |= LVS_EX_FULLROWSELECT;
	dwExStyle |= LVS_EX_GRIDLINES;
	dwExStyle |= LVS_EX_CHECKBOXES;

	m_lstRead.SetExtendedStyle(dwExStyle);

	//Load Column
	CString strClmn;
	VERIFY( strClmn.LoadString(IDS_READ_FLASH_LIST_COL) );
	
	int nLen   = strClmn.GetLength();
	LPTSTR lpBuf  = strClmn.GetBuffer(nLen);
	LPTSTR lpFind = _tcschr(lpBuf, _T(','));
	int     nIndex = 0;
	while(lpFind != NULL)
	{
		*lpFind = _T('\0');
		if(nIndex == 0)
			m_lstRead.InsertColumn(nIndex++,lpBuf,LVCFMT_LEFT,50);
		else
			m_lstRead.InsertColumn(nIndex++,lpBuf,LVCFMT_LEFT,110);
		lpBuf = lpFind + 1;
		lpFind = _tcschr(lpBuf, _T(','));
	}
	m_lstRead.InsertColumn(nIndex, lpBuf, LVCFMT_LEFT, 229 );
	strClmn.ReleaseBuffer();

	FillReadList();

	UpdateData(FALSE);
	OnChkActiveReadFlash();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CFlashOptPage::OnClickLstRead(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LPNMITEMACTIVATE pnia = (LPNMITEMACTIVATE)pNMHDR;

	CEdtBtnCtrl *pCtrl = NULL;
	int nItem = pnia->iItem;
	int nSubItem = pnia->iSubItem;
	if(nItem == -1)
	{
		int nCount = m_lstRead.GetItemCount();
		CString strText;
		if( nCount == 0 ||
			(!m_lstRead.GetItemText(nCount-1,1).IsEmpty() && 
			 !m_lstRead.GetItemText(nCount-1,2).IsEmpty() && 
			 !m_lstRead.GetItemText(nCount-1,3).IsEmpty()) )
		{
			m_lstRead.InsertItem(nCount,_T(""));
			if(nCount > 0)			
			{
				m_lstRead.SetCheck(nCount-1);
			}
			nItem = nCount;
		}	
		else if(m_lstRead.GetItemText(nCount-1,1).IsEmpty() && 
			 m_lstRead.GetItemText(nCount-1,2).IsEmpty() && 
			 m_lstRead.GetItemText(nCount-1,3).IsEmpty())
		{
			m_lstRead.DeleteItem(nCount-1);
			return;
		}
		else
		{
			return;
		}
	}

	if(nSubItem == 0)
	{
		return;
	}
		

	pCtrl = new CEdtBtnCtrl();

	BOOL bEnablBtn = FALSE;
	BOOL bOpenFile = FALSE;
	if(nSubItem == 3)
	{
		bEnablBtn = TRUE;
	}

	if(NULL != pCtrl)
	{		
		pCtrl->EnableFileSelButton(bEnablBtn);
		pCtrl->SetBtnOpenFlag(bOpenFile);
		if(nSubItem == 1 || nSubItem == 2 )
		{
			pCtrl->SetEditFmtMode(EX_FMT_HEX);
		}
		pCtrl->m_nItem = nItem;
		pCtrl->m_nSubItem = nSubItem;
		pCtrl->m_strText = m_lstRead.GetItemText(nItem,nSubItem);
		if(!pCtrl->Create(IDD_EDT_BTN_DLG,FromHandle(m_lstRead.m_hWnd)))
		{
			AfxMessageBox(_T("Error to create EdtBtnCtrl"));
			return;
		}
	}
	else
	{
		return;
	}

	CRect rect;
	m_lstRead.GetSubItemRect(nItem,nSubItem,LVIR_BOUNDS,rect);
	CRect rtList;
	m_lstRead.GetClientRect(&rtList);
	if(rect.right>rtList.right)
		rect.right = rtList.right;
	if(rect.left < rtList.left)
		rect.left = rtList.left;
	if(rect.bottom>rtList.bottom)
		rect.bottom = rtList.bottom;
	if(rect.top < rtList.top)
		rect.top = rtList.top;
	
	pCtrl->SetWindowPos(&wndTop,rect.left,rect.top-1,rect.right-rect.left,rect.bottom-rect.top,NULL);
	
    pCtrl->ShowWindow(SW_SHOW);
	
	*pResult = 0;

}

void CFlashOptPage::OnEndlabeleditLstRead(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
	// TODO: Add your control notification handler code here
	m_lstRead.SetItemText(pDispInfo->item.iItem,pDispInfo->item.iSubItem,pDispInfo->item.pszText);
	*pResult = 0;
}

void CFlashOptPage::OnBtnClear() 
{
	// TODO: Add your control notification handler code here
	m_lstRead.DeleteAllItems();
}

BOOL CFlashOptPage::OnKillActive() 
{
	// TODO: Add your specialized code here and/or call the base class
	this->SetFocus();
	if( !g_bInitSheet)
	{
		if(m_bActiveRead)
		{
			int nCount = m_lstRead.GetItemCount();
			for(int i = 0; i<nCount;i++)
			{
				if(m_lstRead.GetCheck(i))
				{
					CString strBase = m_lstRead.GetItemText(i,1);
					CString strSize = m_lstRead.GetItemText(i,2);
					CString strFile = m_lstRead.GetItemText(i,3);
					if(strBase.IsEmpty() || strFile.IsEmpty() || strSize.IsEmpty())
					{
						AfxMessageBox(IDS_ERR_ITEM_EMPTY);			
						return FALSE;
					}
				}
			}
			if(  nCount > 0 && m_lstRead.GetItemText(nCount-1,1).IsEmpty() && 
				 m_lstRead.GetItemText(nCount-1,2).IsEmpty() && 
				 m_lstRead.GetItemText(nCount-1,3).IsEmpty())
			{
				m_lstRead.DeleteItem(nCount-1);
			}
		}
	}
	
	return CPropertyPage::OnKillActive();
}

void CFlashOptPage::OnOK() 
{
	// TODO: Add your specialized code here and/or call the base class
	UpdateData();
	Clear();
	int nCount = m_lstRead.GetItemCount();
	CFileFind finder;
	for(int i = 0; i<nCount;i++)
	{
		if(m_lstRead.GetCheck(i))
		{
			CString strBase = m_lstRead.GetItemText(i,1);
			CString strSize = m_lstRead.GetItemText(i,2);
			CString strFile = m_lstRead.GetItemText(i,3);
			CString strID;	
			
			if(finder.FindFile(strFile))
			{
				DWORD dwAttr = GetFileAttributes(strFile);
				if(-1 != dwAttr)
				{
					dwAttr &= ~FILE_ATTRIBUTE_READONLY;
					::SetFileAttributes(strFile,dwAttr);
				}
			}

			PFILE_INFO_T pFileInfo = new FILE_INFO_T;
			pFileInfo->arrBlock[0].dwBase = GetDigit(strBase);
			pFileInfo->arrBlock[0].dwSize = GetDigit(strSize);
			strID.Format(_T("R%08X"),pFileInfo->arrBlock[0].dwBase);
			_tcscpy(pFileInfo->szID,strID);
			_tcscpy(pFileInfo->szIDAlias,strID);
			_tcscpy(pFileInfo->szFilePath,strFile);
			_tcscpy(pFileInfo->szType,_T("ReadFlashAndSave"));
			pFileInfo->dwCheckFlag = TRUE;

			m_agFlashOpr.Add((DWORD)pFileInfo);
		}
	}

	//±£´æÉèÖÃ
	if(!m_strIniFile.IsEmpty()&& finder.FindFile(m_strIniFile))
	{
		DWORD dwAttr = GetFileAttributes(m_strIniFile);
		if(-1 != dwAttr)
		{
			dwAttr &= ~FILE_ATTRIBUTE_READONLY;
			::SetFileAttributes(m_strIniFile,dwAttr);
		}
		SaveSettings(m_strIniFile);
	}

	CPropertyPage::OnOK();

}
void CFlashOptPage::Clear()
{
	for(int i = 0; i < m_agFlashOpr.GetSize(); i++)
	{
		delete [] (PFILE_INFO_T)m_agFlashOpr[i];
	}
	m_agFlashOpr.RemoveAll();
}


void CFlashOptPage::OnChkActiveReadFlash() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if(m_bActiveRead)
	{
		m_lstRead.EnableWindow(TRUE);
		GetDlgItem(IDC_FOD_BTN_DEL)->EnableWindow(TRUE);
	}
	else
	{
		m_lstRead.EnableWindow(FALSE);
		GetDlgItem(IDC_FOD_BTN_DEL)->EnableWindow(FALSE);
	}
}

BOOL CFlashOptPage::LoadSettings(LPCTSTR pFileName)
{
	if(NULL == pFileName)
		return FALSE;
	m_strIniFile = pFileName;

	m_agReadFlash.RemoveAll();
    _TCHAR szKeyValue[ MAX_BUF_SIZE ]={0}; 
    GetPrivateProfileSection( g_sz_SEC_READFLASH, szKeyValue, MAX_BUF_SIZE, pFileName );
    CStringArray arrKeyData;    
	UINT nFileCount = (UINT)EnumKeys(szKeyValue,&arrKeyData);
	for(UINT i= 0; i< nFileCount; i++)
	{
		CString strText = arrKeyData[2*i+1];
		if(!strText.IsEmpty())
		{
			m_agReadFlash.Add(strText);
		}
	}

	return TRUE;
}

BOOL CFlashOptPage::SaveSettings(LPCTSTR pFileName)
{
	if(NULL == pFileName)
		return FALSE;

	int nCount = m_lstRead.GetItemCount();
	for(int i= 0; i< nCount; i++)
	{
		CString strText;
		strText.Format(_T("%d:%s:%s:%s"),
						m_lstRead.GetCheck(i),
						m_lstRead.GetItemText(i,1),
						m_lstRead.GetItemText(i,2),
						m_lstRead.GetItemText(i,3));
		CString strKey;
		strKey.Format(_T("%s%02d"),g_sz_KEY_FLASH,i);		
		WritePrivateProfileString(g_sz_SEC_READFLASH,strKey,strText,pFileName);

	}
	return TRUE;
}

void CFlashOptPage::FillReadList()
{
	LoadSettings(m_strIniFile);
	int nFlashCount = m_agReadFlash.GetSize();
	for(int i= 0; i< nFlashCount; i++)
	{
		CString strText = m_agReadFlash[i];
		if(strText.IsEmpty())
			continue;

		CString strBase;
		CString strSize;
		CString strFile;
		BOOL    bCheck;
		
		LPTSTR pBuf = strText.GetBuffer(1);
		LPTSTR pFind = _tcschr(pBuf,':');
		if(pFind != NULL)
		{
			*pFind = '\0';
			bCheck = GetDigit(pBuf);
			pBuf = pFind + 1;			
		}
		else
		{
			strText.ReleaseBuffer();
			continue;
		}

		pFind = _tcschr(pBuf,':');
		if(pFind != NULL)
		{
			*pFind = '\0';
			strBase = pBuf;
			pBuf = pFind + 1;			
		}
		else
		{
			strText.ReleaseBuffer();
			continue;
		}

		pFind = _tcschr(pBuf,':');
		if(pFind != NULL)
		{
			*pFind = '\0';
			strSize = pBuf;
			pBuf = pFind + 1;			
		}
		else
		{
			strText.ReleaseBuffer();
			continue;
		}
		strFile = pBuf;

		int nCount = m_lstRead.GetItemCount();	
		if(!bCheck || (!strBase.IsEmpty() && !strSize.IsEmpty() && !strFile.IsEmpty()))
		{			
			m_lstRead.InsertItem(nCount,_T(""));
			m_lstRead.SetItemText(nCount,1,strBase);
			m_lstRead.SetItemText(nCount,2,strSize);
			m_lstRead.SetItemText(nCount,3,strFile);
			m_lstRead.SetCheck(nCount,bCheck);
		}
	}

	int nCount = m_lstRead.GetItemCount();
	if(  nCount > 0 && m_lstRead.GetItemText(nCount-1,1).IsEmpty() && 
		 m_lstRead.GetItemText(nCount-1,2).IsEmpty() && 
		 m_lstRead.GetItemText(nCount-1,3).IsEmpty())
	{
		m_lstRead.DeleteItem(nCount-1);
	}

}
