/*

Na zacatku tohoto souboru je nastaveni chovani bloku. Pote jsou zde funkce
nezvytne k vytvoreni instance COMu, a funkce, ktera vraci ukazatele na
rozhrani bloku. Prozatim se predpokladaji pouze 2 pridavna rozhrani k tomuto
objektu. 

-- IBlockConfig --
je urcene ke konfiguraci bloku. Je pouzito
v pripade, kdyz uzivatel klikne pravym tlacitkem mysi na blok a vybere
volbu "properties...". Melo by se otevrit okno s nastavenim, ktere musi
byt implementovano

-- IBlockAutoTrigger --
slouzi k automatickemu spousteni procesu. Vyuzije se napriklad pri realtime
zpracovani zvuku a videa, kdy potrebujeme pravidelne zpracovavat useky dat

*/


#include "stdafx.h"
#include "imp_block.h"
//#include "imp_config.h"
//#include "imp_autotrigger.h"
#include <deque>

// jmeno, popis a cesta k bloku
const LPWSTR g_pName = L"Vector combine";
const LPWSTR g_pDesc = L"Combines 2 vectors into one";
const LPWSTR g_pPath = L"Feature Extraction";
// jestli ma blok rohrani, odkomentujte prislusny radek
//#define __BLOCKHAVECONFIGWINDOW
//#define __BLOCKHAVEAUTOTRIGGER
// pocty vstupnich a vystupnich portu
const DWORD g_dwInPortCount = 2;
const DWORD g_dwOutPortCount = 1;
// nazvy vstupnich a vystupnich portu
const WCHAR *g_pInputNameArray[] = {L"VECTOR1", L"VECTOR2"};
const WCHAR *g_pOutputNameArray[] = {L"VECTOR1+2"};

// Tyto funkce nemenit !!!
// funkce volana klientskym programem k vytvoreni instance tridy

HRESULT CreateBlockInstance(REFIID riid, void **ppv)
{
	return CImpBlock::CreateInstance(riid, ppv);
}
// staticka funkce, ktera vytvari instanci tridy
HRESULT CImpBlock::CreateInstance(const IID &riid, void **ppv)
{
	CImpBlock* pObj; 
	pObj = new CImpBlock();
	if (pObj == NULL) 
		return E_OUTOFMEMORY; 
	HRESULT hr; 
	if ( FAILED(hr = pObj->QueryInterface(riid, (void**)ppv))) 
		delete pObj ; 
	return hr; 
}
// funkce, ktera vraci adresu rozhrani
void* CImpBlock::GetInterface(REFIID riid)
{
#ifdef __BLOCKHAVECONFIGWINDOW
	if ( IID_IBlockConfig == riid )
		return m_pxConfig;
#endif
#ifdef __BLOCKHAVEAUTOTRIGGER
	if ( IID_IBlockAutoTrigger == riid )
		return m_pxAutoTrigger;
#endif
	return NULL;
}
// konstruktor
CImpBlock::CImpBlock() : m_pxConfig(NULL), m_pxAutoTrigger(NULL)
{
#ifdef __BLOCKHAVECONFIGWINDOW
	m_pxConfig = new CImpBlockConfig(this);
#endif
#ifdef __BLOCKHAVEAUTOTRIGGER
	m_pxAutoTrigger = new CImpBlockAutoTrigger(this);
#endif
	m_dwInPortCount = g_dwInPortCount;
	m_dwOutPortCount = g_dwOutPortCount;
	m_pName = g_pName;
	m_pDesc = g_pDesc;
	m_pPath = g_pPath;
	m_pInputNameArray = g_pInputNameArray;
	m_pOutputNameArray = g_pOutputNameArray;

	// inicializace promennych

}
// destruktor
CImpBlock::~CImpBlock()
{
#ifdef __BLOCKHAVEAUTOTRIGGER
	if ( m_pxAutoTrigger )
		delete ( m_pxAutoTrigger );
#endif
#ifdef __BLOCKHAVECONFIGWINDOW
	if ( m_pxConfig )
		delete ( m_pxConfig );
#endif
}

