// PPgUpdate.cpp : implementation file
//

#include "stdafx.h"
#include "emule.h"
#include "PPgUpdate.h"
#include "emuleDlg.h"
#include "OtherFunctions.h"
#include "serverWnd.h"
#include "Sion/GUI/Fakecheck.h"
#include "IPFilter.h"
#ifdef ARGOS // MOD: NA - [NeoArgos]
#include "Sion/Argos.h"
#endif // ARGOS // MOD: NA END
#include "Sion/Functions.h"

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


IMPLEMENT_DYNAMIC(CPPgUpdate, CPropertyPage)
CPPgUpdate::CPPgUpdate()
: CPropertyPage(CPPgUpdate::IDD)
{
}

CPPgUpdate::~CPPgUpdate()
{
}

void CPPgUpdate::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CPPgUpdate, CPropertyPage)
	// MOD: FC - [FakeCheck]
	ON_BN_CLICKED(IDC_UPDATEFAKELISTSTART, OnSettingsChange)
	ON_BN_CLICKED(IDC_RESETFAKESURL, OnBnClickedResetfakes)
	ON_BN_CLICKED(IDC_UPDATEFAKES, OnBnClickedUpdatefakes)
	ON_EN_CHANGE(IDC_UPDATE_URL_FAKELIST, OnSettingsChange)
	// MOD: FC END

	ON_EN_CHANGE(IDC_UPDATE_URL_IPFILTER, OnSettingsChange)
	ON_BN_CLICKED(IDC_UPDATEIPFURL, OnBnClickedUpdateipfurl)
	ON_BN_CLICKED(IDC_RESETIPFURL, OnBnClickedResetipfurl)
	ON_BN_CLICKED(IDC_AUTOUPIPFILTER , OnSettingsChange)

#ifdef ARGOS // MOD: NA - [NeoArgos]
	ON_EN_CHANGE(IDC_UPDATE_URL_LEECHERSDAT, OnSettingsChange)
    ON_BN_CLICKED(IDC_UPDATELLURL, OnBnClickedUpdatellurl)
	ON_BN_CLICKED(IDC_RESETLLURL, OnBnClickedResetllurl)
	ON_BN_CLICKED(IDC_AUTOUPLEECHERSDAT , OnSettingsChange)
#endif // ARGOS // MOD: NA END

	ON_BN_CLICKED(IDC_RUNTIMEUPDATE, OnSettingsChange)
	ON_EN_CHANGE(IDC_RUNTIMEUPDATE_TIME, OnSettingsChange)
END_MESSAGE_MAP()



BOOL CPPgUpdate::OnInitDialog()
{
	CPropertyPage::OnInitDialog();
	InitWindowStyles(this);

	LoadSettings();
	Localize();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void SysTimeToStr(LPSYSTEMTIME st, LPTSTR str)
{
	TCHAR sDate[15];
	sDate[0] = _T('\0');
	GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, st, NULL, sDate, 100);
	TCHAR sTime[15];
	sTime[0] = _T('\0');
	GetTimeFormat(LOCALE_USER_DEFAULT, 0, st, NULL ,sTime ,100);
	_stprintf(str, _T("%s %s"), sDate, sTime);
}

