//
//  Copyright (c) 2001, Reson, Inc. All Rights Reserved.
//
//  Filename:   6046DryEndDlg.h : header file
//
//  Project:    6046DryEnd
//
//  Author(s):  J. Marcus    
//
//  Purpose:    Defines the main window of the application, as well as most of the processing
//			
//
//  Notes:      
//              
//
//
//
//
//
//
//
#if !defined(AFX_6046DRYENDDLG_H__781A4BCC_EAB1_4FF6_B133_82F2B3BD8CBF__INCLUDED_)
#define AFX_6046DRYENDDLG_H__781A4BCC_EAB1_4FF6_B133_82F2B3BD8CBF__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////
#include "6046DryEnd.h"
/////////////////////////
#include "7kXMLParsor.h"
/////////////////////////
#include "TabManager.h"
#include "DisplayMngr.h"
#include "QuickViewMngr.h"
/////////////////////////
#include "DlgPopupReport.h"
/////////////////////////
#include "StaticEx.h"
#include "Led.h"
#include "xShadeButton.h"
/////////////////////////
#include "XTFReader.h"
#include "7KReader.h"
#include "EdgeTechChannelScale.h"
/////////////////////////
#include "xListControl.h"
/////////////////////////
#include "HistoryBuffer.h"
/////////////////////////
#include <math.h>
/////////////////////////

/////////////////////////

//Timer ID's
///////////////////////////////////////////////////////////////////////
#define  TIME_EVENT			1	//Sys Times 7 record/play time
#define  FILE_READ_EVENT	2	//File playback timer
#define  ALARM_EVENT		3	
#define	 PLC_SYNC_EVENT		4	//For interval sync...see DlgTcp

///////////////////////////////////////////////////////////////////////

//USE FOR BLINKING LEDS ON QV
#define  DEF_QV_UPDATE		600

//Manages the remote window size
typedef enum tagTOGGLESTATE
{
	ALL = 0, 
	MID, 
	TOP
}
TOGGLESTATE;

/////////////////////////////////////////////////////////////////////////////
// CMy6046DryEndDlg dialog

class CMy6046DryEndDlg : public CDialog
{
// Construction
public:

	CMy6046DryEndDlg(CWnd* pParent = NULL);	// standard constructor

	//DELETE THIS LATER!!!
	//This is for testing only
	void	SimulateXMLMsg();

// Dialog Data
	//{{AFX_DATA(CMy6046DryEndDlg)
	enum { IDD = IDD_MY6046DRYEND_DIALOG };
	CxShadeButton	m_ctrlViewAll;
	CxShadeButton	m_ctrlBtnWedge;
	CxShadeButton	m_ctrlBtn2DBathy;
	CxShadeButton	m_ctrlMute;
	CxShadeButton	m_ctrlBtnHealth;
	CxShadeButton	m_ctrlBtnTextOnly;
	CxShadeButton	m_ctrlBtnSpeeds;
	CxShadeButton	m_ctrlViewReport;
	CxShadeButton	m_ctrlRefresh;
	CxShadeButton	m_ctrlBtnModules;
	CxShadeButton	m_ctrlToggle2;
	CxShadeButton	m_ctrlBtnSummary;
	CxShadeButton	m_ctrlBtnGeneral;
	CxShadeButton	m_ctrlBtnSBP;
	CxShadeButton	m_ctrlBtnHFWF;
	CxShadeButton	m_ctrlBtnLFWF;
	CxShadeButton	m_ctrlBtnHeading;
	CxShadeButton	m_ctrlBtnDepth;
	CxShadeButton	m_ctrlBtnAttitude;
	CxShadeButton	m_ctrlToggle;
	CxShadeButton	m_ctrlQuit;
	CxShadeButton	m_ctrlStop;
	CxShadeButton	m_ctrlRecord;
	CLed			m_ctrlLedHealth;
	CLed			m_ctrlLedRunlist;
	CLed			m_ctrlLedReport;
	CLed			m_ctrlLedModules;
	CLed			m_ctrlLedGeneral;
	CLed			m_Led1;
	CComboBox		m_ctrlMode;
	CStatic			m_ctrlToggleTop;
	CStatic			m_ctrlToggleMid;
	CStaticEx		m_ctrlRecTime;
	CStaticEx		m_ctrlSysTime;
	CxListControl	m_ctrlQVWnd;
	CTabManager		m_ctrlMainTabs;
	CString			m_strSysTime;
	CString			m_strRecTime;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMy6046DryEndDlg)
	public:
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	//{{AFX_MSG(CMy6046DryEndDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnDestroy();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnMainBtnQuit();
	afx_msg void OnMainBtnToggle();
	afx_msg void OnMainDsplySbp();
	afx_msg void OnBtnRecord();
	afx_msg void OnBtnStop();
	afx_msg void OnMainDsplyHfwf();
	afx_msg void OnMainDsplyLfwf();
	afx_msg void OnMainDsplyAttitude();
	afx_msg void OnSelchangeMainModeCombo();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMainDsplyDepth();
	afx_msg void OnMainDsplyHeading();
	virtual void OnOK();
	afx_msg void OnMainBtnToggle2();
	afx_msg void OnQvGeneral();
	afx_msg void OnQvModules();
	afx_msg void OnQvRunlist();
	afx_msg void OnQvRefresh();
	afx_msg void OnBtnViewreport();
	afx_msg void OnBtnAltquit();
	afx_msg void OnBtnMinimize();
	afx_msg void OnMainDsplyNavigation();
	afx_msg void OnMainDsplyText();
	afx_msg void OnQvHealth();
	afx_msg void OnButtonMute();
	afx_msg void OnMainDsply2dbathy();
	afx_msg void OnMainDsplyWedge();
	afx_msg void OnClose();
	afx_msg void OnCaptureChanged(CWnd *pWnd);
	afx_msg void OnButtonShowall();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()

