/*
 =======================================================================================
  Name        :VersMngrDlg.cpp
 
  Author      :Vincent Lhullier				Date :16/07/97
 
  Description :dialog box to restore old security saved version
 =======================================================================================
  Modification -> Author :                  Date :
  Description :
 =======================================================================================
 */

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "stdafx.h"
#include "VersMngr.h"
#include "VersMngrDlg.h"
#include "SaveMngr.h"

#include <io.h>
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


/*
 =======================================================================================
	CVersMngrDlg dialog
 =======================================================================================
 */


/*
 ----------------------------------------------------------------------------------------
  Description   : constructor
  pParent             -> Parent window
 ----------------------------------------------------------------------------------------
 */
CVersMngrDlg::CVersMngrDlg(CWnd* pParent) : CDialog(CVersMngrDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVersMngrDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


/*
 ----------------------------------------------------------------------------------------
  Description   : Data exchange
 ----------------------------------------------------------------------------------------
 */
void CVersMngrDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVersMngrDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

/*
 ----------------------------------------------------------------------------------------
  Description   : Message map
 ----------------------------------------------------------------------------------------
 */
BEGIN_MESSAGE_MAP(CVersMngrDlg, CDialog)
	//{{AFX_MSG_MAP(CVersMngrDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_RESTORE, OnButtonRestore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/*
 =======================================================================================
	CVersMngrDlg message handlers
 =======================================================================================
 */


/*
 ----------------------------------------------------------------------------------------
  Description   : WM_INITDIALOG
 ----------------------------------------------------------------------------------------
 */
BOOL CVersMngrDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	/*
	 * Init version list
	 */
	m_fn_vRefreshVersionList();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

/*
 ----------------------------------------------------------------------------------------
  Description   : WM_PAINT
 ----------------------------------------------------------------------------------------
 */
void CVersMngrDlg::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();
	}
}


/*
 ----------------------------------------------------------------------------------------
  Description   : WM_QUERYDRAGICON
 ----------------------------------------------------------------------------------------
 */
HCURSOR CVersMngrDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/*
 ----------------------------------------------------------------------------------------
  Description   : BN_CLICKED on IDC_BUTTON_RESTORE
 ----------------------------------------------------------------------------------------
 */
void CVersMngrDlg::OnButtonRestore() 
{
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
	CListBox	*p_oLB = (CListBox *) GetDlgItem( IDC_LIST_VERSIONS );
	long		lIndex = p_oLB->GetCurSel();
	long		lCount = p_oLB->GetCount();
	long		lVersion;
	char		szGameData[_MAX_PATH];
	char		szVersionDir[_MAX_PATH];
	char		szFileName[ _MAX_PATH ], *p_szEndFileName, *p_szEnd;
	FILE		*hpDelFile;
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	if (lIndex != LB_ERR)
	{
		strcpy( szGameData, "GameData" );
		for (lVersion = lCount - 1; lVersion >= lIndex; lVersion --)
		{
			SAVE_fn_vGetVersionDirName( lVersion, szVersionDir );

			/*
			 * delete all files that was created in current version
			 */
			/*
			p_szEndFileName = szFileName + sprintf( szFileName, "%s\\", szGameData );
			strcpy( p_szEndFileName,"delete.lst" );
			*/
			sprintf(szFileName, "%s\\delete.lst", szVersionDir );

			if ( _access( szFileName, 0) == 0)
			{
				if ( (hpDelFile = fopen( szFileName, "rt" )) != NULL)
				{
					p_szEndFileName = szFileName + sprintf( szFileName, "%s\\", szGameData );
					while (fgets(p_szEndFileName, MAX_PATH, hpDelFile) != NULL)
					{
						if ((p_szEnd = strrchr( p_szEndFileName, '\n')) != NULL)
							*p_szEnd = 0;
						if ((p_szEnd = strrchr( p_szEndFileName, '\r')) != NULL)
							*p_szEnd = 0;
						DeleteFile( szFileName );
					}
					fclose (hpDelFile );
				}
				sprintf(szFileName, "%s\\delete.lst", szVersionDir );
				DeleteFile ( szFileName );
			}


			/* 
			 * watch if modiflst.txt exist in Gamedata dir and not in version dir
			 */
			/*
			if (lVersion == lIndex)
			{
				char szModifFileName[ _MAX_PATH ];

				/*
				 * check if modiflst.txt doesn't exist in version dir
				 */
				/*
				sprintf( szModifFileName, "%s\\modiflst.txt", szVersionDir );
				if (_access( szModifFileName, 0) != 0)
				{
					sprintf( szModifFileName, "%s\\modiflst.txt", szGameData );
					if (_access( szModifFileName, 0) == 0)
					{
						DeleteFile( szModifFileName );
					}
				}
			}
			*/

			if ( !SAVE_fn_bMoveDirectory( szVersionDir, szGameData ) )
			{
				LPVOID lpMsgBuf;
				FormatMessage
					( 
						FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
						NULL,
						GetLastError(),
						MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
						(LPTSTR) &lpMsgBuf,
						0,
						NULL 
					);
				MessageBox( (char *) lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
				LocalFree( lpMsgBuf );
			}
		}
	}
	m_fn_vRefreshVersionList();
}


/*
 =======================================================================================
	Specific functions 
 =======================================================================================
 */

/*
 ----------------------------------------------------------------------------------------
  Description   : fill list of versions
 ----------------------------------------------------------------------------------------
 */
void CVersMngrDlg::m_fn_vRefreshVersionList( void )
{
	char		a_cVersion[1000];
	SYSTEMTIME	a_stTime[1000];
	long		lNumberOfVersions;
	long		lVersion;
	char		szLine[ 256 ];
	CListBox	*p_oLB = (CListBox *) GetDlgItem (IDC_LIST_VERSIONS);

	p_oLB->ResetContent();

	lNumberOfVersions = SAVE_fn_lAnalyseCurrentUsedVersion( a_cVersion, a_stTime );
	
	p_oLB->ShowWindow( lNumberOfVersions != 0 );
	GetDlgItem( IDC_PICTURE_NOTHING)->ShowWindow( lNumberOfVersions == 0);
	GetDlgItem( IDC_BUTTON_RESTORE)->EnableWindow( lNumberOfVersions != 0 );

	for (lVersion = 0; lVersion <  lNumberOfVersions; lVersion ++ )
	{
		sprintf
			( 
				szLine, 
				"%03d -> %02d/%02d/%d %02d:%02d:%02d",
				lVersion,
				a_stTime[lVersion].wDay, 
				a_stTime[lVersion].wMonth,
				a_stTime[lVersion].wYear, 
				a_stTime[lVersion].wHour, 
				a_stTime[lVersion].wMinute,
				a_stTime[lVersion].wSecond
			);
		p_oLB->AddString( szLine );
	}
	p_oLB->SetCurSel( lNumberOfVersions - 1 );
}

