// OptionHelpper.cpp: implementation of the COptionHelpper class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BMPlatform.h"
#include "OptionHelpper.h"

COptionHelpper ohObject;

#define DEFAULT_CHECK_BAUDRATE_TIMES 3
#define DEFAULT_MAX_LENGTH 0x3000
#define INVALID_OPTION_VALUE -1
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

COptionHelpper::COptionHelpper()
{
    m_nCheckBaudTimes = INVALID_OPTION_VALUE;
    m_nRepartitionFlag = INVALID_OPTION_VALUE;
	m_nReadFlashBRFlag = INVALID_OPTION_VALUE;
	m_strProduct = _T("");
	m_bChangeTimeOutSetting = FALSE;
}

COptionHelpper::~COptionHelpper()
{

}

int COptionHelpper::GetCheckBaudTimes( const _TCHAR* lpszFileType )
{
    if( INVALID_OPTION_VALUE != m_nCheckBaudTimes )
    {
        // It is changed by client,do not load from ini file
        return m_nCheckBaudTimes;
    }

    _TCHAR szConfigIniFile[MAX_PATH];
    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\BMTimeout.ini") );
    
    return GetPrivateProfileInt( _T("Check Baud Times") , lpszFileType, DEFAULT_CHECK_BAUDRATE_TIMES, szConfigIniFile);    
}

int COptionHelpper::GetTimeout( _TCHAR* lpszOperation )
{
    _TCHAR szConfigIniFile[MAX_PATH];
    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\BMTimeout.ini") );

	CString strSection = _T("Timeout");
	if(m_bChangeTimeOutSetting && !m_strProduct.IsEmpty())
	{
		strSection = m_strProduct;
	}
    
    return GetPrivateProfileInt( strSection , lpszOperation, 1000, szConfigIniFile);    
}

int COptionHelpper::Get7ENumOnce()
{
	_TCHAR szConfigIniFile[MAX_PATH];    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\BMTimeout.ini") );

	return GetPrivateProfileInt( _T("CheckBaudSetting") , _T("7ENumPerTime"), 1, szConfigIniFile);  
}

int COptionHelpper::GetPacketLength( _TCHAR* lpszFileType )
{
    _TCHAR szConfigIniFile[MAX_PATH];    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\BMFileType.ini") );

    return GetPrivateProfileInt( _T("Max Length"), lpszFileType,DEFAULT_MAX_LENGTH, szConfigIniFile);
}

int COptionHelpper::GetRepartitionFlag()
{
    if( INVALID_OPTION_VALUE != m_nRepartitionFlag )
    {
        // It is changed by client,do not load from ini file
        return m_nRepartitionFlag;
    }    

    _TCHAR szConfigIniFile[MAX_PATH];    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\BMFileType.ini") );
    
    return GetPrivateProfileInt( _T("Repartition"), _T("strategy"),1, szConfigIniFile);
}

BOOL COptionHelpper::GetFileOperations( _TCHAR* lpszFileType,CStringArray* pOperations )
{
    _TCHAR szConfigIniFile[MAX_PATH];    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\BMFileType.ini") );
    
    _TCHAR szSection[MAX_PATH];
    GetPrivateProfileSection( lpszFileType, szSection, MAX_PATH, szConfigIniFile );
    
    
    return EnumKeys(szSection,pOperations);        
}

// Format of pSection
// "Key1 = data1\0Key2 = data2\0....Keyn = datan\0\0"
int COptionHelpper::EnumKeys(_TCHAR* pSection,CStringArray* pKeys)
{
    ASSERT(pSection != NULL);
    ASSERT(pKeys != NULL);
    
    int number = 0;
    while(1)
    {
        CString strLine = pSection;
        int nLen = strLine.GetLength();
        if(0 == nLen)
        {
            // All keys are read
            break;
        }
        
        int nIndex = strLine.Find('=');
        if(-1 == nIndex)
        {
            // Not a valid key
            continue;
        }
        CString strKey = strLine.Left(nIndex);
        strKey.TrimLeft();
        strKey.TrimRight();
        pKeys->Add(strKey);
        
        CString strData = strLine.Right(nLen - nIndex - 1);
        strData.TrimLeft();
        strData.TrimRight();
        pKeys->Add(strData);
        
        number++;
        
        pSection += nLen + 1;        
    }
    
    return number;
}