void CPPgUpdate::LoadSettings(void)
{
	GetDlgItem(IDC_RUNTIMEUPDATE_TIME)->SetWindowText(StrLine(_T("%i"),thePrefs.m_iRuntimeUpdateIntervals));
	if(thePrefs.m_bAutoRuntimeUpdate)
		CheckDlgButton(IDC_RUNTIMEUPDATE,1);
	else
		CheckDlgButton(IDC_RUNTIMEUPDATE,0);

	// MOD: FC - [FakeCheck]
	GetDlgItem(IDC_UPDATE_URL_FAKELIST)->SetWindowText(thePrefs.m_sUpdateURLFakeList);
	if(thePrefs.m_bUpdateFakeStartup)
		CheckDlgButton(IDC_UPDATEFAKELISTSTART,1);
	else
		CheckDlgButton(IDC_UPDATEFAKELISTSTART,0);
	// MOD: FC END

	GetDlgItem(IDC_UPDATE_URL_IPFILTER)->SetWindowText(thePrefs.m_sUpdateURLIPFilter);
	if(thePrefs.m_bAutoUpdateIPFilter)
		CheckDlgButton(IDC_AUTOUPIPFILTER,1);
	else
		CheckDlgButton(IDC_AUTOUPIPFILTER,0);

#ifdef ARGOS // MOD: NA - [NeoArgos]
	GetDlgItem(IDC_UPDATE_URL_LEECHERSDAT)->SetWindowText(thePrefs.m_sUpdateURLLeechersList);
	if(thePrefs.m_bUpdateLeechersStartup)
		CheckDlgButton(IDC_AUTOUPLEECHERSDAT,1);
	else
		CheckDlgButton(IDC_AUTOUPLEECHERSDAT,0);
#endif // ARGOS // MOD: NA END

	// MOD: FC - [FakeCheck]
	TCHAR sTime[30];
	sTime[0] = _T('\0');
	SysTimeToStr(thePrefs.GetFakesVersion(), sTime);
	GetDlgItem(IDC_FAKELIST_VERSION)->SetWindowText(sTime);
	// MOD: FC END


#ifdef ARGOS // MOD: NA - [NeoArgos]
	sTime[0] = _T('\0');
	SysTimeToStr(thePrefs.GetIPFilterVersion(), sTime);
	GetDlgItem(IDC_LEECHERLIST_VERSION)->SetWindowText(sTime);
#endif // ARGOS // MOD: NA END
}

BOOL CPPgUpdate::OnApply()
{

	CString buffer;
	
	GetDlgItem(IDC_RUNTIMEUPDATE_TIME)->GetWindowText(buffer);
	thePrefs.m_iRuntimeUpdateIntervals = min(_tstoi(buffer),3);
	thePrefs.m_bAutoRuntimeUpdate = IsDlgButtonChecked(IDC_RUNTIMEUPDATE)!=0;

	// MOD: FC - [FakeCheck]
	GetDlgItem(IDC_UPDATE_URL_FAKELIST)->GetWindowText(buffer);
	_tcscpy(thePrefs.m_sUpdateURLFakeList, buffer);
	thePrefs.m_bUpdateFakeStartup = IsDlgButtonChecked(IDC_UPDATEFAKELISTSTART)!=0;
	// MOD: FC END

	
	GetDlgItem(IDC_UPDATE_URL_IPFILTER)->GetWindowText(buffer);
	_tcscpy(thePrefs.m_sUpdateURLIPFilter, buffer);
	thePrefs.m_bAutoUpdateIPFilter = IsDlgButtonChecked(IDC_AUTOUPIPFILTER)!=0;
	
#ifdef ARGOS // MOD: NA - [NeoArgos]
    GetDlgItem(IDC_UPDATE_URL_LEECHERSDAT)->GetWindowText(buffer);
	_tcscpy(thePrefs.m_sUpdateURLLeechersList, buffer);
	thePrefs.m_bUpdateLeechersStartup = IsDlgButtonChecked(IDC_AUTOUPLEECHERSDAT)!=0;
#endif // ARGOS // MOD: NA END

	LoadSettings();
	SetModified(FALSE);
	
	return CPropertyPage::OnApply();
}
void CPPgUpdate::Localize(void)
{
	if(m_hWnd)
	{
		//SetWindowText(GetResString(IDS_MORPH2));
		
		// MOD: FC - [FakeCheck]
		GetDlgItem(IDC_UPDATE_FILE)->SetWindowText(GetResString(IDS_FILES));
		GetDlgItem(IDC_UPDATEFAKELISTSTART)->SetWindowText(GetResString(IDS_X_UPDATEFAKECHECKONSTART));
		GetDlgItem(IDC_UPDATEFAKES)->SetWindowText(GetResString(IDS_X_UPDATE));
		GetDlgItem(IDC_RESETFAKESURL)->SetWindowText(GetResString(IDS_X_RESET));
		// MOD: FC END
		
		GetDlgItem(IDC_UPDATE_SECURITY)->SetWindowText(GetResString(IDS_SECURITY));
		GetDlgItem(IDC_AUTOUPIPFILTER)->SetWindowText(GetResString(IDS_X_UPDATEIPFILTERONSTART));
		GetDlgItem(IDC_UPDATEIPFURL)->SetWindowText(GetResString(IDS_X_UPDATE));
		GetDlgItem(IDC_RESETIPFURL)->SetWindowText(GetResString(IDS_X_RESET));

#ifdef ARGOS // MOD: NA - [NeoArgos]
		GetDlgItem(IDC_UPDATE_PROTECTION)->SetWindowText(GetResString(IDS_X_PROTECTION));
		GetDlgItem(IDC_AUTOUPLEECHERSDAT)->SetWindowText(GetResString(IDS_X_AUTOUPLEECHERSDAT));
		GetDlgItem(IDC_UPDATELLURL)->SetWindowText(GetResString(IDS_X_UPDATE));
		GetDlgItem(IDC_RESETLLURL)->SetWindowText(GetResString(IDS_X_RESET));
#endif // ARGOS // MOD: NA END

		GetDlgItem(IDC_UPDATE_AUTO)->SetWindowText(GetResString(IDS_X_AUTO_UPDATE));
		GetDlgItem(IDC_RUNTIMEUPDATE)->SetWindowText(GetResString(IDS_X_RUNTIMEUPDATE));
	}
}

