/*****************************************************************************
Open Source MIT License

Copyright (c) 2017 Matthew Ahmed

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*****************************************************************************/

#if !defined(AFX_ZDIALOG_H__59CE5781_FE9B_11D7_97F8_00B0D0468F18__INCLUDED_)
#define AFX_ZDIALOG_H__59CE5781_FE9B_11D7_97F8_00B0D0468F18__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ZDialog.h : header file
//


#include "Settings.h"
#define SERROR_MAX    8191


// For fnWaitForMsg & fnYield
#define WFM_ERROR     -1
#define WFM_TIMEOUT   0
#define WFM_GOTMSG    1

// For fnFindExtent etc
//#define FE_FOUND    1
//#define FE_NOTFOUND 0
//#define FE_ERROR    -1

//struct ZWinAppData; // forward declaration
typedef struct ZWinAppDataClone
{
	TCHAR sProgDirSlash[2048]; // Larger for possible error message getting paths
  TCHAR sExeNameNoDotExt[_MAX_PATH];
  TCHAR sAppDocsDirSlash[_MAX_PATH];
  TCHAR sAppDataDirSlash[_MAX_PATH];
} ZWinAppDataClone;

/////////////////////////////////////////////////////////////////////////////
// CZDialog dialog

class CZDialog : public CDialog
{
// Construction
public:
	CZDialog(INT id_dlg, LPCTSTR sCapt = NULL, CWnd* pParent = NULL,
						CSettings * pSet = NULL, ZWinAppDataClone * pzwd = NULL);
  CZDialog::~CZDialog();

// Dialog Data
	//{{AFX_DATA(CZDialog)
//	enum { IDD = IDD_ZBASE };
	enum { IDD = 666 };
		// NOTE: the ClassWizard will add data members here
	//}}AFX_DATA

//	static int CALLBACK BrowseForFolderCallback(HWND hwnd, UINT uMsg,
//																			LPARAM lParam,  LPARAM lpData);

//virtual int PublicProcessString(LPCTSTR sStr); // Does nothing, overidable

void MB(LPCTSTR sMsg1, LPCTSTR sMsg2 = NULL, LPCTSTR sMsg3 = NULL, 
				LPCTSTR sMsg4 = NULL, LPCTSTR sMsg5 = NULL, LPCTSTR sMsg6 = NULL);

void MB(UINT uType, LPCTSTR sMsg1, LPCTSTR sMsg2 = NULL, LPCTSTR sMsg3 = NULL, 
				LPCTSTR sMsg4 = NULL, LPCTSTR sMsg5 = NULL, LPCTSTR sMsg6 = NULL);

BOOL fnErr(LPCTSTR sErr1, LPCTSTR sErr2 = NULL, LPCTSTR sErr3 = NULL,
					 LPCTSTR sErr4 = NULL, LPCTSTR sErr5 = NULL, LPCTSTR sErr6 = NULL);

void fnVoid(LPCTSTR sErr1, LPCTSTR sErr2 = NULL, LPCTSTR sErr3 = NULL,
					  LPCTSTR sErr4 = NULL, LPCTSTR sErr5 = NULL, LPCTSTR sErr6 = NULL);

/*
BOOL fnFindExtent(LPCTSTR sSource, LPCTSTR sBegin, LPCTSTR sEnd, INT * npBegin,
                  INT * npFound, BOOL bIgnoreCase = FALSE);

BOOL fnFindExtent(LPCTSTR sSource, LPCTSTR sBegin, LPCTSTR sEnd, INT * npBegin,
                  INT * npFound, CString & csFound, BOOL bIgnoreCase = FALSE);

BOOL fnReplaceExtent(CString & csSource, LPCTSTR sBegin, LPCTSTR sEnd,
                     LPCTSTR sReplace, BOOL bIgnoreCase = FALSE);

BOOL fnReadTextFile(LPCTSTR sPath, CString & csReturn,
                    INT nMinBytes = 600, INT nMaxBytes = 131072);

BOOL fnWriteTextFile(LPCTSTR sPath, LPCTSTR sText, CString & csErrMsg);
*/
// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CZDialog)
	//}}AFX_VIRTUAL

// Implementation
protected:
//	BOOL PickFolder(CString & csTarget, LPCTSTR sTitle = NULL,
//									LPCTSTR sText = NULL, LPCTSTR sStartDir = NULL);

	CSettings * m_pSet;
  CWnd * m_pParent;

  ZWinAppDataClone * m_pzwd;
	
	CString m_csCapt;
	CString m_csCaptMain;
	CString csBS;
	CString csBS2;
	TCHAR sBS[2048];
	TCHAR sBS2[2048];
	INT nBS;
	INT nBS2;
	INT nInd;
	UINT uInd;
	CString csrn;
	CString csn;
  LPCTSTR sDigits;

/*********************************************************************************
 Hack for converting ZDlg class to MFC. Copy ZWin/ZDlg functions used to here: 
**********************************************************************************/

