#pragma once

#include "StatisticsWindow.h"
#include "CameraConfigDlg.h"
#include "afxwin.h"
#include "afxcmn.h"
#include "../Includes/registers.h"
#include "MultiSnapDlg.h"	// v1.0.4.0

class CameraPort:
	public IDispatchImpl<_IPort>
{
public:
	CameraPort( DEV_HANDLE hDevice ):
	  m_hDevice(hDevice)
	  {
	  };

	  HRESULT __stdcall QueryInterface( const IID&, void** )
	  {
		  return S_OK;
	  };

	  ULONG __stdcall AddRef()
	  {
		  return S_OK;
	  };

	  ULONG __stdcall Release()
	  {
		  return S_OK;
	  };

      HRESULT __stdcall raw_Read (
		/*[in]*/ unsigned __int64 address,
        /*[out]*/ unsigned char * buffer,
        /*[in]*/ unsigned __int64 length )
	  {
		  if( GC_ERR_SUCCESS == GCReadData( m_hDevice, address, buffer, (size_t*)&length ) )
			  return S_OK;
		  else
			  return E_FAIL;

	  };

      HRESULT __stdcall raw_Write (
		/*[in]*/ unsigned __int64 address,
        /*[in]*/ unsigned char * buffer,
        /*[in]*/ unsigned __int64 length )
	  {
		  if( GC_ERR_SUCCESS == GCWriteData( m_hDevice, address, buffer, (size_t*)&length ) )
			  return S_OK;
		  else
			  return E_FAIL;
	  };


private:
	TL_HANDLE m_hDevice;
};


class _CCameraPort: public GenApi::IPort
{
public:

	_CCameraPort( DEV_HANDLE hDevice ):
	  m_hDevice(hDevice)
	  {
	  };

protected:

	virtual void Read(void *pBuffer, int64_t Address, int64_t Length)
	{
		if( GC_ERR_SUCCESS != GCReadData( m_hDevice, Address, pBuffer, (size_t*)&Length ) )
			throw ACCESS_EXCEPTION( "Failure reading device data" );
	};

    virtual void Write(const void *pBuffer, int64_t Address, int64_t Length)
	{
		if( GC_ERR_SUCCESS != GCWriteData( m_hDevice, Address, pBuffer, (size_t*)&Length ) )
			throw ACCESS_EXCEPTION( "Failure writing device data" );
	};

	virtual EAccessMode GetAccessMode() const
	{
		return RW;
	};

private:
	DEV_HANDLE m_hDevice;
};

class _GrabberPort: public GenApi::IPort
{
public:

	_GrabberPort( TL_HANDLE hTL ):
	  m_hTL(hTL)
	  {
	  };

protected:

	virtual void Read(void *pBuffer, int64_t Address, int64_t Length)
	{
		if( GC_ERR_SUCCESS != GCReadData( m_hTL, Address, pBuffer, (size_t*)&Length ) )
			throw ACCESS_EXCEPTION( "Failure reading grabber data" );
	};

    virtual void Write(const void *pBuffer, int64_t Address, int64_t Length)
	{
		if( GC_ERR_SUCCESS != GCWriteData( m_hTL, Address, pBuffer, (size_t*)&Length ) )
			throw ACCESS_EXCEPTION( "Failure writing grabber data" );
	};

	virtual EAccessMode GetAccessMode() const
	{
		return RW;
	};

private:
	TL_HANDLE m_hTL;
};


// CVideoWindow dialog

class CVideoWindow : public CDialog
{
	DECLARE_DYNAMIC(CVideoWindow)

public:
	CVideoWindow( TL_HANDLE hTL, DEV_HANDLE hDevice, DWORD iChannel, CWnd* pParent = NULL);   // standard constructor
	virtual ~CVideoWindow();

// Dialog Data
	enum { IDD = IDD_VIDEO_DIALOG };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	DECLARE_MESSAGE_MAP()
	HICON m_hIcon;
public:
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	virtual BOOL OnInitDialog();

public:

	void SetUpDrawing( UINT nWidth, UINT nHeight, GEV_PIXEL_TYPE pixelType, bool bMirror );
	void DrawImage( UINT nWidth, UINT nHeight, GEV_PIXEL_TYPE pixelType, PVOID pImage, HDC dc );

private:

	BITMAPINFO* m_pBmpInfo;
	BITMAPINFO* m_pRotatedBmpInfo;
	/*LOGPALETTE* m_pLogPal;
	CPalette m_palette;*/
public:
//	afx_msg void OnPaint();
//	afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//	afx_msg void OnDestroy();
	virtual void OnCancel();

public:
	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
	afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

private:

	void SetScroll( CScrollBar* pScrollBar, UINT nSBCode, UINT nPos );

private:

	int sx;
	int sy;