////////////////////////////////////////////////////////////////////////////////////
//  BEGIN MY STUFF
////////////////////////////////////////////////////////////////////////////////////

    //File Reading objects
public:

	CXTFReader					m_xtfReader;		
	C7KReader					m_7kReader;	
	
	C7kXMLParsor				m_xmlParsor;
	
	//Display managers
	CDisplayMngr				m_DspManager;
	CQuickViewMngr				m_QVManager;
	EMESSAGETYPE				m_QVMsgType;
	
	BOOL						m_bRemoteConnected;

	//DISPLAY FLAGS
	bool						m_bWaterfallHIGH, m_bWaterfallLOW, m_bWaterfallSBP;
	bool						m_bAttitude, m_bDepth, m_bHeading, m_bNav, m_bTextOnly, m_bBathy, m_bImagery;

private:
	
	//Remote 6046 Interface Object
	C6046RemoteClient			m_6046RemoteClient;
	
	//Status popup Dialog
	CDlgPopupReport				*m_dlgPopupReport;
	BOOL						m_bPopupReport;

	//RECORD PARSING HELPERS
	C7kProtocol					m_7kProtocol;
	C7kStatus					m_7kStatus;
	C7kAcknowlege			    m_7kAckNack;

	//////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////
	//RECORD PROCESSING HEADERS ETC...
	//////////////////////////////////////////////////////////
	PRECORDHEADER7K						m_p7kHeader;		// DRF
    BYTE								*pDynamicBytes;
	ULONG								dynSize;
	//SSS
	PSIDESCANRECORDTYPEHEADER           psRTH;				// SIDESCANRECORDTYPEHEADER OR SUBBOTTOMRECORDTYPEHEADER
    PCHANNELINFO	                    psPortChanInfo;		// CHANNELINFO PORT
    BYTE *								pbyPortImagery;		// Raw Imagery PORT
    PCHANNELINFO						psStbdChanInfo;		// CHANINFOHEADER STBD
    BYTE *								pbyStbdImagery;		// Raw Imagery STBD  
	SidescanHeaderType *	            psETSSHeaderPort;
    SidescanHeaderType *	            psETSSHeaderStbd;	
	//SBP
	PCHANNELINFO						psSBPChanInfo;
	BYTE *								pbySBPImagery;
	SegyDataType *						psETSBPHeader;
	//NAV
	NBlueFin7kMessages::PBLUEFINRECORDTYPEHEADER			psBlueFinHeader;
	NBlueFin7kMessages::PBLUEFINENVIRONMENTMESSAGE			psBlueFinENV;
	NBlueFin7kMessages::PBLUEFINNAVIGATIONMESSAGE			psBlueFinNAV;		
	NBlueFin7kMessages::PBLUEFINNETWORKHEADER				psBlueFinNet;
	//////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////



	//Scaling ping data
	CEdgeTechChannelScale		m_ChanScaleHFSSPort;
	CEdgeTechChannelScale		m_ChanScaleHFSSStbd;
	CEdgeTechChannelScale		m_ChanScaleLFSSPort;
	CEdgeTechChannelScale		m_ChanScaleLFSSStbd;
	CEdgeTechChannelScale		m_ChanScaleSBP;

	//Record storage
	CHistoryBuffer				m_buff7kDataRecords;
	CHistoryBuffer				m_buff7kSysRecords;
	RECORD						m_recReuseRecord;
	
	//Configuration
	CSystemConfig				*m_pConfig;								//Object is in the app class

	//Critical section manages thread safety
	CRITICAL_SECTION			CriticalSection;

	//DATA EXTRACTION BUFFERS - used in RealTimeHandler Function
	short						*Data16Bit;
	BYTE						*Data8Bit;
	ULONG						m_ulMaxBuffer;
	UINT						m_HFSSPingsProcessed;
	UINT						m_LFSSPingsProcessed;

	//Maintain which QV Led was last lit, so we can re-dim
	CLed*						m_pLedLastLit;
		
	//TIME MNGMT VARS
    CTimeSpan					elapsed;
	char						timeBuff[128];
	time_t						RealTime, StartTime;
	SYSTEMTIME					m_SYSTIME;
	
	BOOL						m_bPlaying;
	BOOL						m_bFolderPlay;
	BOOL						m_bAllFilesPlayed;
	BOOL						m_bLogging;
	
	int							hour,min,sec,total;
	CString						m_str6046ON, m_str6046OFF;	//objects to receive strings from string table
	

	DISPLAY_TYPE				m_DspType;					//used to notify which button was pressed

	//WINDOW TOGGLE VARS
	TOGGLESTATE					m_ToggleState;
	CRect						m_rectAll, m_rectMid, m_rectTop;
	
	//SYSTEM MODE VARS
	MODE_STATE					modeState;					//Playback or Collect
	BOOL						bXTFPlayerInitialized;	//either XTF or 7K file
	BOOL						b7KPlayerInitialized;
	BOOL						m_bEOFReached;

	//UI OPTIONS
	BOOL						m_bSyncTime;
	UINT						requestedPlaySpeed;

	//TIMER IDS
	UINT						m_uiTimeTimerID;
	UINT						m_uiAlarmTimerID;
	UINT						m_uiFileTimerID;
	UINT						m_uiPLCSyncTimerID;


	////////////////////////
	//	BEGIN FUNCTIONS   //
	////////////////////////

