// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently

#pragma once

#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#define _CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES 1
#if defined(_MSC_VER) && _MSC_VER >= 1400
	#define STRNCPY(dest, src, size) strncpy_s(dest, size, src, _TRUNCATE)
#else
	#define STRNCPY(dest, src, size) strncpy(dest, src, size)
#endif

#if defined(_MSC_VER) && _MSC_VER >= 1400
#pragma comment( linker, "/NODEFAULTLIB:\"libcp\"" )
#endif

#define _ATL_APARTMENT_THREADED 
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN		// Exclude rarely-used stuff from Windows headers
#endif

// Modify the following defines if you have to target a platform prior to the ones specified below.
// Refer to MSDN for the latest info on corresponding values for different platforms.
#ifndef WINVER				// Allow use of features specific to Windows 95 and Windows NT 4 or later.
#define WINVER 0x0400		// Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif

#ifndef _WIN32_WINNT		// Allow use of features specific to Windows NT 4 or later.
#define _WIN32_WINNT 0x0400		// Change this to the appropriate value to target Windows 98 and Windows 2000 or later.
#endif						

#ifndef _WIN32_WINDOWS		// Allow use of features specific to Windows 98 or later.
#define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
#endif

#ifndef _WIN32_IE			// Allow use of features specific to IE 4.0 or later.
#define _WIN32_IE 0x0400	// Change this to the appropriate value to target IE 5.0 or later.
#endif

#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS	// some CString constructors will be explicit

// turns off MFC's hiding of some common and often safely ignored warning messages
#define _AFX_ALL_WARNINGS

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes

#include <afxdtctl.h>		// MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>			// MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

#include <afxsock.h>		// MFC socket extensions
#include <afxdhtml.h>        // HTML Dialogs

#include <afxmt.h>

#	pragma comment(lib,"../Lib/TLI.lib")
#	if _MSC_VER < 1400
#		pragma comment(lib,"GCBase_MD_VC71_v1_1.lib")
#		pragma comment(lib,"GenApi_MD_VC71_v1_1.lib")
#	else
#		pragma comment(lib,"GCBase_MD_VC80_v1_1.lib")
#		pragma comment(lib,"GenApi_MD_VC80_v1_1.lib")
#	endif
//#if defined(_DEBUG)
//#	pragma comment(lib,"../Bin/Debug/TLI.lib")
//#	if _MSC_VER < 1400
//#		pragma comment(lib,"GCBase_MDd_VC71_v1_1.lib")
//#		pragma comment(lib,"GenApi_MDd_VC71_v1_1.lib")
//#	else
//#		pragma comment(lib,"GCBase_MDd_VC80_v1_1.lib")
//#		pragma comment(lib,"GenApi_MDd_VC80_v1_1.lib")
//#	endif
//#else
//#	pragma comment(lib,"../Lib/TLI.lib")
//#	if _MSC_VER < 1400
//#		pragma comment(lib,"GCBase_MD_VC71_v1_1.lib")
//#		pragma comment(lib,"GenApi_MD_VC71_v1_1.lib")
//#	else
//#		pragma comment(lib,"GCBase_MD_VC80_v1_1.lib")
//#		pragma comment(lib,"GenApi_MD_VC80_v1_1.lib")
//#	endif
//#endif

#include <Client.h>

#include <GenICam.h>

using namespace GenICam;
using namespace GenICam::Client;

using namespace GenApi;

#define HTONL(x) ( (((x) << 24) & 0xff000000) | (((x) << 8) & 0x00ff0000) | (((x) >> 8) & 0x0000ff00) | (((x) >> 24) & 0x000000ff) )
#define HTONS(x) ( (((x) << 8) & 0xff00) | (((x) >> 8) & 0x00ff) )
inline PCSTR GCStrError( GC_ERROR err )
{
#define _STRE(e) case e: return #e;
	switch( err )
	{
		_STRE(GC_ERR_SUCCESS)
		_STRE(GC_ERR_INVALID_BUFFER_SIZE)
		_STRE(GC_ERR_INVALID_HANDLE)
		_STRE(GC_ERR_INVALID_ID)
		_STRE(GC_ERR_ACCESS_DENIED)
		_STRE(GC_ERR_NO_DATA)
		_STRE(GC_ERR_ERROR)
		_STRE(GC_ERR_INVALID_PARAMETER)
		_STRE(GC_ERR_TIMEOUT)
		_STRE(GC_ERR_INVALID_FILENAME)
		_STRE(GC_ERR_INVALID_ADDRESS)
		_STRE(GC_ERR_FILE_IO)
	}
#undef _STRE
	return "";
}

#include "GigaCamMsg.h"

extern HANDLE _hEventLog;


#include <string>
#include <map>
#include <set>
#include <list>
#include <afxdisp.h>
#include <fstream>
#include "../ZipLib/unzip.h"

using namespace std;

#pragma warning(disable: 4192 4278)

#import "mshtml.tlb"

#import "progid:GenICamExplorer.GenICamExplorerCtrl.1" no_namespace

#import "msxml.tlb"

//#include "UART.h"
#include <atlbase.h>
#include <atlcom.h>
#include <afxdlgs.h>


// Flags for CCP register
#define CCP_NOCONTEXT				0x0000
#define PRIVILEGE_EXCLUSIVE			0x0001		//!< Exclusive control privilege (read-write)
#define PRIVILEGE_CONTROL			0x0002		//!< Control privilege (read-write)
#define PRIVILEGE_MONITOR			0x0004		//!< Monitor privilege (read-only)

#include "WaitDlg.h"