	UINT m_nImageWidth;
	UINT m_nImageHeight;
public:
	afx_msg void OnSize(UINT nType, int cx, int cy);

public:

	BITMAPINFO* GetBmpInfo(){ return m_pBmpInfo; };
	afx_msg void OnPaint();

private:
	TL_HANDLE m_hTL;
public:
	DEV_HANDLE m_hDevice;

private:

	UINT m_nLastWidth;
	UINT m_nLastHeight;
	PVOID m_pLastImage;

private:
	CToolBarCtrl m_ctlToolBar;
	CImageList m_ilToolBar;
	CStatusBarCtrl m_ctlStatusBar;

private:
	CameraPort m_cameraPort;
	HANDLE m_hGrabberThread;
	DWORD m_idGrabberThread;
	HANDLE m_hGrabberThreadStartedEvent;
	bool m_bStopGrabberThread;
	DS_HANDLE m_hDS;
	UINT m_nImageSize;
	CStatisticsWindow* m_pStatisticsWindow;
	CCameraConfigDlg* m_pConfigurationWindow;
	list<BUFFER_HANDLE> m_buffers;
	CBitmap m_bmpPlay;
	CBitmap m_bmpListen;
	CBitmap m_bmpStop;
	CBitmap m_bmpView;
	CBitmap m_bmpOptions;
	CBitmap m_bmpTools;
	UINT m_nImageTimeout;
	UINT m_nBufferSize;
	bool m_bMirror;
	BYTE* m_pUnpackedImage;
	BYTE* m_pUnpackedImage1;
	BYTE* m_pUnpackedImage2;
	bool m_bShowVideo;
	//CUART m_uart;
	bool m_bIsGood;
	CRITICAL_SECTION m_csLock;
	PVOID m_pCurrentImageData;
	PVOID m_pCurrentRawData;
	bool m_bMonitoring;
	CNodeMapRef m_cameraNodeMap;
	_CCameraPort _m_cameraPort;
	CNodeMapRef m_grabberNodeMap;
	_GrabberPort m_grabberPort;
	string m_sXML;
	DWORD m_nMinInterFrameTime;
	LARGE_INTEGER m_liLastDisplayTime;
	DWORD m_nGrabbed;
	bool m_bForeground;
	bool m_bWarnedAboutHB;
	DWORD m_nMaxFPS;
	DWORD m_iChannel;
	DWORD m_cVideoStreams;
	LARGE_INTEGER m_liFreq;
	list<LARGE_INTEGER> m_imageTimeStamps;
	double m_curFPS;
	DWORD m_lastFPSShowTime;
	ULONGLONG m_nFramesToAcquire;
	bool m_bCalcWB;
	CRect m_rcWB;
	bool m_bWBDrawing;
	struct
	{
		int pr;
		int pg1;
		int pg2;
		int pb;
	} m_wb;
	CSize m_viewPort;
	int m_rotate;
	HDC m_hdc;
	PBYTE m_pRotatedImage;
	bool m_bHS;

public:
	bool m_bPlayOK;
	bool m_bShowWave;
	bool m_bShowAdj;
	GEV_PIXEL_TYPE m_pixelType;

protected:

	static DWORD WINAPI _GrabberThread( PVOID pData )
	{
		static_cast<CVideoWindow*>(pData)->GrabberThread();

		return 0;
	};


	void GrabberThread();

protected:

	bool InitializeAcquisition();
	bool DeinitializeAcquisition();
	bool OpenConfiguration();
	bool CloseConfiguration();
	bool StartAcquisition();
	bool StopAcquisition();

protected:
	void InitializeStats();
	void DeinitializeStats();
	void UpdateStats();
	bool CheckPacketSize();
	void StopGrabbing(EVENT_HANDLE hGCEvent, CCommandPtr& ptrAcquisitionStop, INode* pParamsLocked);
	PBYTE AllocateMemory(size_t n);
	void FreeMemory(PVOID p);

private:

	list<pair<CStat,int> > m_stats;

public:

