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

#include <stdlib.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)
{
	// Config 처리를 위한 객체 생성
	Config conf;
	
	// Config File open
	if( conf.Open( szConfFile) == false )
	{
		errorMessage = "Config file open failed.[" + szConfFile + "]";
		return false;
	}

	// 각 Section , Key 값 파싱 및 저장처리
	std::string szValue;

	// [common]1. 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();
    
	// [common]2. file storage root
	if( conf.GetConfig( PROG_NAME, "FILE_STORAGE_ROOT", szValue ) == false )
	{
		if( !CheckValue(szValue, conf, "COMMON", "FILE_STORAGE_ROOT") )
		{
			errorMessage  = "Config info get failed. [";
			errorMessage += PROG_NAME;
			errorMessage += "]->FILE_STORAGE_ROOT";
			return false;
		}
	}
	stConfig.szStorageRoot = szValue;
	szValue.clear();

	// [common]3. 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();
    
    // [common]4. internal nic name
	if( conf.GetConfig( PROG_NAME, "INTERNAL_NIC_NAME", szValue ) == false )
	{
		if( !CheckValue(szValue, conf, "COMMON", "INTERNAL_NIC_NAME") )
		{
			errorMessage  = "Config info get failed. [";
			errorMessage += PROG_NAME;
			errorMessage += "]->INTERNAL_NIC_NAME";
			return false;
		}
	}
	stConfig.szInternalNicName = szValue;
	szValue.clear();

	// [ftsd]1. worker process count
	if( conf.GetConfig( PROG_NAME, "WORKER_PROCESS_COUNT", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->WORKER_PROCESS_COUNT";
		return false;
	}
	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->WORKER_PROCESS_COUNT";
		return false;
	}
	stConfig.nWorkerProcessCount = atoi(szValue.c_str());
	szValue.clear();
	
	// [ftsd]2. max file send thread count
	if( conf.GetConfig( PROG_NAME, "MAX_FILE_SEND_THREAD", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->MAX_FILE_SEND_THREAD";
		return false;
	}
	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->MAX_FILE_SEND_THREAD";
		return false;
	}
	stConfig.nMaxFileSendThread = atoi(szValue.c_str());
	szValue.clear();
	
	// [ftsd]3. fhs internal ip cache timeout
	if( conf.GetConfig( PROG_NAME, "FHS_INTERNAL_IP_CACHE_TIMEOUT", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->FHS_INTERNAL_IP_CACHE_TIMEOUT";
		return false;
	}
	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->FHS_INTERNAL_IP_CACHE_TIMEOUT";
		return false;
	}
	stConfig.nFhsInternalIpCacheTimeout = atoi(szValue.c_str());
	szValue.clear();

	// [ftsd]4. proper storage refresh preiod(sec)
	if( conf.GetConfig( PROG_NAME, "PROPER_STORAGE_REFRESH_PERIOD", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->PROPER_STORAGE_REFRESH_PERIOD";
		return false;
	}
	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->PROPER_STORAGE_REFRESH_PERIOD";
		return false;
	}
	stConfig.nProperStorageRefresPeriod = atoi(szValue.c_str());
	szValue.clear();
	
	// [ftsd]5. max network bandwidth use amount(%)
	if( conf.GetConfig( PROG_NAME, "MAX_BANDWIDTH_USE", szValue ) == false )
	{
		errorMessage  = "Config info get failed. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->MAX_BANDWIDTH_USE";
		return false;
	}
	if( !CheckValue(szValue, conf, "", "") )
	{
		errorMessage  = "Config info get empty. [";
		errorMessage += PROG_NAME;
		errorMessage += "]->MAX_BANDWIDTH_USE";
		return false;
	}
	stConfig.nMaxBandwidthUse = atoi(szValue.c_str());
	szValue.clear();
	
	// 성공시
	return true;
}