public:
	
	///////////////////////////////////////////////////////////////////////////////////////
	//FILE PLAYBACK
	void						BeginPlay				(	BOOL	folder,	  //IF TRUE, PATH IS FOLDER
															CString name,	  //PATH
															int playSpeed);   //Called from DlgPlayback

	void						KillPlay				(	void );

	void						ModifyPlaySpeed			(	int newSpeed);						   //Modify the update speed

	void						CloseAllOpenFiles		(	void );

	///////////////////////////////////////////////////////////////////////////////////////
	//INITIALIZATION HELPERS
	
	void						LoadImages				(	void );
	
	void						InitToggle				(	void );
	
	void						InitCtrlBarItems		(	void );
	
	void						InitTabs				(	void );
	
	void						InitAlarmLeds			(	void );
	
	void						InitQVLeds				(	void );
	
	void						InitButtons				(	void );

	void						LoadSettings			(	void );
	
	void						SaveSettings			(	void );

	///////////////////////////////////////////////////////////////////////////////////////
	//CALLBACS FOR DATA ETC

	static void					CallbackSystem			(	void			*pvParam,      // Optional parameter. Typically client's this pointer.
															unsigned char	*pbyData,      // Pointer to data.
															unsigned long   ulBytes );	   // Number Bytes in data


	static void					CallbackData			(	void			*pvParam,      // Optional parameter. Typically client's this pointer.
															unsigned char   *pbyData,      // Pointer to data.
															unsigned long   ulBytes );	   // Number Bytes in data
	
	///////////////////////////////////////////////////////////////////////////////////////
	//HELPER FUNCTIONS - 
private:
	
	void						RealTimeHandler			(	void );							//Data
	
	void						SysDataHandler			(	void );							//System Alerts/etc

	
