#pragma once
#include "afxwin.h"

#include <afxole.h>         // MFC OLE 클래스입니다.
#include <afxodlgs.h>       // MFC OLE 대화 상자 클래스입니다.
#include <afxdisp.h>        // MFC 자동화 클래스입니다.

#include "TranTabCtrl.h"
#include "WebBrowser2.h"
#include "TextProgressCtrl.h"
#include "RegUtil.h"
#include "../ConsoleApp(DN)/Struct.h"
#include "../ConsoleApp(DN)/SocketUtil.h"

#include "MD5.h"
#include "ResultDlg.h"

typedef struct {
	bool    bFolder;
	CString sText;
	CString sSrcPath;
	CString sDstPath;
	__int64 nSize;
	__int64 nTransferredSize;
	CString sLastModified;
	int     nState;	// -1:전송중, 0:전송전, 1:완료, 2:에러, 3:건너뜀, 4:중지, 5:일시정지
} TRANFILE_STRUCT, *PTRANFILE_STRUCT;

typedef struct {
	bool doRetry;
	int retryCnt;
	PTRANFILE_STRUCT retryFile;
} RETRY_TFINFO , *PRETRY_TFINFO;

enum TRANFILE_STATE {
	TFS_TRANSFERRING = -1,
	TFS_READY = 0,
	TFS_COMPLETE,
	TFS_ERROR,
	TFS_SKIPED,
	TFS_STOP,
	TFS_PAUSE,
};

#define WMU_TRANSFER_IS_READY  (WM_USER + 1)
#define WMU_TRANSFER           (WM_USER + 2)
#define WMU_TRANSFER_COMPLETED (WM_USER + 3)
#define WMU_TRANSFER_ERROR     (WM_USER + 4)
#define WMU_TRANSFER_CLOSE     (WM_USER + 5)

#define TIMER_ID_CLOSE        1
#define TIMER_ID_UPDATESTATUS 2
#define TIMER_ID_TRANSFER     3

#define LOG_PATH1 "C:\\TEMP"
#define LOG_PATH2 "C:\\TEMP\\SP-CONSOLE"
#define LOG_FILE "upload_fail.log"
#define DEBUG_FILE "debug.log"
#define LOG_UPLOAD "upload.log"

// CTransferDlg 대화 상자입니다.

class CTransferDlg : public CDialog
{
	DECLARE_DYNAMIC(CTransferDlg)

public:
	CTransferDlg(CWnd* pParent = NULL);   // 표준 생성자입니다.
	virtual ~CTransferDlg();

// 대화 상자 데이터입니다.
	enum { IDD = IDD_TRANSFER };

protected:
	HCOMMLIB m_hcl;

	map<CString,int> m_mapTranFile;
	CPtrArray        m_paTranFile;

	CString m_sTitle;

	bool m_bPaused;
	bool m_bResume;
	bool m_bStop;

	CWinThread* m_pthread;

	bool m_bUpdating;
	bool m_bUpdateMode;
	
	int     m_nTotalCount;
	UINT64  m_nTotalSize;
	UINT64  m_nTotalTransferred;
	double  m_nTransferSpeed;
	UINT64  m_nSpeedTransferred;
	DWORD   m_nSpeedTick;
	int     m_iTransfer;
	CTime   m_tmStart;
	__int64 m_nElapsed;
	DWORD   m_dwOldTick;

	double m_nGapTransfer;

	int    m_nSelectedCount;
	UINT64 m_nSelectedSize;

	UINT64 m_nCurrentFileSize;
	UINT64 m_nBaseFileSize;
	UINT64 m_nTransferredSize;

	int  m_nWriteAction;
	BOOL m_bWriteApplyAll;

	UINT m_nTimer;

	CString m_sSkippedFolder;

	DWORD   m_dwError;
	CString m_sError;
	CString m_szTargetPath;
	CString m_szHashedTargetPath;
	
	CSocketUtil m_sutil;

	PTRANFILE_STRUCT m_retry_file;
	PRETRY_TFINFO m_retry_tfinfo;

	CString m_strFailedFiles;
	int m_nFailedFileCount;

public:
    CWebBrowser2      m_wbAdvert;
	CTranTabCtrl      m_tab;
	CButton           m_btnDelFile;
	CString           m_sFolder;
	CListCtrl         m_list;
	CStatic           m_stcStatus;
	CStatic           m_grpNow;
	CStatic           m_stcSpeedTitle;
	CStatic           m_stcSpeed;
	CStatic           m_stcSaveFileTitle;
	CStatic           m_stcSaveFile;
	CTextProgressCtrl m_prsNow;
	CStatic           m_grpTotal;
	CStatic           m_stcETimeTitle;
	CStatic           m_stcETime;
	CStatic           m_stcLTimeTitle;
	CStatic           m_stcLTime;
	CStatic           m_stcFileCountTitle;
	CStatic           m_stcFileCount;
	CTextProgressCtrl m_prsTotal;
	CButton           m_btnStart;
	CButton           m_btnPause;
	CButton           m_btnClose;

	TRANPARAM_STRUCT m_tp;
	LPSTR            m_pszFileList;
	bool			 m_pause_disabled;

	CResultDlg		 m_dlgResult;

protected:
	void InitTransferEnv();

	int GetAction(CString sOldObj, CString sOldMTime, UINT64 nOldSize, CString sNewObj, CString sNewMTime, UINT64 nNewSize);

	bool AppendRemoteFile(CString sPath);
	bool AppendLocalFile(CString sPath, int nDepth);
	void AppendFile(CString sPath, UINT64 nSize, CString sLastModified, int nDepth, int iPos = -1);

	void ShowFileCount();
	void ShowTotalProgress();

	void LoadWritePolicy();

	void ShowProgressStatus(UINT64 nTransferred);

	static UINT LoadTransferFile(LPVOID);
	static void TransferCallbackProc(LPVOID, __int64 nTransfered);

	void RedrawControl1();
	void RedrawControl2();

	void FileOptLogsRequest(PTRANFILE_STRUCT);
	void SendMailWhenUploadFail(const char*);
	void WriteWhenUploadFail(const char* ,const char*);
	void WriteDebugLog(const char* ,const char*,const char*);

public:
	void TransCompleteWithDistribute();
	void Pause(bool bPause = true);
	void Resume();

	void ShowStatus(bool bSelected = false, bool bUpdate = true, bool bAllSelected = false);

	DECLARE_EVENTSINK_MAP()
	void DocumentCompleteWBAdvert(LPDISPATCH pDisp, VARIANT* URL);
	void NavigateErrorWBAdvert(LPDISPATCH pDisp, VARIANT* URL, VARIANT* Frame, VARIANT* StatusCode, BOOL* Cancel);

	CString GetHashedPath(CString strPath);
	void AddFailedFile(CString strPathName, bool bDirectory);

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 지원입니다.

	DECLARE_MESSAGE_MAP()

public:
	virtual BOOL OnInitDialog();
	afx_msg void OnClose();
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnBtnDelfile();
	afx_msg void OnBtnStart();
	afx_msg void OnBtnPause();
	afx_msg void OnBtnClose();
	afx_msg void OnListItemChanged(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnListKeydown(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg LRESULT OnTransferIsReady(WPARAM, LPARAM);
	afx_msg LRESULT OnTransfer(WPARAM, LPARAM);
	afx_msg LRESULT OnTransferCompleted(WPARAM, LPARAM);
	afx_msg LRESULT OnTransferError(WPARAM, LPARAM);
	afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);

};
