/***************************************************************************
                   Config Class (Config.cpp)
                -----------------------------------------

	begin      : 2013/09/16
	copyright  : (C) 2005 Solbox Inc.
	author     : Development 1 Team
					- 2013/09/16 
	email      : dev1@solbox.com
	version    : 3.2.0

	CopyRight(C) 2005 Solbox Inc. All Rights reserved.
	Redistribution and use in source and binary forms, with or with out
	modification, are not permitted in outside of Solbox Inc.
***************************************************************************/
#include "cc_tsd.h"
#include "Configs.h"
#include "Config.h"

CMyConfig *CMyConfig::m_pInstance = NULL;

void StringSplit(string str, string delim, vector<string> &results, bool bUseEmpty /*= false*/)
{
	const string strEmpty("");
	string::size_type cutAt;

	while( (cutAt = str.find_first_of(delim)) != str.npos )
	{
		if(cutAt > 0)
		{
			results.push_back(str.substr(0,cutAt));
		}
		else
		{
			if(bUseEmpty && cutAt == 0)
				results.push_back(strEmpty);
		}
		str = str.substr(cutAt+1);
	}
	if(str.length() > 0)
	{
		results.push_back(str);
	}
}

CCommonConfig::CCommonConfig( string szFilename, string szProgramName )
	: m_szConfigFile(szFilename), m_szProgramName(szProgramName), m_nLogLevel(0)
{
}

CCommonConfig::~CCommonConfig()
{
}

bool CCommonConfig::LoadConf()
{
#ifdef _DEBUG
	cout << "CCommonConfig::LoadConf() =>" << endl;
#endif // _DEBUG
	string szValue;

	// Config 贸府甫 困茄 按眉 积己
	Config conf;

	// Config File open
	if( conf.Open( m_szConfigFile ) == false )
	{
		m_szErrMessage = "Config file open failed.[" + m_szConfigFile + "]";
		return false;
	}

	// log path
	if( conf.GetConfig( "COMMON", "DEFAULT_LOG_DIR", szValue ) )
	{
		m_szAppLogRoot = szValue;
	}

	// log level
	if( conf.GetConfig( "COMMON", "LOG_LEVEL", szValue ) )
	{
		m_nLogLevel = atoi( szValue.c_str() );
	}

#ifdef _DEBUG
	PrintValue();
#endif // _DEBUG

	return true;
}

bool CCommonConfig::CheckValue()
{
	if(m_szAppLogRoot.empty())
	{
		m_szErrMessage  = "Config info get failed. [" + m_szProgramName + " or COMMON]->DEFAULT_LOG_DIR";
		return false;
	}

	if( m_nLogLevel <= 0)
	{
		m_szErrMessage  = "Config info get failed. [" + m_szProgramName + " or COMMON]->LOG_LEVEL";
		return false;
	}

	return true;	
}

void CCommonConfig::PrintValue()
{
	cout << "Log   Value : " << m_szAppLogRoot << "," << m_nLogLevel << endl;
}

CMyConfig::CMyConfig( string szFilename, string szProgramName)
	: CCommonConfig(szFilename, szProgramName)
	, m_nListenPort(0), m_nDBPort(0), m_bLoginInfo(false)
{
}

CMyConfig::~CMyConfig()
{
}

