// ConvertPatchInfoV1toV2Dlg.cpp : implementation file
//

#include "ConvertPatchInfoV1toV2.h"
#include "ConvertPatchInfoV1toV2Dlg.h"

#include "PatchInfoList.h"

#include "ZipArchive.h"


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

/////////////////////////////////////////////////////////////////////////////
// CConvertPatchInfoV1toV2Dlg dialog

CConvertPatchInfoV1toV2Dlg::CConvertPatchInfoV1toV2Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CConvertPatchInfoV1toV2Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConvertPatchInfoV1toV2Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CConvertPatchInfoV1toV2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConvertPatchInfoV1toV2Dlg)
	DDX_Control(pDX, IDC_EDIT_FILEPATH, m_editFilePath);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CConvertPatchInfoV1toV2Dlg, CDialog)
	//{{AFX_MSG_MAP(CConvertPatchInfoV1toV2Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
	ON_BN_CLICKED(IDC_BUTTON_GO, OnButtonGo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConvertPatchInfoV1toV2Dlg message handlers

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

	// 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
	m_ctrlLogCtrl.Init( 12, 95, 534, 98, GetSafeHwnd() );
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CConvertPatchInfoV1toV2Dlg::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 CConvertPatchInfoV1toV2Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CConvertPatchInfoV1toV2Dlg::OnButtonBrowse() 
{
	// TODO: Add your control notification handler code here
	


	CFileDialog fileDlg( TRUE, "zip", "PatchInfo.zip", 0,
		"PatchInfo.zip File|PatchInfo.zip|Zip Files (*.zip)|*.zip|All Files (*.*)|*.*||", NULL );
	if( fileDlg.DoModal() == IDOK )
	{
		m_editFilePath.SetWindowText( fileDlg.GetPathName() );
	}
}

void CConvertPatchInfoV1toV2Dlg::OnButtonGo() 
{
	// TODO: Add your control notification handler code here
	CString strDestPath;
	CString strV1PatchInfoPath;
//	CString strPatchInfoV2Path;

	m_editFilePath.GetWindowText( strV1PatchInfoPath );
	int nLoc = strV1PatchInfoPath.ReverseFind( '\\' );
	if( -1 == nLoc )
	{
		strDestPath = "";
//		strPatchInfoV2Path = "PatchInfoV2.zip";
	}
	else
	{
		strDestPath = strV1PatchInfoPath.Left( nLoc+1 );
//		strPatchInfoV2Path = strDestPath + "\\PatchInfoV2.zip";
	}
	
	CZipArchive zipArc;
	CPatchInfoList pil;
	
	// V1 PatchInfo 압축해제
	m_ctrlLogCtrl.OutputMessage( "1. Decompressing PatchInfo.zip\n" );
	zipArc.Open( strDestPath+"PatchInfo.zip", CZipArchive::zipOpen );
	zipArc.ExtractFile( 0, strDestPath, false, "PatchInfo" );
	zipArc.Close();

	// LoadV1()
	m_ctrlLogCtrl.OutputMessage( "2. Loading PatchInfo\n" );
	pil.LoadV1( strDestPath+"PatchInfo" );

	// V1 PatchInfo 삭제
	DeleteFile( strDestPath+"PatchInfo" );

	// PatchInfoV2 저장
	m_ctrlLogCtrl.OutputMessage( "3. Saving PatchInfoV2\n" );
	pil.Save( strDestPath+"PatchInfoV2" );

	// PatchInfoV2.zip 에 압축
	m_ctrlLogCtrl.OutputMessage( "4. Compressing PatchInfoV2.zip\n" );
	zipArc.Open( strDestPath+"PatchInfoV2.zip", CZipArchive::zipCreate );
	zipArc.AddNewFile( strDestPath+"PatchInfoV2", "PatchInfoV2" );
	zipArc.Close();

	// PatchInfoV2 삭제
	DeleteFile( strDestPath+"PatchInfoV2" );
}
