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

	begin      : 2015/04/28
	copyright  : (C) 2013 Solbox Inc.
	author     : Development Team
	email      : storage.sd@solbox.com
	version    : 3.5

	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 "DaemonConfigs.h"
#include <cstdlib>
#include <iostream>
#include "Config.h"

CDeamonConfig *CDeamonConfig::m_pInstance = NULL;

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

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;
}

CDeamonConfig::CDeamonConfig(string szFilename, string szProgramName)
	: CCommonConfig(szFilename, szProgramName)
{
	m_nLogServerPort = 0;
}

CDeamonConfig::~CDeamonConfig()
{
}

bool CDeamonConfig::LoadConf()
{
	CCommonConfig::LoadConf();

	std::vector< std::string > vecValue;
#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;
	}

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

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

	// log backup server host
	if( conf.GetConfig( m_szProgramName, "LOG_SERVER_HOST", szValue ) == false )
    {   
        m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->LOG_SERVER_HOST";
        return false;
    }   
    m_szLogServer = szValue;
	szValue.clear();

	// log backup server host
	if( conf.GetConfig( m_szProgramName, "LOG_SERVER_PORT", szValue ) == false )
    {   
        m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->LOG_SERVER_PORT";
        return false;
    }   
    m_nLogServerPort = atoi( szValue.c_str() );;
	szValue.clear();

	// application logfile retention period(unit : day)
	if( conf.GetConfig( m_szProgramName, "APP_LOG_PERIOD", szValue ) == false )
    {   
        m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->APP_LOG_PERIOD";
        return false;
    }   
    m_nAppLogPeriod = atoi( szValue.c_str() );;
	szValue.clear();
	
	// log file searching target directory list
	if( conf.GetConfig( m_szProgramName, "SEARCHING_DIR_LIST", vecValue ) == false )
    {   
        m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->SEARCHING_DIR_LIST";
        return false;
    }   
    m_vecSearchingDirList = vecValue;


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

bool CDeamonConfig::CheckValue()
{
	if (CCommonConfig::CheckValue() == false)
		return false;


	if(m_szLogServer.empty())
	{
		m_szErrMessage  = "Config info get failed. [" + m_szProgramName + "]->LOG_SERVER_HOST";
		return false;
	}

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

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

	return true;
}

void CDeamonConfig::PrintValue()
{
	CCommonConfig::PrintValue();	
}

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

	return true;
}

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

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