// NeoPatchMakerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NeoPatchMaker.h"
#include "NeoPatchMakerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	// ClassWizard generated virtual function overrides
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNeoPatchMakerDlg dialog

CNeoPatchMakerDlg::CNeoPatchMakerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNeoPatchMakerDlg::IDD, pParent)
    , m_bCheckNotice(FALSE)
{
	m_nDestType = -1;
	m_nSrcType = -1;
	m_strPackagePath = _T("");
	m_strSrcPath = _T("");
	m_dwMaxPackageSize = 0;
	m_dwUpdateVersion = 0;
	m_strFTPPassword = _T("");
	m_strFTPAddr = _T("");
	m_strFTPAccount = _T("");
	m_strDestPath = _T("");
	m_nFTPPort = 0;
	m_bPartialFileCompare = FALSE;
	m_bPassiveMode = FALSE;

    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_bBusy = false;
}

void CNeoPatchMakerDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);

    DDX_Control(pDX, IDC_INFOVIEW, m_editInfoView);
    DDX_Radio(pDX, IDC_RADIO_DEST_TYPE_LOCAL, m_nDestType);
    DDX_Radio(pDX, IDC_RADIO_SRC_TYPE_NONE, m_nSrcType);
    DDX_Text(pDX, IDC_EDIT_PACKAGE_LOCATION, m_strPackagePath);
    DDX_Text(pDX, IDC_EDIT_SRC_LOCATION, m_strSrcPath);
    DDX_Text(pDX, IDC_EDIT_PACKAGE_MAX_SIZE, m_dwMaxPackageSize);
    DDV_MinMaxDWord(pDX, m_dwMaxPackageSize, 1, 100000);
    DDX_Text(pDX, IDC_EDIT_VERSION, m_dwUpdateVersion);
    DDV_MinMaxDWord(pDX, m_dwUpdateVersion, 100, 4294967295);
    DDX_Text(pDX, IDC_EDIT_FTP_PASSWORD, m_strFTPPassword);
    DDX_Text(pDX, IDC_EDIT_FTP_ADDR, m_strFTPAddr);
    DDX_Text(pDX, IDC_EDIT_FTP_ACCOUNT, m_strFTPAccount);
    DDX_Text(pDX, IDC_EDIT_DEST_LOCATION, m_strDestPath);
    DDX_Text(pDX, IDC_EDIT_FTP_PORT, m_nFTPPort);
    DDX_Check(pDX, IDC_CHECK_PARTIAL_FILE_COMAPARE, m_bPartialFileCompare);
    DDX_Check(pDX, IDC_CHECK_PASSIVE_MODE, m_bPassiveMode);
    DDX_Check(pDX, IDC_NOTICE_CHECK, m_bCheckNotice);
}

BEGIN_MESSAGE_MAP(CNeoPatchMakerDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_GO, OnButtonGo)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON_CHECK_PATCHINFO_VERSION, OnButtonCheckPatchinfoVersion)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNeoPatchMakerDlg message handlers

BOOL CNeoPatchMakerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	LoadSettings();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNeoPatchMakerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CNeoPatchMakerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNeoPatchMakerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


bool CNeoPatchMakerDlg::Go()
{
	m_PatchMaker.SetPackageFilePath( m_strPackagePath );
	m_PatchMaker.SetLocalFilePath( m_strSrcPath );
	m_PatchMaker.SetDestFilePath( m_strDestPath );
	m_PatchMaker.SetPassiveMode( m_bPassiveMode );
    m_PatchMaker.SetCheckNoticeFolder( static_cast<bool>(m_bCheckNotice) );
	
	FILE_LOCATION_TYPE flt;
	
	switch( m_nSrcType )
	{
	case 0: flt = FILE_NONE; break;
	case 1: flt = FILE_ON_LOCAL; break;
	case 2: flt = FILE_ON_FTP; break;
	}
	m_PatchMaker.SetSourceType( flt );
	
	switch( m_nDestType )
	{
	case 0: flt = FILE_ON_LOCAL; break;
	case 1: flt = FILE_ON_FTP; break;
	}
	m_PatchMaker.SetDestType( flt );
	
	m_PatchMaker.SetPackageSize( m_dwMaxPackageSize * 1024 );
	m_PatchMaker.SetUpdateVersion( m_dwUpdateVersion );
	
	m_PatchMaker.SetFTPServerAddr( m_strFTPAddr );
	m_PatchMaker.SetFTPAccount( m_strFTPAccount );
	m_PatchMaker.SetFTPPassword( m_strFTPPassword );
	m_PatchMaker.SetFTPPort( m_nFTPPort );
	m_PatchMaker.SetPartialFileCompare( static_cast<bool>(m_bPartialFileCompare) );

	CWnd* pBtn = GetDlgItem( IDC_BUTTON_GO );
	pBtn->EnableWindow( FALSE );

	m_PatchMaker.Go( (CEdit*)GetDlgItem(IDC_INFOVIEW) );

	pBtn->EnableWindow( TRUE );

	return true;
}

UINT CNeoPatchMakerDlg::PatchMakeThread( LPVOID pParam )
{
	CNeoPatchMakerDlg* pDlg = (CNeoPatchMakerDlg*)pParam;

	pDlg->Go();

	return TRUE;
}