	//CUART* GetUART() { return &m_uart; };
	bool IsGood() { return m_bIsGood; };
	afx_msg LRESULT OnKickIdle( WPARAM wp, LPARAM lp );
	afx_msg void OnToolsDeviceparameters();
	afx_msg void OnUpdateToolsDeviceparameters(CCmdUI *pCmdUI);
	afx_msg void OnVideoPlay();
	afx_msg void OnVideoStop();
	afx_msg void OnUpdateVideoPlay(CCmdUI *pCmdUI);
	afx_msg void OnUpdateVideoStop(CCmdUI *pCmdUI);
//	afx_msg void OnEnterIdle(UINT nWhy, CWnd* pWho);
	afx_msg void OnToolsUpdatefirmware();
	afx_msg void OnUpdateToolsUpdatefirmware(CCmdUI *pCmdUI);
	afx_msg void OnToolsUartcontrol();
	afx_msg void OnUpdateToolsUartcontrol(CCmdUI *pCmdUI);
	afx_msg void OnToolsSavesnapshot();
	afx_msg void OnUpdateToolsSavesnapshot(CCmdUI *pCmdUI);
	afx_msg void OnViewVideo();
	afx_msg void OnUpdateViewVideo(CCmdUI *pCmdUI);
	afx_msg void OnViewStatistics();
	afx_msg void OnUpdateViewStatistics(CCmdUI *pCmdUI);
	afx_msg void OnOptionsImagetimeout();
	afx_msg void OnUpdateOptionsImagetimeout(CCmdUI *pCmdUI);
	afx_msg void OnOptionsBuffersize();
	afx_msg void OnUpdateOptionsBuffersize(CCmdUI *pCmdUI);
	afx_msg void OnOptionsMirrorview();
	afx_msg void OnUpdateOptionsMirrorview(CCmdUI *pCmdUI);
	afx_msg void OnOptionsMulticastaddress();
	afx_msg void OnUpdateOptionsMulticastaddress(CCmdUI *pCmdUI);
	afx_msg void OnOptionsMaxFPS();
	afx_msg void OnUpdateViewRegister(CCmdUI *pCmdUI);
	afx_msg void OnViewRegister();
	afx_msg void OnUpdateOptionsMaxFPS(CCmdUI *pCmdUI);
	afx_msg void OnViewWaveform();
	afx_msg void OnUpdateViewWaveform(CCmdUI *pCmdUI);
	afx_msg LRESULT OnDisplayError(WPARAM,LPARAM);
	afx_msg LRESULT OnNodeChanged(WPARAM,LPARAM);
	afx_msg LRESULT OnReinitialize(WPARAM,LPARAM);
	CScrollBar m_ctlVScrollBar;
	CScrollBar m_ctlHScrollBar;
//	virtual BOOL OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pLResult);
protected:
	virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);

public:
	GEV_PIXEL_TYPE GetPixelType() { return m_pixelType; };
	static const DWORD WM_DISPLAY_ERROR = WM_USER + 1000;
	static const DWORD WM_REINITIALIZE =  WM_USER + 1001;

public:
	afx_msg void OnOpenanothervideostream0();
	afx_msg void OnOpenanothervideostream1();
	afx_msg void OnOpenanothervideostream2();
	afx_msg void OnOpenanothervideostream3();
	afx_msg void OnOpenanothervideostream4();
	afx_msg void OnOpenanothervideostream5();
	afx_msg void OnOpenanothervideostream6();
	afx_msg void OnOpenanothervideostream7();
	afx_msg void OnOpenanothervideostream8();
	afx_msg void OnOpenanothervideostream9();
	afx_msg void OnOpenanothervideostream10();
protected:

	void OpenVideoStream( DWORD iChannel );

public:
	afx_msg void OnOpenanothervideostreamMore();

private:

	static multiset<DEV_HANDLE> m_openDevices;
	static CCriticalSection m_csOpenDevices;
	CPoint m_ScrollPos;
	int m_iTimeoutMode;	// timeout = TRUE / no timeout = FALSE
	BUFFER_HANDLE m_hBuffer;
	bool m_bMultiSaveFlg;					// v1.0.4.0
	CString m_strFilepath;					// v1.0.4.0
	int m_nFrameSum;						// v1.0.4.0
	int m_nFrameCnt;						// v1.0.4.0
	CMultiSnapDlg* m_pMultiSnapDlg;			// v1.0.4.0
	void Savesnapshot(CString filepath, int frame = 0, bool multi = false);	// v1.0.4.0

public:
	void ChgMultiSaveImage();		// v1.0.4.0

private:

	string GetCaption( DWORD iChannel );
	int SaveTiffFile(char * filename, unsigned char * pimage, DWORD width, DWORD height);

public:
	afx_msg void OnViewTg();
	afx_msg void OnViewAD_L();
	afx_msg void OnViewAD_R();
	afx_msg void OnViewADJUST();
	afx_msg void OnViewAGFRAME();
	afx_msg void OnViewCamCtrl();
	afx_msg void OnViewPartial();
	afx_msg void OnViewModeSet();
	afx_msg void OnViewTrigger();
	afx_msg void OnViewUserMemory();
	afx_msg void OnViewLut();
	afx_msg void OnViewDsp();

public:
	void MKM_SaveBitmap(CString filepath);
	GC_ERROR ReadReg(uint64_t iAddress, DWORD &data);
	GC_ERROR WriteReg(uint64_t iAddress, DWORD data);
	void ViewStatuBarMousePoint(CPoint);
	void FreeWndMemory();

