/***************************************************************************
                   Convert Anonymous Directory Class  ( ConvertAnonymous.cpp )
                -----------------------------------------

	begin      : 2013/03/18
	copyright  : (C) 2013 Solbox Inc.
	author     : Development 1 Team
					- 2013/03/18 - 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 "ConvertAnonymousDir.h"
#include "Logger.h"
#include "UtilFn.h"

CConvertAnonymosDir::CConvertAnonymosDir(string subkey /* = "" */)
	: m_subkey(subkey)
{

}

CConvertAnonymosDir::~CConvertAnonymosDir()
{

}

int CConvertAnonymosDir::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;
			}


			pMap->insert(CJobDataPair(KEY_JOB_TYPE, JOB_ANONY_DIR));
			pMap->insert(CJobDataPair(KEY_JOBSUB_TYPE, m_subkey));
			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]));

			int nAuthRead = body[0].size() + 1 + body[1].size() + 1 + body[2].size();

			LOG(LDBG, "AnonymosDir auth : (%d)%s@%s, %s, %s", nAuthRead, 
				ids[0].c_str(), ids[1].c_str(), body[1].c_str(), body[2].c_str());						

			if(m_subkey.compare(JOB_ANONY_DIR_GET) != 0)
			{

				ostringstream szDataNum;
				szDataNum << (ntohl(*((int *)((char*)pFrom + nAuthRead))));

				// body start position : auth + data count(4Byte)
				ostringstream uri;
				int bodypos = nAuthRead+sizeof(int);
				char *p = (char*)pFrom + bodypos;
				int nextpos = 0;
				for( int i = 0; i < atoi(szDataNum.str().c_str()); ++i)
				{
					if ( i > 0 )
						uri << SDK_DELIMITER;

					LOG(LDBG, "CConvertAnonymosDir::ToJobData Body(%d) %s"
						, i,(char*) p + nextpos);

					uri << (char*)p + nextpos;			

					nextpos += MAX_SIZE_OF_FILENAME; // old 
				}

				LOG(LDBG, "AnonymosDir Data :(%s)%s"					
					, szDataNum.str().c_str(), uri.str().c_str());		

				pMap->insert(CJobDataPair(KEY_REQ_DATA_NUM, szDataNum.str()));
				pMap->insert(CJobDataPair(KEY_REQ_URI, uri.str().c_str()));
			}


			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* CConvertAnonymosDir::ToClientData(int nServiceType, int nEnd, void* pFrom, int & outlen)
{
	void *r = NULL;
	CJobDataMap *pMap = reinterpret_cast<CJobDataMap *>(pFrom);

	switch(nServiceType)
	{
	case CServiceInfo::SDK:
		{
			if(m_subkey.compare(JOB_ANONY_DIR_GET) == 0)
			{
				string ruri = GetJobMapValue(pFrom, KEY_RES_URI);
				if(ruri.empty())
				{
					outlen = 0;
					LOG(LWAR, "There is no data to be returned.[%s]",JOB_ANONY_DIR_GET);
					break;
				}

				vector<string> rdata;
				char d = SDK_DELIMITER;
				int len = sizeof(int);

				StringSplitEx(ruri, d, rdata);

				outlen = len + (rdata.size() * MAX_SIZE_OF_FILENAME);
				BYTE *t = NULL;
				t = new BYTE[outlen + 1];
				if(t)
				{
					memset(t, 0,outlen + 1);

					*((int *)(t)) = htonl(rdata.size());
					for (int i = 0; i < (int)rdata.size(); i++) 
					{
						LOG(LDBG, "ToClientData src(%d) : %s",i, rdata[i].c_str());
						//snprintf((t + len), MAX_SIZE_OF_FILENAME - 1, "%s", rdata[i].c_str());
						memcpy((t + len), rdata[i].c_str(), MAX_SIZE_OF_FILENAME);
						len += MAX_SIZE_OF_FILENAME;
					}

					t[outlen] = '\0';
					r = (void*) t;
					LOG(LDBG, "CConvertAnonymosDir ToClientData(SDK) : %s, %d", (char *)r, outlen);
				}
				else
				{
					outlen = -1;
					LOG(LERR, "Memory allocation failure.");
				}
			}
			else if(m_subkey.compare(JOB_ANONY_DIR_SET) == 0 || m_subkey.compare(JOB_ANONY_DIR_UNSET) == 0 )
			{
				outlen = 0;
			}
			else 
			{
				outlen = -1;
				LOG(LERR, "Service does not support the conversion.");
			}
		}
		break;
	case CServiceInfo::XML:
	case CServiceInfo::ETC:
	default:
		outlen = -1;
		LOG(LERR, "Service does not support the conversion.");
		break;
	}

	return r;
}