void CNeoPatchMakerDlg::OnButtonGo() 
{
	// TODO: Add your control notification handler code here

	//m_PatchMaker.Test();

	UpdateData( TRUE );
	SaveSettings();

	AfxBeginThread( PatchMakeThread, this );
	
	//pBtn->EnableWindow( TRUE );
}

void CNeoPatchMakerDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	UpdateData( TRUE );
	SaveSettings();
	
	CDialog::OnClose();
}

void CNeoPatchMakerDlg::LoadSettings()
{
	CWinApp* pApp = AfxGetApp();
	m_strPackagePath = pApp->GetProfileString( "SETTING", "PACKAGE_FILE_PATH", "" );
	m_strSrcPath = pApp->GetProfileString( "SETTING", "SOURCE_FILE_PATH", "" );
	m_strDestPath = pApp->GetProfileString( "SETTING", "DEST_FILE_PATH", "" );
	m_nSrcType = pApp->GetProfileInt( "SETTING", "SOURCE_TYPE", 0 );
	m_nDestType = pApp->GetProfileInt( "SETTING", "DEST_TYPE", 0 );
	m_dwMaxPackageSize = pApp->GetProfileInt( "SETTING", "PACKAGE_SIZE", 1 );
	m_dwUpdateVersion = pApp->GetProfileInt( "SETTING", "UPDATE_VERSION", 100 );
	m_strFTPAddr = pApp->GetProfileString( "SETTING", "FTP_SERVER_ADDR", "" );
	m_strFTPAccount = pApp->GetProfileString( "SETTING", "FTP_ACCOUNT", "" );
	m_strFTPPassword = pApp->GetProfileString( "SETTING", "FTP_PASSWORD", "" );
	m_nFTPPort = pApp->GetProfileInt( "SETTING", "FTP_PORT", 21 );
	m_bPassiveMode = pApp->GetProfileInt( "SETTING", "PASSIVE_MODE", FALSE );
	
	UpdateData( FALSE );
}


void CNeoPatchMakerDlg::SaveSettings()
{
	UpdateData( TRUE );

	CWinApp* pApp = AfxGetApp();
	pApp->WriteProfileString( "SETTING", "PACKAGE_FILE_PATH", m_strPackagePath );
	pApp->WriteProfileString( "SETTING", "SOURCE_FILE_PATH", m_strSrcPath );
	pApp->WriteProfileString( "SETTING", "DEST_FILE_PATH", m_strDestPath );
	pApp->WriteProfileInt( "SETTING", "SOURCE_TYPE", m_nSrcType );
	pApp->WriteProfileInt( "SETTING", "DEST_TYPE", m_nDestType );
	pApp->WriteProfileInt( "SETTING", "PACKAGE_SIZE", m_dwMaxPackageSize );
	pApp->WriteProfileInt( "SETTING", "UPDATE_VERSION", m_dwUpdateVersion );
	pApp->WriteProfileString( "SETTING", "FTP_SERVER_ADDR", m_strFTPAddr );
	pApp->WriteProfileString( "SETTING", "FTP_ACCOUNT", m_strFTPAccount );
	pApp->WriteProfileString( "SETTING", "FTP_PASSWORD", m_strFTPPassword );
	pApp->WriteProfileInt( "SETTING", "FTP_PORT", m_nFTPPort );
	pApp->WriteProfileInt( "SETTING", "PASSIVE_MODE", m_bPassiveMode );
}

void CNeoPatchMakerDlg::OnButtonCheckPatchinfoVersion() 
{
	// TODO: Add your control notification handler code here
	UpdateData( TRUE );
	SaveSettings();

	m_PatchMaker.SetPackageFilePath( m_strPackagePath );
	m_PatchMaker.SetLocalFilePath( m_strSrcPath );
	m_PatchMaker.SetDestFilePath( m_strDestPath );
	m_PatchMaker.SetPassiveMode( m_bPassiveMode );
	
	FILE_LOCATION_TYPE flt;
	
	switch( m_nSrcType )
	{
	case 0: flt = FILE_NONE; break;
	case 1: flt = FILE_ON_LOCAL; break;
	case 2: flt = FILE_ON_FTP; break;
	}
	m_PatchMaker.SetSourceType( flt );
	
	switch( m_nSrcType )
	{
	case 0: flt = FILE_ON_LOCAL; break;
	case 1: flt = FILE_ON_FTP; break;
	}
	m_PatchMaker.SetDestType( flt );
	
	m_PatchMaker.SetPackageSize( m_dwMaxPackageSize * 1024 );
	m_PatchMaker.SetUpdateVersion( m_dwUpdateVersion );
	
	m_PatchMaker.SetFTPServerAddr( m_strFTPAddr );
	m_PatchMaker.SetFTPAccount( m_strFTPAccount );
	m_PatchMaker.SetFTPPassword( m_strFTPPassword );
	m_PatchMaker.SetFTPPort( m_nFTPPort );
	m_PatchMaker.SetPartialFileCompare( static_cast<bool>(m_bPartialFileCompare) );
	

	int nVersion = m_PatchMaker.GetPatchInfoVersion();

	CString strTmp;
	strTmp.Format( "%d", nVersion );
	GetDlgItem( IDC_EDIT_PATCHINFO_VERSION )->SetWindowText( strTmp );
}