// MOD: FC - [FakeCheck]
void CPPgUpdate::OnBnClickedUpdatefakes()
{
	OnApply();
	theApp.FakeCheck->DownloadFakeList();
	TCHAR sBuffer[30];
	sBuffer[0] = _T('\0'); 
	SysTimeToStr(thePrefs.GetFakesVersion(), sBuffer);
	GetDlgItem(IDC_FAKELIST_VERSION)->SetWindowText(sBuffer);
}

void CPPgUpdate::OnBnClickedResetfakes()
{
	CString strBuffer = _T("http://emulepawcio.sourceforge.net/nieuwe_site/Ipfilter_fakes/fakes.zip");
	GetDlgItem(IDC_UPDATE_URL_FAKELIST)->SetWindowText(strBuffer);
	memset(thePrefs.GetFakesVersion(), 0, sizeof(SYSTEMTIME));
	GetDlgItem(IDC_FAKELIST_VERSION)->SetWindowText(_T(""));
}
// MOD: FC END

void CPPgUpdate::OnBnClickedUpdateipfurl()
{
	OnApply();
	theApp.ipfilter->UpdateIPFilterURL();
	TCHAR sBuffer[30];
	sBuffer[0] = _T('\0'); 
	SysTimeToStr(thePrefs.GetIPFilterVersion(), sBuffer);
	GetDlgItem(IDC_IPFILTER_VERSION)->SetWindowText(sBuffer);
}

void CPPgUpdate::OnBnClickedResetipfurl()
{
	CString strBuffer = _T("http://emulepawcio.sourceforge.net/nieuwe_site/Ipfilter_fakes/ipfilter.zip");
	GetDlgItem(IDC_UPDATE_URL_IPFILTER)->SetWindowText(strBuffer);
	memset(thePrefs.GetIPFilterVersion(), 0, sizeof(SYSTEMTIME));
	GetDlgItem(IDC_IPFILTER_VERSION)->SetWindowText(_T(""));
}

#ifdef ARGOS // MOD: NA - [NeoArgos]
void CPPgUpdate::OnBnClickedUpdatellurl()
{
	OnApply();
	theApp.argos->UpdateLeecherList();
	TCHAR sBuffer[30];
	sBuffer[0] = _T('\0'); 
	SysTimeToStr(thePrefs.GetLeechersVersion(), sBuffer);
	GetDlgItem(IDC_LEECHERLIST_VERSION)->SetWindowText(sBuffer);
}


void CPPgUpdate::OnBnClickedResetllurl()
{
	CString strBuffer = _T("http://perso.numericable.fr/geynetlau/Sion/SionLeechers.zip");
	GetDlgItem(IDC_UPDATE_URL_LEECHERSDAT)->SetWindowText(strBuffer);
	memset(thePrefs.GetLeechersVersion(), 0, sizeof(SYSTEMTIME));
	GetDlgItem(IDC_LEECHERLIST_VERSION)->SetWindowText(_T(""));
}
#endif // ARGOS // MOD: NA END