public:
	afx_msg void OnDeviceconfigurationExport();
	afx_msg void OnUpdateDeviceconfigurationExport(CCmdUI *pCmdUI);
	afx_msg void OnDeviceconfigurationImport();
	afx_msg void OnUpdateDeviceconfigurationImport(CCmdUI *pCmdUI);
	afx_msg void OnKamieraCalculatewhitebalance();
	afx_msg void OnUpdateKamieraCalculatewhitebalance(CCmdUI *pCmdUI);
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnZoomZoomin();
	afx_msg void OnUpdateZoomZoomin(CCmdUI *pCmdUI);
	afx_msg void OnZoomZoomout();
	afx_msg void OnUpdateZoomZoomout(CCmdUI *pCmdUI);
	afx_msg void OnZoomFittowindow();
	afx_msg void OnUpdateZoomFittowindow(CCmdUI *pCmdUI);
	afx_msg void OnZoomActualpixels();
	afx_msg void OnUpdateZoomActualpixels(CCmdUI *pCmdUI);
	afx_msg void OnRotateLeft90();
	afx_msg void OnUpdateRotateLeft90(CCmdUI *pCmdUI);
	afx_msg void OnRotateRight90();
	afx_msg void OnUpdateRotateRight90(CCmdUI *pCmdUI);

public:
	void SendCursor(int cursor1, int cursor2);
	void SendCursorDlgMove(int x, int y);
	afx_msg void OnToolsSavemultisnapshot();
};

// CImageTimeoutDlg dialog

class CImageTimeoutDlg : public CDialog
{
	DECLARE_DYNAMIC(CImageTimeoutDlg)

public:
	CImageTimeoutDlg(CWnd* pParent = NULL);   // standard constructor
	virtual ~CImageTimeoutDlg();

// Dialog Data
	enum { IDD = IDD_IMAGE_TIMEOUT };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	void UpdateUI(void);

	DECLARE_MESSAGE_MAP()
public:
	UINT m_nImageTimeout;
	int m_iTimeoutMode;					// timeout = TRUE / no timeout = FALSE
private:
	CSpinButtonCtrl m_ctlSpin;
public:
	virtual BOOL OnInitDialog();
	afx_msg void OnBnClickedCheck1();
public:
	CButton m_chkTimeout;
};
#pragma once


// CBufferSizeDlg dialog

class CBufferSizeDlg : public CDialog
{
	DECLARE_DYNAMIC(CBufferSizeDlg)

public:
	CBufferSizeDlg(CWnd* pParent = NULL);   // standard constructor
	virtual ~CBufferSizeDlg();

// Dialog Data
	enum { IDD = IDD_BUFFER_SIZE };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	DECLARE_MESSAGE_MAP()
public:
	virtual BOOL OnInitDialog();
	UINT m_nBufferSize;
private:
	CSpinButtonCtrl m_ctlSpin;
};
#pragma once


// CMulticastDlg dialog

class CMulticastDlg : public CDialog
{
	DECLARE_DYNAMIC(CMulticastDlg)

public:
	CMulticastDlg(CWnd* pParent = NULL);   // standard constructor
	virtual ~CMulticastDlg();

// Dialog Data
	enum { IDD = IDD_MULTICAST };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	DECLARE_MESSAGE_MAP()
private:
	CComboBox m_ctlMulticastGroup;

public:

	DWORD m_dwAddr;
	virtual BOOL OnInitDialog();
	afx_msg void OnBnClickedUnicast();
	afx_msg void OnBnClickedMulticast();
protected:
	virtual void OnOK();
};
#pragma once


// CMaximumFPSDlg dialog

class CMaximumFPSDlg : public CDialog
{
	DECLARE_DYNAMIC(CMaximumFPSDlg)

public:
	CMaximumFPSDlg(CWnd* pParent = NULL);   // standard constructor
	virtual ~CMaximumFPSDlg();

// Dialog Data
	enum { IDD = IDD_MAX_FPS };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	DECLARE_MESSAGE_MAP()
public:
	virtual BOOL OnInitDialog();
public:
	unsigned int m_nMaxFPS;
	CSpinButtonCtrl m_ctlSpin;
};
#pragma once


// COpenVideoStreamDlg dialog

class COpenVideoStreamDlg : public CDialog
{
	DECLARE_DYNAMIC(COpenVideoStreamDlg)

public:
	COpenVideoStreamDlg(DWORD cVideoStreams, CWnd* pParent = NULL);   // standard constructor
	virtual ~COpenVideoStreamDlg();

// Dialog Data
	enum { IDD = IDD_VIDEO_CHANNEL };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	DECLARE_MESSAGE_MAP()
protected:
	DWORD m_cVideoStreams;
public:
	DWORD m_iChannel;
protected:
	CSpinButtonCtrl m_ctlChannelSpin;
public:
	virtual BOOL OnInitDialog();
};
