/***************************************************************************
                   Convert Volume Information Class  ( ConvertVolumeInfo.cpp )
                -----------------------------------------

	begin      : 2013/03/15
	copyright  : (C) 2013 Solbox Inc.
	author     : Development 1 Team
					- 2013/03/15 - 1st dadamin
	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 "rc_mngd.h"
#include "Convert.h"
#include "ConvertVolumeInfo.h"
#include "Logger.h"
#include "UtilFn.h"

CConvertVolumeInfo::CConvertVolumeInfo()
{

}

CConvertVolumeInfo::~CConvertVolumeInfo()
{

}

int CConvertVolumeInfo::ToJobData(int nServiceType, void* pFrom, void *pTo)
{
	int r = 0;
	CJobDataMap *pMap = reinterpret_cast<CJobDataMap *>(pTo);
	
	switch(nServiceType)
	{
	case CServiceInfo::SDK:
		{
			vector<string> body;
			StringSplitEx((char *)pFrom,SDK_DELIMITER, body);
			if (body.size() != 3 )
			{
				r = JOB_ERR_CONVERT;
				LOG(LERR, "Data conversion failed.");
				break;
			}

			vector<string> ids;
			StringSplit(body[0].c_str(), ID_DELIMITER, ids);
			if (ids.size() != 2 )
			{
				r = JOB_ERR_CONVERT;
				LOG(LERR, "Data conversion failed.");
				break;
			}

			LOG(LDBG, "VolumeInfo Body : %s@%s, %s, %s"
				,ids[0].c_str(), ids[1].c_str(), body[1].c_str(), body[2].c_str() );

			//SDKReqBodySpInfo *pBody = (SDKReqBodySpInfo*)pFrom;
			pMap->insert(CJobDataPair(KEY_JOB_TYPE, JOB_VOLUME_INFO));
			pMap->insert(CJobDataPair(KEY_SP_SVC_ID, ids[0]));
			pMap->insert(CJobDataPair(KEY_SP_USER_ID, ids[1]));
			
			pMap->insert(CJobDataPair(KEY_REQ_USER_PASS_CRYPTED, body[1]));
			pMap->insert(CJobDataPair(KEY_REQ_REG_DATE, body[2]));
			r = pMap->size();
		}
		break;
	case CServiceInfo::XML:
	case CServiceInfo::ETC:
	default:
		r = JOB_ERR_CONVERT;
		LOG(LERR, "Service does not support the conversion.");
		break;
	}		
	
	return r;
}

void* CConvertVolumeInfo::ToClientData(int nServiceType, int nCode, void* pFrom, int & outlen)
{
	void* r = NULL;
	CJobDataMap *pMap = reinterpret_cast<CJobDataMap *>(pFrom);

	switch(nServiceType)
	{
	case CServiceInfo::SDK:
		{			
			string maxstorage = GetJobMapValue(pFrom, KEY_MAX_STR_SIZE);
			string used = GetJobMapValue(pFrom, KEY_DIR_USED);

			if(maxstorage.empty() && used.empty() )
			{
				outlen = 0;
				LOG(LWAR, "There is no data to be returned.[%s/%s]", 
					used.c_str(),maxstorage.c_str());
				break;
			}

			char d = SDK_DELIMITER;
			ostringstream outdata;
			outdata  << maxstorage <<  d <<  used;

			BYTE *t = NULL;
			outlen= outdata.str().size();
			t = new BYTE[outlen + 1];
			if(t)
			{
				memcpy(t, outdata.str().c_str(), outlen);
				t[outlen] = '\0';
				r = (void*) t;
				LOG(LDBG, "CConvertVolumeInfo ToClientData(SDK) : %s, %d", (char *)r, outlen);
			}
			else
			{
				outlen = -1;
				LOG(LERR, "Memory allocation error.[%d]", errno);
			}
		}
		break;
	case CServiceInfo::XML:
	case CServiceInfo::ETC:
	default:
		outlen = -1;
		LOG(LERR, "Service does not support the conversion.");
		break;
	}

	return r;
}