void COptionHelpper::SetCheckBaudTimes( int nTimes )
{
    m_nCheckBaudTimes = nTimes;
}

void COptionHelpper::SetRepartitionFlag( int nFlag )
{
    m_nRepartitionFlag = nFlag;
}

void COptionHelpper::SetReadFlashBefRepFlag(int nFlag)
{
	m_nReadFlashBRFlag = nFlag;
}

int COptionHelpper::GetNVItemID()
{
    _TCHAR szConfigIniFile[MAX_PATH];
    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\BMTimeout.ini") );
    
    return GetPrivateProfileInt( _T("NVItem"), _T("ItemID"), 0x0, szConfigIniFile); 
}

int COptionHelpper::GetLogFlag()
{
    _TCHAR szConfigIniFile[MAX_PATH];
    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\BMTimeout.ini") );
    
    return GetPrivateProfileInt( _T("Log"), _T("Enable"), 0x0, szConfigIniFile); 
}

int COptionHelpper::GetReadFlashBefRepFlag()
{
    if( INVALID_OPTION_VALUE != m_nReadFlashBRFlag )
    {
        // It is changed by client,do not load from ini file
        return m_nReadFlashBRFlag;
    }    

    _TCHAR szConfigIniFile[MAX_PATH];    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\BMFileType.ini") );
    
    return GetPrivateProfileInt( _T("ReadFlashBeforeRepartition"), _T("strategy"),0, szConfigIniFile);
}

BOOL COptionHelpper::SetProperty(LONG lFlags, _TCHAR* lpszName,const VARIANT * pvarValue)
{
	UNUSED_ALWAYS(lFlags);
	CString strName = lpszName;
	BOOL bOK = FALSE;
	if(strName.CompareNoCase(_T("PRODUCT"))== 0)
	{
		m_strProduct = (_TCHAR*)pvarValue->bstrVal;

		// judge if the customer product time out is set
		_TCHAR szConfigIniFile[MAX_PATH];    
		GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
		_tcscat( szConfigIniFile,  _T("\\BMTimeout.ini") );
    
		_TCHAR szSection[MAX_PATH];
		DWORD dwSize = GetPrivateProfileSection( m_strProduct, szSection, MAX_PATH, szConfigIniFile );
		
		m_bChangeTimeOutSetting = (dwSize==0?FALSE:TRUE);	
		bOK = TRUE;
	}
	
	return bOK;
}
BOOL COptionHelpper::GetProperty(LONG lFlags, _TCHAR* lpszName,VARIANT * pvarValue)
{
	UNUSED_ALWAYS(lFlags);
	UNUSED_ALWAYS(lFlags);

	CString strName = lpszName;
	BOOL bOK = FALSE;
	if(strName.CompareNoCase(_T("CheckNVTimes"))== 0)
	{	
		_TCHAR szConfigIniFile[MAX_PATH];    
		GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
		_tcscat( szConfigIniFile,  _T("\\BMTimeout.ini") );
    
		//_TCHAR szSection[MAX_PATH];
		DWORD dwTimes = GetPrivateProfileInt( _T("CheckNVTimes"), _T("times"), 0, szConfigIniFile );
		
		pvarValue->uintVal = dwTimes;
			
		bOK = TRUE;
	}

	return bOK;
}

int COptionHelpper::GetDebugModeFlag(_TCHAR* lpszFileID)
{
    _TCHAR szConfigIniFile[MAX_PATH];
    
    GetModuleFilePath( g_theApp.m_hInstance, szConfigIniFile );
    _tcscat( szConfigIniFile,  _T("\\ChannelConfig.ini") );
    
    return GetPrivateProfileInt( _T("DebugMode"), lpszFileID, 0, szConfigIniFile); 
}