

#include "stdafx.h"
#include "ObjectFactory.h"
#include "SystemInfo.h"
#include "IsisScanner.h"
#include "logger.h"



CObjectFactory::CObjectFactory()
	: m_pSystemInfo(nullptr)
{
#ifdef _DEBUG
	spdlog::set_level(spdlog::level::trace);
#else
	spdlog::set_level(spdlog::level::critical);
#endif

	PixRegister("Valgdirektoraret Tonsberg");
	PixdfltInit();
}

CObjectFactory::~CObjectFactory()
{
	if (m_pSystemInfo)
	{
		m_pSystemInfo->Release();
	}
	PixdfltDone();
}

STDMETHODIMP CObjectFactory::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* const arr[] =
	{
		&IID_IObjectFactory
	};

	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i], riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CObjectFactory::CreateScanner(BSTR ScannerModel, BSTR ConfigFilename, IIsisScanner** ppResult)
{
	CComObject<CIsisScanner>* pScanner = nullptr;
	CComObject<CIsisScanner>::CreateInstance(&pScanner);
	const auto hr = pScanner->Init(ScannerModel, ConfigFilename);
	if (FAILED(hr))
	{
		return hr;
	}
	return pScanner->QueryInterface(ppResult);
}

STDMETHODIMP CObjectFactory::CreateScannerByDriverDescription(BSTR DriverDescription, BSTR ConfigFilename, IIsisScanner** ppResult)
{
	CComObject<CIsisScanner>* pScanner = nullptr;
	CComObject<CIsisScanner>::CreateInstance(&pScanner);
	ISystemInfo* pSystemInfo = nullptr;
	CreateSystemInfo(&pSystemInfo);
	BSTR driverName = nullptr;
	pSystemInfo->DriverDescriptionToDriverName(DriverDescription, &driverName);
	const auto hr = pScanner->InitByDriverName(driverName, ConfigFilename);
	::SysFreeString(driverName);
	pSystemInfo->Release();
	if (FAILED(hr))
	{
		return hr;
	}
	return pScanner->QueryInterface(ppResult);
}
STDMETHODIMP CObjectFactory::CreateScannerByDriverName(BSTR DriverName, BSTR ConfigFilename, IIsisScanner** ppResult)
{
	CComObject<CIsisScanner>* pScanner = nullptr;
	CComObject<CIsisScanner>::CreateInstance(&pScanner);
	const auto hr = pScanner->InitByDriverName(DriverName, ConfigFilename);
	if (FAILED(hr))
	{
		return hr;
	}
	return pScanner->QueryInterface(ppResult);
}
STDMETHODIMP CObjectFactory::CreateSystemInfo(ISystemInfo** ppResult)
{
	if (!m_pSystemInfo)
	{
		CComObject<CSystemInfo>* pInfo = nullptr;
		CComObject<CSystemInfo>::CreateInstance(&pInfo);
		const auto hr = pInfo->QueryInterface(&m_pSystemInfo);
		if (FAILED(hr))
		{
			return hr;
		}
	}
	return m_pSystemInfo->QueryInterface(ppResult);
}