  BOOL ToLog(LPCTSTR sMsg1, LPCTSTR sMsg2 = NULL, LPCTSTR sMsg3 = NULL);
  BOOL m_bFlushLog;
  TCHAR m_sError[SERROR_MAX + 1];
  BOOL SetError(LPCTSTR sMsg1, LPCTSTR sMsg2 = NULL, LPCTSTR sMsg3 = NULL,
					   LPCTSTR sMsg4 = NULL, LPCTSTR sMsg5 = NULL, LPCTSTR sMsg6 = NULL);
  BOOL AppendError(LPCTSTR sMsg1, LPCTSTR sMsg2 = NULL, LPCTSTR sMsg3 = NULL,
					   LPCTSTR sMsg4 = NULL, LPCTSTR sMsg5 = NULL, LPCTSTR sMsg6 = NULL);
  void ClearError();
  BOOL SetSysErr(LPCTSTR sErr1, LPCTSTR sErr2 = NULL,
									LPCTSTR sErr3 = NULL, LPCTSTR sErr4 = NULL);

  HANDLE m_hfLog;
  BOOL StartLog(LPCTSTR sFileName = NULL, LPCTSTR sMsg = NULL, BOOL bFlush = FALSE); 
  BOOL CloseLog(LPCTSTR sMsg = NULL);
  BOOL WriteLineToLog(LPCTSTR sLine, BOOL bTimeStamp = TRUE,
                      BOOL bAddRN = TRUE);
  BOOL SelectFolder(HWND hOwner, LPTSTR sReturn, LPCTSTR sTitle = NULL,
                      LPCTSTR sDefault = NULL, INT nCsidlRoot = 0);

  void zPeekMessage(void);

	BOOL ReadReg(HKEY hOpen, LPCTSTR sKey, LPCTSTR sValue, LPTSTR sTarget,
												BOOL bErrMsg = FALSE, DWORD dwSize = _MAX_PATH);

  BOOL LogRaw(LPCTSTR sLine, BOOL bAddRN = TRUE);

/*****************************************************************************
  END INSERT COPIED FROM ZWin
*******************************************************************************/


/*
  // Only used if descendent changing cursors
  ULONG_PTR m_ulpOldCursor;

	int fnWaitForMsg(UINT uMsg, UINT uTimeOut, WPARAM * pWp = NULL,
													LPARAM * pLp = NULL, UINT uTimerID = 789);

	inline BOOL fnYield(UINT uDelay = 10, UINT uTimerID = 678)
	{
		//      Here, 0 (i.e. WFM_TIMEOUT) is the expected fnWaitForMsg return
		return (fnWaitForMsg(0, uDelay, NULL, NULL, uTimerID)) ? FALSE : TRUE;
	}

	inline BOOL fnYield(LPCSTR sErrPlace, UINT uDelay = 10, UINT uTimerID = 678)
	{												// sErrPlace can be NULL
		return (fnWaitForMsg(0, uDelay, NULL, NULL, uTimerID)) ?
																		fnErr("Timer Error", sErrPlace) : TRUE;
	}

  
// public for dependent calls
//	BOOL fnErr(LPCTSTR sErr1, LPCTSTR sErr2 = NULL, LPCTSTR sErr3 = NULL,
//						LPCTSTR sErr4 = NULL, LPCTSTR sErr5 = NULL, LPCTSTR sErr6 = NULL);

*/
  CString GetSysErrMsg();
  BOOL fnSysErr(LPCTSTR sErr1, LPCTSTR sErr2 = NULL, LPCTSTR sErr3 = NULL,
								LPCTSTR sErr4 = NULL, LPCTSTR sErr5 = NULL);
/*
	BOOL GetShellDirName(LPCTSTR sValue, LPTSTR sDir, BOOL bCurUser = TRUE);
	BOOL GetShellDirName(LPCTSTR sValue, CString & csDir, BOOL bCurUser = TRUE);
  BOOL fnShellExecOpen(LPCTSTR lpFile);	
*/
	HICON m_hIcon;
/*
	BOOL fnReadReg(HKEY hOpen, LPCSTR sKey, LPCSTR sValue, LPSTR sTarget,
												BOOL bErrMsg = FALSE, DWORD dwSize = _MAX_PATH);
	BOOL fnGetExePath(LPCSTR sExe, LPSTR sTarget,
											BOOL bDirOnly = FALSE, BOOL bErrMsg = FALSE);

  void fnTruncateCtime(CTime & ct);
*/
	// Generated message map functions
	//{{AFX_MSG(CZDialog)
	virtual BOOL OnInitDialog();
  virtual void OnOK();
  virtual void OnCancel();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
/*
  BOOL fnLBSelStringExact(HWND hCtl, LPCTSTR sSearch, LRESULT & nlInd);
  BOOL fnLBSelStringExact(HWND hCtl, LPCTSTR sSearch);
  BOOL fnLBSelStringExact(int nID, LPCTSTR sSearch, LRESULT & nlInd);
  BOOL fnLBSelStringExact(int nID, LPCTSTR sSearch);
*/
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_ZDIALOG_H__59CE5781_FE9B_11D7_97F8_00B0D0468F18__INCLUDED_)