#define TL_CHECK( f, m )\
{\
	GC_ERROR gce = f;\
	if( gce != GC_ERR_SUCCESS )\
	{\
		CWaitDlg::Hide();\
		static char sz[256];\
		sprintf( sz, "%s: %s", m, GCStrError( gce ) );\
		throw string(sz);\
	}\
}

#define HR_CHECK( f, m )\
{\
	HRESULT hr = f;\
	if( FAILED(hr) )\
	{\
		static char sz[256];\
		sprintf( sz, "%s: 0x%08X", m, hr );\
		throw string(sz);\
	}\
}


inline string GetXML( void* hPort, bool bIsDevice )
{
	char szURL[2048];
	size_t cbURL = sizeof(szURL);

	string sManufacturer;

	char szManufacturer[256];
	size_t cbManufacturer = sizeof(szManufacturer);

	if( bIsDevice )
	{
		TL_CHECK( DevGetInfo( hPort, DEVICE_INFO_MANUFACTURER, NULL, szManufacturer, &cbManufacturer ), "Failed to get device manufacturer" )		
	}
	else
	{
		TL_CHECK( GCInfo( NULL, TL_INFO_MANUFACTURER, NULL, szManufacturer, &cbManufacturer ), "Failed to get grabber manufacturer" )
	}

	sManufacturer = szManufacturer;

	TL_CHECK( GCGetURL( hPort, szURL, &cbURL ), "Failed to get URL" );

	_strlwr( szURL );

	CString sPrefix = CString(szURL).SpanExcluding( ":" );

	char* pszXML = NULL;
	size_t length = 0;

	if( sPrefix == "file" )
	{
		string sFilePath = szURL + sPrefix.GetLength() + 1;

		size_t requiredSize;
		getenv_s(&requiredSize, NULL, 0, "GENICAM_ROOT");
		char *str = (char *)malloc(requiredSize * sizeof(char));
		getenv_s(&requiredSize, str, requiredSize, "GENICAM_ROOT");
		if( bIsDevice )
		{
			sFilePath = str + string("xml\\Cameras\\") + "Sony" + "\\" + sFilePath;
		}
		else
		{
			//sFilePath = str + string("xml\\TransportLayers\\") + sManufacturer + "\\" + sFilePath;
			sFilePath = str + string("xml\\TransportLayers\\") + "Sony" + "\\" + sFilePath;
		}
		free(str);

		FILE* pfFile;
		fopen_s( &pfFile, sFilePath.c_str(), "rb" );

		if( ! pfFile )
			throw string( "XML not found" );

		length = _filelength( pfFile->_file );

		pszXML = new char[length+1];

		if( ! pszXML )
			throw string( "Out of memory" );

		if( fread( pszXML, 1, length, pfFile ) < 1 )
		{
			delete pszXML;

			throw string( "Error reading XML" );
		}

		fclose( pfFile );

		pszXML[length] = 0;
	}
	else
	if( sPrefix == "local" )
	{
		string sFileName = CString( szURL + sPrefix.GetLength() + 1 ).SpanExcluding( ";" );
		string sAddress = CString( szURL + sPrefix.GetLength() + 1 + sFileName.length() + 1 ).SpanExcluding( ";" );
		string sLength = CString( szURL + sPrefix.GetLength() + 1 + sFileName.length() + 1 + sAddress.length() + 1);

		uint64_t address;

		char* c;
		
		address = strtoul( sAddress.c_str(), &c, 16 );

		length = strtoul( sLength.c_str(), &c, 16 );

		pszXML = new char[length + 1]; 

		if( ! pszXML )
			throw string( "Out of memory" );

		TL_CHECK( GCReadData( hPort, address, pszXML, &length ), "Failed to read XML" );

		pszXML[length] = 0;
	}
	//else
	//if( sPrefix == "http" )
	//{
	//	string sUrl = szURL;		
	//}
	else
		throw string( "Invalid XML URL prefix: " + sPrefix );

	if( strstr( szURL, ".zip" )  )
	{
		HZIP hZip = OpenZip( pszXML, (DWORD)length, 0 );

		if( ! hZip )
			throw string( "Invalid zipped XML" );

		ZIPENTRY ze;
		
		ZRESULT zr = GetZipItem( hZip, 0, &ze );

		if( zr != ZR_OK )
			throw string( "Invalid zipped XML" );

		char* pszUXML = new char[ze.unc_size + 1];

		if( ! pszUXML )
			throw string( "Out of memory" );

		zr = UnzipItem( hZip, 0, pszUXML, ze.unc_size );

		if( zr != ZR_OK )
			throw string( "Invalid zipped XML" );

		delete pszXML;

		pszUXML[ze.unc_size] = 0;

		pszXML = pszUXML;

		CloseZip( hZip );
	}

	string sXML = pszXML;

	delete pszXML;

	return sXML;
}

#define MAX_FPS 30
#define COUNT_FPS 30

inline INode* GET_NODE( CNodeMapRef& nodeMap, const char* nodeName )
{
	INode* pNode = nodeMap._GetNode( nodeName );

	if( ! pNode )
	{
		char sz[1024];

		sprintf( sz, "GenICam feature '%s' is missing. Please update the XML file.", nodeName );

		throw GenericException( sz, "", 0 );
	}

	return pNode;
}

#include <vfw.h>

#pragma comment(lib, "vfw32.lib")

#ifdef HYPERSTREAM

#include <HyperStream.h>

#include <delayimp.h>

#endif //HYPERSTREAM

#	pragma comment(lib,"../Lib/libtiff.lib")
