// CalibrationgPage.cpp : implementation file
//

#include "stdafx.h"
#include "dloader.h"
#include "CalibrationPage.h"
#include "supergridctrl.h"
#include "MainPage.h"
#include "SettingsSheet.h"
#include "FileDlg.h"

// NV flag section
static _TCHAR g_sz_NV_PART[] = _T("NV");
static _TCHAR g_sz_NV_FLAG[] = _T("Backup calibration");
static _TCHAR g_sz_NV_REPL[] = _T("Replace flag");
static _TCHAR g_sz_NV_CONTINUE[]=_T("Continue flag");
static _TCHAR g_sz_NV_IMEI_FLAG[] = _T("Backup IMEI");
static _TCHAR g_sz_NV_BACKUP_PATH[]=_T("BackupPath");
static _TCHAR g_sz_NV_SAVE_LOCAL_FLAG[]=_T("SaveToLocalFlag");



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

/////////////////////////////////////////////////////////////////////////////
// CCalibrationPage dialog
IMPLEMENT_DYNCREATE(CCalibrationPage, CPropertyPage)


CCalibrationPage::CCalibrationPage()
	: CPropertyPage(CCalibrationPage::IDD)
{
	//{{AFX_DATA_INIT(CCalibrationPage)
	m_strPath = _T("");
	m_bSaveToLocal = FALSE;
	//}}AFX_DATA_INIT

	m_strIniFile = _T("");
	m_strCurProduct = _T("");
	m_strTempCurProduct = _T("");
	m_pXmlConfigParse = NULL;
	m_strLocalPath = _T("D:\\__NVBackupDir");
	m_strPath = m_strLocalPath;

}