public:
	void						EnableEdgetechScaling	(	UINT			type, 
															BOOL bEnable);

	void						ResetScaling			(	UINT type);

	
	void						SetTimeSync				(	BOOL			bEnable);

	void						AutoPLCSync				(	BOOL			start,
															UINT			seconds );

	void						GetSyncedInfo			(	QV_GENERAL	    *info );

	
	void						FlipOrientation			(	BOOL			bFlip );	//projector orientation is handled in display only
	
	///////////////////////////////////////////////////////////////////////////////////////
	//VARIOUS SYSTEM HELPERS

	//	ALARMS - add alarm adds to the alarm counter, when the user acknowledges alarms,
	//call SubtractAlarm().  When alarm count ==0, LED and Sound become inactive
	UINT						m_uiInternalAlarmCount;

	void						AddAlarm				(	void );
	
	void						SubtractAlarm			(	void );

	void						ClearAllAlarms			(	void );
	
	void						MuteCurrentAlarm		(	void );
	

	///////////////////////////////////////////////////////////////////////////////////////
	//	REMOTE 6046 DLL CALLS
	BOOL						RemoteConnect			(	char			*pszRemoteAddress, 
															unsigned int	port);
	BOOL						RemoteDisconnect		(	void );
	
	BOOL						RemoteSubscribe			(	UINT			sensorIndex, 
															ULONG			recordType );

	BOOL						RemoteUnsubscribe		(	UINT			sensorIndex, 
															ULONG			recordType );
	
	void						RemoteGetPulseFiles		(   int             riSensorIndex ); 

	void						RemoteLogging			(   bool            rbEnable );  
	
	void						RemoteLoggingSetup		(	void );

	void						RemoteLoggingSetup		(	ULONG			fileMaxSize,
															ULONG			maxOverlapRecords );
    
	void						RemoteSwitchFileName	(   char            *pszFileName = NULL );                  
    
	BOOL						RemoteSetPath           (   char            *pszPath );                             
    
	void						RemoteAlarm             (   int				alarmID,
															C6046RemoteClient::EALARMACTION      reAlarmAction );
    
	void						RemoteReportAlarms		(	void );

	void						RemoteVersion           (   void );
    
	void						RemoteLoad              (   void );
    
	void						RemoteSave              (   void );
    
	void						RemoteStatus            (   int             riSensorIndex );
    
	void						RemoteVerbose           (   bool            rbEnable );
    
	void						RemoteReset             (   int             riSensorIndex );
    
	void						RemoteShutdown          (   int             riMode );                            
    
	void						RemoteModules           (   void );
    
	void						RemoteRunList           (   void );
    
	BOOL						RemoteAdd               (   int				riModuleNumber,
															int				riSubsystemId,
															int             riPortNumber,
															LPCTSTR         pszName  );
    
	BOOL						RemoteRemove            (   int				riSensorIndex );
    
	void						RemoteReport            (   void );
    
	void						RemotePort              (   int				riSensorIndex,
															int				riPort );

	void						RemoteHealth			(	void );

	void						RemoteAutoStart			(	DWORD			mode );

	void						RemoteAutoHealthCheck	(	bool			enable,
															ULONG			secs );
	
	void						RemotePingEnable		(	UINT			sensorIndex,  
															BOOL			bEnable );
	
	void						RemoteSetRange			(	UINT			sensorIndex,	 
															float			value);
	
	void						RemoteSetPulse			(	UINT			sensorIndex,	 
															CString			file );

	void						RemoteSetPulseWidth		(	UINT			sensorIndex, 
															float			pulseWidth );

	
	void						RemoteSetPower			(	UINT			sensorIndex,	 
															float			value );
	
	void						RemoteSetDuration		(	UINT			sensorIndex, 
															float			value );
	
	void						RemoteSetGain			(	UINT			sensorIndex,	 
															int				value );

	void						RemoteSetMaxRate		(	UINT			sensorIndex,
															float			value );

	void						RemoteAliveMonitor		(	bool			enable );

	void						RemoteGetConfigInfo		(	int				sensorIndex );

	void						RemoteGetCurrentSettings(	int				sensorIndex );

	void						RemoteGetParameters		(   int				sensorIndex );

	void						RemoteRangeDepthFilters (	int				sensorIndex, 
															float			minRange,
															float			maxRange,
															float			minDepth,
															float			maxDepth );

	void						RemoteBottomDetectFlag	(	int				sensorIndex,
															ULONG			flags );


	////////////////////////////////////////
	//Other Helper Funtions
	void						ForceClose				(	DISPLAY_TYPE	type );

	void						ToggleAlarmsDisplay		(	void );

private:
	//other helpers, nonprocessing intensive
	CDC							dcImage, dcTrans;
  	CBitmap						bmreson;
	CBitmap						bitmapTrans;
  	BITMAP						bminfo;
	int							nWidth,nHeight;
	ULONG						m_ulLastBeams;

	void						DrawLogo				(   CDC				*pDC, 
															int				x, 
															int				y, 
															COLORREF		crColour );

	NBlueFin7kMessages::PBLUEFINNAVIGATIONMESSAGE	SimulateBlueFin (	void );
	NBlueFin7kMessages::BLUEFINNAVIGATIONMESSAGE	m_BFSimMsg;
	
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_6046DRYENDDLG_H__781A4BCC_EAB1_4FF6_B133_82F2B3BD8CBF__INCLUDED_)
