// NandFlashSettings.cpp : implementation file
//

#include "stdafx.h"
#include "dloader.h"
#include "NandFlashSettings.h"

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

#define NAND_SECTION                    _T("NandFlash")
#define NAND_TYPE_KEY                   _T("FlashType")
#define NAND_STRATEGY_KEY               _T("ReparStrategy")
#define NAND_STRATEGY_GUI				_T("GUI")
#define NAND_STRATEGY_SIM_KEY           _T("SimpleRepStrategy")
#define NAND_SHOW_FLASH_TYPE            _T("ShowFlashType")

int EnumKeys(char* pSection,CStringArray* pKeys);

/////////////////////////////////////////////////////////////////////////////
// CNandFlashSettings property page

IMPLEMENT_DYNCREATE(CNandFlashSettings, CPropertyPage)

CNandFlashSettings::CNandFlashSettings() : CPropertyPage(CNandFlashSettings::IDD)
{
	//{{AFX_DATA_INIT(CNandFlashSettings)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
    m_nRepartitionFlag = REPAR_STRATEGY_STOP;
    m_nFlashType = 0;
	
	m_bSimpleRep = TRUE;
	m_bShowFalshType = FALSE;
}

CNandFlashSettings::~CNandFlashSettings()
{
}

void CNandFlashSettings::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNandFlashSettings)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CNandFlashSettings, CPropertyPage)
	//{{AFX_MSG_MAP(CNandFlashSettings)
	ON_BN_CLICKED(IDC_RADIO_REPARTITION_ALWAYS, OnRadioRepartitionAlways)
	ON_BN_CLICKED(IDC_RADIO_REPARTITION_DO, OnRadioRepartitionDo)
	ON_BN_CLICKED(IDC_RADIO_REPARTITION_IGNORE, OnRadioRepartitionIgnore)
	ON_BN_CLICKED(IDC_RADIO_REPARTITION_STOP, OnRadioRepartitionStop)
	ON_CBN_SELCHANGE(IDC_COMBO_FLASH_TYPE, OnSelchangeComboFlashType)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNandFlashSettings message handlers

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

void CNandFlashSettings::OnRadioRepartitionAlways() 
{
    m_nRepartitionFlag = REPAR_STRATEGY_ALWAYS;	
}

void CNandFlashSettings::OnRadioRepartitionDo() 
{
    m_nRepartitionFlag = REPAR_STRATEGY_DO;	
}

void CNandFlashSettings::OnRadioRepartitionIgnore() 
{
	m_nRepartitionFlag = REPAR_STRATEGY_IGNORE;	
}

void CNandFlashSettings::OnRadioRepartitionStop() 
{
	m_nRepartitionFlag = REPAR_STRATEGY_STOP;	
}


BOOL CNandFlashSettings::LoadSettings(LPCTSTR pFileName)
{
    ASSERT(NULL != pFileName);
    if(NULL == pFileName)
    {
        return FALSE;
    }
    m_strIniFile = pFileName;

#if defined(_SPUPGRADE) || defined (_DOWNLOAD_FOR_PRODUCTION)

#else
    m_nRepartitionFlag = GetPrivateProfileInt( NAND_SECTION,NAND_STRATEGY_KEY,REPAR_STRATEGY_STOP,pFileName );
    m_nFlashType = GetPrivateProfileInt( NAND_SECTION,NAND_TYPE_KEY,0,pFileName );
#endif

	m_bSimpleRep = GetPrivateProfileInt( NAND_STRATEGY_GUI,NAND_STRATEGY_SIM_KEY,1,pFileName );
	m_bShowFalshType = GetPrivateProfileInt( NAND_STRATEGY_GUI,NAND_SHOW_FLASH_TYPE,0,pFileName );
	
    return TRUE;
}

BOOL CNandFlashSettings::SaveSettings(LPCTSTR pFileName)
{
    ASSERT(NULL != pFileName);
    if(NULL == pFileName)
    {
        return FALSE;
    }

    CString strTemp;
    strTemp.Format( _T("%d"),m_nRepartitionFlag );
    WritePrivateProfileString( NAND_SECTION,NAND_STRATEGY_KEY,strTemp,pFileName );

    strTemp.Format( _T("%d"),m_nFlashType );
    WritePrivateProfileString( NAND_SECTION,NAND_TYPE_KEY,strTemp,pFileName );
    
    return TRUE;
}

BOOL CNandFlashSettings::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
    switch( m_nRepartitionFlag )
    {
    case REPAR_STRATEGY_STOP:
        ((CButton*)GetDlgItem( IDC_RADIO_REPARTITION_STOP ))->SetCheck(1);
        break;
    case REPAR_STRATEGY_IGNORE:
        ((CButton*)GetDlgItem( IDC_RADIO_REPARTITION_IGNORE ))->SetCheck(1);
        break;
    case REPAR_STRATEGY_DO:
        ((CButton*)GetDlgItem( IDC_RADIO_REPARTITION_DO ))->SetCheck(1);
        break;
    case REPAR_STRATEGY_ALWAYS:
    default:
        m_nRepartitionFlag = REPAR_STRATEGY_ALWAYS;
        ((CButton*)GetDlgItem( IDC_RADIO_REPARTITION_ALWAYS ))->SetCheck(1);
        break;
    }

    ( ( CComboBox* )GetDlgItem( IDC_COMBO_FLASH_TYPE ) )->SetCurSel( m_nFlashType );

	
	if(m_bSimpleRep)
	{
		GetDlgItem( IDC_RADIO_REPARTITION_IGNORE )->ShowWindow(SW_HIDE);
		GetDlgItem( IDC_RADIO_REPARTITION_DO )->ShowWindow(SW_HIDE);
	}	

	// hide flash type, it not used from now on,2009-11-27
	if(!m_bShowFalshType)
	{
		CWnd *pWnd = NULL;
		pWnd = GetDlgItem(IDC_NFD_STC_FLASH_TYPE);
		if(pWnd != NULL)
		{
			pWnd->ShowWindow(SW_HIDE);
		}
		pWnd = GetDlgItem(IDC_COMBO_FLASH_TYPE);
		if(pWnd != NULL)
		{
			pWnd->ShowWindow(SW_HIDE);
		}
		pWnd = GetDlgItem(IDC_NFD_STC_GRP);
		if(pWnd != NULL)
		{
			pWnd->ShowWindow(SW_HIDE);
		}
	}


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

void CNandFlashSettings::OnSelchangeComboFlashType() 
{
    m_nFlashType = ( ( CComboBox* )GetDlgItem( IDC_COMBO_FLASH_TYPE ) )->GetCurSel();
}