bool CMyConfig::LoadConf()
{
	if( CCommonConfig::LoadConf() == false)
		return false;
#ifdef _DEBUG
	cout << "CMyConfig::LoadConf() =>" << endl;
#endif // _DEBUG
	string szValue;

	// Config 贸府甫 困茄 按眉 积己
	Config conf;

	// Config File open
	if( conf.Open( m_szConfigFile ) == false )
	{
		m_szErrMessage = "Config file open failed.[" + m_szConfigFile + "]";
		return false;
	}

	// log dir
	if( conf.GetConfig( m_szProgramName, "DEFAULT_LOG_DIR", szValue ) )
	{
		m_szAppLogRoot = szValue;
	}

	// log level
	if( conf.GetConfig( m_szProgramName, "LOG_LEVEL", szValue ) )
	{
		m_nLogLevel = atoi( szValue.c_str() );
	}
	
	// TCP Listen Port
	if(conf.GetConfig( m_szProgramName, "TCP_LISTEN_PORT", szValue ))
	{
		m_nListenPort = atoi( szValue.c_str() );
	}

	// db host(ip)
	if( conf.GetConfig( m_szProgramName, "CCDB_IP", szValue ) )
	{
		m_szDBIP = szValue;
	}

	// db port
	if( conf.GetConfig( m_szProgramName, "CCDB_PORT", szValue ) )
	{
		m_nDBPort = atoi( szValue.c_str() );
	}

	// db name
	if( conf.GetConfig( m_szProgramName, "CCDB_DB_NAME", szValue ) )
	{
		m_szDBName = szValue;
	}

	// db account
	if( conf.GetConfig( m_szProgramName, "CCDB_ACCT", szValue ) )
	{
		m_szDBAccount = szValue;
	}

	// db account password
	if( conf.GetConfig( m_szProgramName, "CCDB_ACCT_PW", szValue ) )
	{
		m_szDBPwd = szValue;
	}

	// log dir
	if( conf.GetConfig( m_szProgramName, "LOGIN_INFO_INSERT", szValue ) )
	{
		m_szLoginInfo = szValue;
	}

#ifdef _DEBUG
	PrintValue();
#endif // _DEBUG
	return true;
}

bool CMyConfig::CheckValue()
{
	if( CCommonConfig::CheckValue() )
	{
		if( m_nListenPort <= 0)
		{
			m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->TCP_LISTEN_PORT";
			return false;
		}

		if(m_szDBIP.size() <= 0)
		{
			m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->CCDB_IP";
			return false;
		}

		if( m_nDBPort <= 0)
		{
			m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->CCDB_PORT";
			return false;
		}

		if(m_szDBName.size() <= 0)
		{
			m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->CCDB_DB_NAME";
			return false;
		}

		if(m_szDBAccount.size() <= 0)
		{
			m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->CCDB_ACCT";
			return false;
		}

		if(m_szDBPwd.size() <= 0)
		{
			m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->CCDB_ACCT_PW";
			return false;
		}

		if( strcmp(m_szLoginInfo.c_str(), "on") == 0 || strcmp(m_szLoginInfo.c_str(), "off") == 0 )
		{
			if( strcmp(m_szLoginInfo.c_str(), "on") == 0 )
			{
				m_bLoginInfo = true;
			}
			else
			{
				m_bLoginInfo = false;
			}
		}
		else
		{
			m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->LOGIN_INFO_INSERT";
			return false;
		}

		return true;
	}
	else 
	{
		return false;
	}

	return true;
}

void CMyConfig::PrintValue()
{
	CCommonConfig::PrintValue();

	cout << "TCP Listen Port :" << m_nListenPort << endl;
	cout << "DB Host         :" << m_szDBIP << endl;
	cout << "DB Port         :" << m_nDBPort << endl;
	cout << "DB Name         :" << m_szDBName << endl;
	cout << "DB Account      :" << m_szDBAccount << endl;
	cout << "DB Account Pwd  :" << m_szDBPwd << endl;
	cout << "Loginf Info     :" << m_szLoginInfo << endl;
}

bool CMyConfig::Init( string szProgramName, string szFilename )
{
	if( CMyConfig::m_pInstance == NULL )
	{
		CMyConfig::m_pInstance = new CMyConfig(szFilename, szProgramName);
	}

	return true;
}

void CMyConfig::Exit()
{
	if( CMyConfig::m_pInstance != NULL )
	{
		delete CMyConfig::m_pInstance;
		CMyConfig::m_pInstance = NULL;
	}
}

CMyConfig* CMyConfig::GetInstance()
{
	return CMyConfig::m_pInstance;
}