///////////////////////////////
// TYTO FUNKCE IMPLEMENTOVAT //
///////////////////////////////

STDMETHODIMP CImpBlock::InitInstance()
{
	CBlock::InitInstance();
	return S_OK;
}

STDMETHODIMP CImpBlock::ExitInstance()
{
	CBlock::ExitInstance();
	return S_OK;
}

STDMETHODIMP CImpBlock::ProcessRev(const DWORD dwProcessId, REVDATAMAP &rRevDataMap)
{
	return S_OK;
}

STDMETHODIMP CImpBlock::ClearRevMap(REVDATAMAP &rRevDataMapClean)
{
	return S_OK;
}

STDMETHODIMP CImpBlock::ProcessData(const LPBLOCKDATA pInputData)
{
	BOOL bRet = TRUE;
	std::deque<std::pair<void*, DWORD> > dqLayers;
	
	DWORD dwLayers = pInputData->dwLayersArray[0];
	if ( pInputData->dwLayersArray[1] > dwLayers )
		dwLayers = pInputData->dwLayersArray[1];
	DWORD dwUsedLayers = 0;

	for ( DWORD dwLayer = 0; dwLayer < dwLayers; dwLayer++ )
	{
		BYTE *pDataSrc = (BYTE*)pInputData->pDataArrayArray[0][dwLayer];
	
		BOOL bL1 = FALSE, bL2 = FALSE;
		DWORD dwTgtLen = 4;
		if ( pInputData->dwLayersArray[0] && 
			pInputData->dwDataLenArrayArray[0][dwLayer] > 4 &&
			dwLayer < pInputData->dwLayersArray[0] &&
			memcmp(pInputData->pDataArrayArray[0][dwLayer], "VCTd", 4) == 0 )
		{
			dwTgtLen += pInputData->dwDataLenArrayArray[0][dwLayer] - 4;
			bL1 = TRUE;
		}
		if ( pInputData->dwLayersArray[1] &&
			pInputData->dwDataLenArrayArray[1][dwLayer] > 4 &&
			dwLayer < pInputData->dwLayersArray[1] &&
			memcmp(pInputData->pDataArrayArray[0][dwLayer], "VCTd", 4) == 0 )
		{
			dwTgtLen += pInputData->dwDataLenArrayArray[1][dwLayer] - 4;
			bL2 = TRUE;
		}

		BYTE *pTgtData = (BYTE*)malloc(dwTgtLen);
		DWORD dwOffset = 4;
		memcpy(pTgtData, "VCTd", 4);
		if ( bL1 )
		{
			memcpy(&pTgtData[dwOffset], &((BYTE*)pInputData->pDataArrayArray[0][dwLayer])[4], pInputData->dwDataLenArrayArray[0][dwLayer] - 4);
			dwOffset += pInputData->dwDataLenArrayArray[0][dwLayer] - 4;
		}
		if ( bL2 )
		{
			memcpy(&pTgtData[dwOffset], &((BYTE*)pInputData->pDataArrayArray[1][dwLayer])[4], pInputData->dwDataLenArrayArray[1][dwLayer] - 4);
			dwOffset += pInputData->dwDataLenArrayArray[1][dwLayer] - 4;
		}

		dqLayers.push_back(std::pair<LPVOID, DWORD>(pTgtData, dwTgtLen));
		dwUsedLayers++;
	}

	LockBlockData();
	DeleteBlockData();
	PrepareBlockData();
	NewBlockPortData(0, dwUsedLayers);
	dwUsedLayers = 0;
	while ( !dqLayers.empty() )
	{
		SetDataPtr(0, dwUsedLayers++, (*dqLayers.begin()).first, (*dqLayers.begin()).second);
		dqLayers.pop_front();
		
	}
	UnlockBlockData();

	return bRet ? S_OK : S_FALSE;
}

STDMETHODIMP_(void) CImpBlock::Serialize(CArchive &ar)
{
	if ( ar.IsStoring() )
	{
		ar;
	}
	else
	{
		ar;
	}
}