CCalibrationPage::~CCalibrationPage()
{
	if(m_pXmlConfigParse != NULL)
	{
		m_pXmlConfigParse->Release();
		m_pXmlConfigParse = NULL;
	}
}
void CCalibrationPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCalibrationPage)
	DDX_Control(pDX, IDC_LIST_FRAME, m_stcFrame);
	DDX_Text(pDX, IDC_DC_EDT_PATH, m_strPath);
	DDV_MaxChars(pDX, m_strPath, 256);
	DDX_Check(pDX, IDC_CHK_SAVE_TO_LOCAL, m_bSaveToLocal);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCalibrationPage, CPropertyPage)
	//{{AFX_MSG_MAP(CCalibrationPage)
	ON_BN_CLICKED(IDC_CHK_SAVE_TO_LOCAL, OnChkSaveToLocal)
	ON_BN_CLICKED(IDC_DC_BTN_BROWSE, OnDcBtnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCalibrationPage message handlers

BOOL CCalibrationPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();

	m_lstBackup.Create(WS_CHILD | WS_VISIBLE | WS_TABSTOP, CRect(0,0,0,0),this,0x1001);	
	m_lstBackup.ModifyStyleEx(0,WS_EX_CLIENTEDGE);

    m_lstBackup.FillList();

	CRect rc;
	m_stcFrame.GetWindowRect(rc);
	ScreenToClient(rc);
	m_stcFrame.ShowWindow(SW_HIDE);
	if(m_lstBackup.GetSafeHwnd())
		m_lstBackup.MoveWindow(rc);
	
	GetDlgItem(IDC_DC_EDT_PATH)->EnableWindow(m_bSaveToLocal);
	GetDlgItem(IDC_STC_VER_SAP)->EnableWindow(m_bSaveToLocal);
	GetDlgItem(IDC_DC_BTN_BROWSE)->EnableWindow(m_bSaveToLocal);
	GetDlgItem(IDC_STC_LOCAL_PATH)->EnableWindow(m_bSaveToLocal);

	((CEdit *)GetDlgItem(IDC_DC_EDT_PATH))->UpdateData(FALSE);


	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
BOOL CCalibrationPage::LoadSettings(LPCTSTR pFileName)
{
	ASSERT(NULL != pFileName);
    if(NULL == pFileName)
    {
        return FALSE;
    }	
	m_strIniFile = pFileName;
	
    _TCHAR szBuf[MAX_PATH]={0};  
	DWORD dwLen = ::GetPrivateProfileString(g_sz_NV_PART,g_sz_NV_BACKUP_PATH,NULL,szBuf,MAX_PATH,pFileName);
	if(dwLen !=0 )
	{
		m_strPath = szBuf;
		m_strLocalPath = m_strPath;
	}
// Download need not save nv to local
// Dloader can set by GUI
// So SPUpgrade must be initialized by configure
#ifdef _SPUPGRADE
	m_bSaveToLocal = ::GetPrivateProfileInt( g_sz_NV_PART, g_sz_NV_SAVE_LOCAL_FLAG, 0, pFileName );
#endif

	if(m_bSaveToLocal && !CreateDeepDirectory(m_strPath))
	{
		CString strErr;
		strErr.Format(_T("Create directory [%s] fail.\n This directory is configured in [%s]."),m_strPath,pFileName);
		AfxMessageBox(strErr);
		return FALSE;
	}
	
	return TRUE;
}
BOOL CCalibrationPage::SaveSettings(LPCTSTR pFileName)
{
    if(NULL == pFileName)
    {
        return FALSE;
    }

	CString strTemp;
    strTemp.Format( _T("%d"), m_bSaveToLocal );
    ::WritePrivateProfileString( g_sz_NV_PART,g_sz_NV_SAVE_LOCAL_FLAG,strTemp,pFileName);

	::WritePrivateProfileString(g_sz_NV_PART,g_sz_NV_BACKUP_PATH,m_strLocalPath,pFileName);

	return m_lstBackup.SaveSettings(pFileName);
}

void CCalibrationPage::OnOK()
{
    CPropertyPage::OnOK();

    m_strCurProduct = m_strTempCurProduct;
	m_lstBackup.Update();

	((CEdit *)GetDlgItem(IDC_DC_EDT_PATH))->UpdateData();

	if(!m_strPath.IsEmpty())
	{
		m_strLocalPath=m_strPath;
	}

	if(m_bSaveToLocal && m_strLocalPath.GetLength()>2 && !CreateDeepDirectory(m_strLocalPath))
	{
		_ASSERTE( 0 );	
	}

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


BOOL CCalibrationPage::OnKillActive() 
{
	// TODO: Add your specialized code here and/or call the base class
	this->SetFocus();
	if( !g_bInitSheet)
	{
		m_lstBackup.UpdateTemp();
	}
	if(m_lstBackup.m_pNvBkpItmArray == NULL && m_lstBackup.m_pTempNvBkpItmArray != NULL)
	{
		m_lstBackup.Update();
	}

	((CEdit *)GetDlgItem(IDC_DC_EDT_PATH))->UpdateData();

	if(m_bSaveToLocal && m_strLocalPath.GetLength()>2 && !CreateDeepDirectory(m_strPath))
	{
		CString strErr;
		strErr.Format(_T("Create directory [%s] fail."),m_strPath);
		AfxMessageBox(strErr);
		return FALSE;
	}

	return CPropertyPage::OnKillActive();
}

LPCTSTR CCalibrationPage::GetXmlFile(LPCTSTR pFileName)
{
	static _TCHAR szXmlFile[MAX_PATH] = {0};
	_tcscpy(szXmlFile,pFileName);
	_TCHAR * p = _tcsrchr(szXmlFile,_T('\\'));
	if(p == NULL)
	{
		return NULL;
	}
	*p=0;
	_tcscat(szXmlFile,_T("\\BMAConfig.xml"));
	return szXmlFile;
}

void CCalibrationPage::OnChkSaveToLocal() 
{
	// TODO: Add your control notification handler code here
	m_bSaveToLocal = !m_bSaveToLocal;
	GetDlgItem(IDC_DC_EDT_PATH)->EnableWindow(m_bSaveToLocal);
	GetDlgItem(IDC_STC_VER_SAP)->EnableWindow(m_bSaveToLocal);
	GetDlgItem(IDC_DC_BTN_BROWSE)->EnableWindow(m_bSaveToLocal);
	GetDlgItem(IDC_STC_LOCAL_PATH)->EnableWindow(m_bSaveToLocal);
}

void CCalibrationPage::OnDcBtnBrowse() 
{
	CFileDlg fdlg;
	fdlg.SetHwnOwner(this->m_hWnd);
    CString strDir = fdlg.GetSelectDir();
    if( strDir.IsEmpty() )
        return;
	
	m_strPath = strDir;
	GetDlgItem(IDC_DC_EDT_PATH)->SetWindowText(m_strPath);
}
