#include "fhs_raidmond.h"
#include "Config.h"
#include "GetConfig.h"

/// @brief config value chech
/// @param szValue	[in] value
/// @param conf [in] Config 객체
/// @param defsection [in] default section
/// @param defkey [in] default key
/// @return 성공시 true, 오류 발생시 false 를 반환.
static bool CheckValue(std::string& szValue, Config& conf,  const std::string& defsection, const std::string& defkey)
{
	if( szValue.size() > 0 ) 
		return true;

	if( defsection.size() <= 0 || defkey.size() <= 0 ) 
		return false;

	conf.GetConfig( defsection, defkey, szValue );

	if( szValue.size() > 0 ) 
		return true;

	return false;
}


bool GetConfig( const std::string& szConfFile, struct st_config& stConfig, std::string& errorMessage )
{
	bool ret = false;

	// Config 처리를 위한 객체 생성
	Config conf;

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

	std::string szValue;
    szValue.clear();

	// log path
	if( conf.GetConfig( PROG_NAME, "DEFAULT_LOG_DIR", szValue ) == false )
	{
		if( !CheckValue(szValue, conf, "COMMON", "DEFAULT_LOG_DIR") )
		{
			errorMessage  = "Config info get failed. [";
			errorMessage += PROG_NAME;
			errorMessage += "]->LOG_DIR";
			return false;
		}
	}
	stConfig.szLogDir = szValue;

    szValue.clear();
	// log level
	if( conf.GetConfig( PROG_NAME, "LOG_LEVEL", szValue ) == false )
	{
		if( !CheckValue(szValue, conf, "COMMON", "LOG_LEVEL") )
		{
			errorMessage  = "Config info get failed. [";
			errorMessage += PROG_NAME;
			errorMessage += "]->LOG_LEVEL";
			return false;
		}
	}
	stConfig.nLogLevel = atoi(szValue.c_str());

    szValue.clear();
	// check term
	if( conf.GetConfig( PROG_NAME, "HEALTH_CHECK_TERM", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->HEALTH_CHECK_TERM";
		return false;
	}
	
	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->HEALTH_CHECK_TERM";
		return false;
	}
	stConfig.nCheckTerm = atoi(szValue.c_str());

    szValue.clear();
	// report port 
	if( conf.GetConfig( PROG_NAME, "REPORT_THREAD_PORT", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->REPORT_THREAD_PORT";
		return false;
	}

	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->REPORT_THREAD_PORT";
		return false;
	}
	stConfig.nReportPort = atoi(szValue.c_str());
	
    szValue.clear();
	//event level  
	if( conf.GetConfig( PROG_NAME, "EVENT_LEVEL", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->EVENT_LEVEL";
		return false;
	}
	
	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->EVENT_LEVEL";
		return false;
	}
	stConfig.nEventLevel = atoi(szValue.c_str());
    szValue.clear();
	//sms send host 
	if( conf.GetConfig( PROG_NAME, "SMS_SEND_HOST", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->SMS_SEND_HOST";
		return false;
	}

	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->SMS_SEND_HOST";
		return false;
	}
	stConfig.szSMSHost = szValue;

    szValue.clear();
	//sms send port
	if( conf.GetConfig( PROG_NAME, "SMS_SEND_PORT", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->SMS_SEND_HOST";
		return false;
	}

	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->SMS_SEND_PORT";
		return false;
	}
	stConfig.nSMSPort = atoi(szValue.c_str());

    szValue.clear();
	//sms send file
	if( conf.GetConfig( PROG_NAME, "SMS_SEND_URI", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->SMS_SEND_FILE";
		return false;
	}

	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->SMS_SEND_FILE";
		return false;
	}
	stConfig.szSMSSendfile = szValue;
    szValue.clear();
	ret = true;

	return ret;
}

