// PgTweaks.cpp : implementation file
//

#include "stdafx.h"
#include "emule.h"
#include "PPgTweaks.h"



// CPPgTweaks dialog

IMPLEMENT_DYNAMIC(CPPgTweaks, CPropertyPage)
CPPgTweaks::CPPgTweaks()
	: CPropertyPage(CPPgTweaks::IDD)
{
	guardian=false;
}

CPPgTweaks::~CPPgTweaks()
{
}

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

BEGIN_MESSAGE_MAP(CPPgTweaks, CPropertyPage)

	ON_EN_CHANGE(IDC_MAXCON5SEC, OnSettingsChange)
	ON_BN_CLICKED(IDC_VERBOSE, OnSettingsChange)
	ON_BN_CLICKED(IDC_AUTOTAKEED2KLINKS, OnSettingsChange)
	ON_BN_CLICKED(IDC_UPDATEQUEUE, OnSettingsChange)

	// Added by Tarod -new options
	ON_EN_CHANGE(IDC_URL_PING, OnSettingsChange)
	ON_EN_CHANGE(IDC_BREAKEVENMS, OnSettingsChange)
	ON_EN_CHANGE(IDC_MAXXSATONCE, OnSettingsChange)
	ON_EN_CHANGE(IDC_QUEUESIZE, OnSettingsChange)
	ON_BN_CLICKED(IDC_DISABLE_XS, OnBnClickedDisableXS)
	ON_BN_CLICKED(IDC_THROTTLE_UPLOAD, OnSettingsChange)
	ON_BN_CLICKED(IDC_DYNUP, OnBnClickedDynup)


END_MESSAGE_MAP()


// CPPgTweaks message handlers

void CPPgTweaks::OnBnClickedDisableXS()
{
	if (guardian) return;

	guardian=true;
	SetModified();

	CString str ;

	GetDlgItem(IDC_MAXXSATONCE)->EnableWindow(!IsDlgButtonChecked(IDC_DISABLE_XS));
	if ((bool)IsDlgButtonChecked(IDC_DISABLE_XS)) {
		str.Format("%u", (unsigned)((float)app_prefs->prefs->maxsourceperfile * 0.9f));
	} else {
		str.Format("%u", 300) ;
	}

	guardian=false;
}

void CPPgTweaks::OnBnClickedDynup()
{
	char buffer[256];

	if (guardian) return;

	guardian = true;
	SetModified();

	GetDlgItem(IDC_URL_PING)->EnableWindow(IsDlgButtonChecked(IDC_DYNUP));
	GetDlgItem(IDC_BREAKEVENMS)->EnableWindow(IsDlgButtonChecked(IDC_DYNUP));
	if (IsDlgButtonChecked(IDC_DYNUP)) {
		if (GetDlgItem(IDC_URL_PING)->GetWindowTextLength()) {
			CString strBuffer;
			GetDlgItem(IDC_URL_PING)->GetWindowText(strBuffer);
			strBuffer.Trim();
			strcpy(app_prefs->prefs->url_ping, strBuffer.GetBuffer());
		}

		if (GetDlgItem(IDC_BREAKEVENMS)->GetWindowTextLength()) {
			GetDlgItem(IDC_BREAKEVENMS)->GetWindowText(buffer, 20) ;
			if (atoi(buffer) < 100) {
				app_prefs->prefs->base_ping = 100 ;
				sprintf(buffer, "%d", app_prefs->prefs->base_ping);
				GetDlgItem(IDC_BREAKEVENMS)->SetWindowText(buffer) ;
			} else if (atoi(buffer) > 800) {
				app_prefs->prefs->base_ping = 800 ;
				sprintf(buffer, "%d", app_prefs->prefs->base_ping);
				GetDlgItem(IDC_BREAKEVENMS)->SetWindowText(buffer);
			} else {
				app_prefs->prefs->base_ping = (atoi(buffer) / 100) * 100; // convert to 100 multiple
				sprintf(buffer, "%d", app_prefs->prefs->base_ping);
				GetDlgItem(IDC_BREAKEVENMS)->SetWindowText(buffer);
			}
		}
	}

	guardian = false;
}

BOOL CPPgTweaks::OnInitDialog()
{
	CPropertyPage::OnInitDialog();

	LoadSettings();
	Localize();

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

void CPPgTweaks::LoadSettings(void)
{
	if(m_hWnd)
	{
		CString strBuffer;

		strBuffer.Format("%d", app_prefs->GetMaxConperFive());
		GetDlgItem(IDC_MAXCON5SEC)->SetWindowText(strBuffer);
	}
	if(app_prefs->prefs->m_bVerbose)
		CheckDlgButton(IDC_VERBOSE,1);
	else
		CheckDlgButton(IDC_VERBOSE,0);

	if(app_prefs->prefs->m_bupdatequeuelist)
		CheckDlgButton(IDC_UPDATEQUEUE,1);
	else
		CheckDlgButton(IDC_UPDATEQUEUE,0);
	// Barry
	if(app_prefs->prefs->autotakeed2klinks)
		CheckDlgButton(IDC_AUTOTAKEED2KLINKS,1);
	else
		CheckDlgButton(IDC_AUTOTAKEED2KLINKS,0);

	// Added by Tarod -load new added settings
	GetDlgItem(IDC_MAXXSATONCE)->EnableWindow(!IsDlgButtonChecked(IDC_DISABLE_XS));
	if (app_prefs->prefs->disablexs)
		CheckDlgButton(IDC_DISABLE_XS,1);
	else
		CheckDlgButton(IDC_DISABLE_XS,0);
/*	if (app_prefs->prefs->throttleupload)
		CheckDlgButton(IDC_THROTTLE_UPLOAD,1);
	else
		CheckDlgButton(IDC_THROTTLE_UPLOAD,0);*/
	CheckDlgButton(IDC_THROTTLE_UPLOAD,0); //Rectencle (own function)
   	if (app_prefs->prefs->dynamicupload)
		CheckDlgButton(IDC_DYNUP,1);
	else
		CheckDlgButton(IDC_DYNUP,0);

	if(m_hWnd)
	{
		CString strBuffer;
		strBuffer.Format("%d", app_prefs->prefs->queuesize);
		GetDlgItem(IDC_QUEUESIZE)->SetWindowText(strBuffer);

		strBuffer.Format("%d", app_prefs->prefs->remotequeuelimit);
		GetDlgItem(IDC_QR_LIMIT)->SetWindowText(strBuffer);

		strBuffer = app_prefs->prefs->url_ping;
		strBuffer.Trim();
		GetDlgItem(IDC_URL_PING)->SetWindowText(strBuffer);

		strBuffer.Format("%d", app_prefs->prefs->base_ping);
		GetDlgItem(IDC_BREAKEVENMS)->SetWindowText(strBuffer);
		GetDlgItem(IDC_URL_PING)->EnableWindow(IsDlgButtonChecked(IDC_DYNUP));
		GetDlgItem(IDC_BREAKEVENMS)->EnableWindow(IsDlgButtonChecked(IDC_DYNUP));

		strBuffer.Format("%d", app_prefs->prefs->max_xs_at_once);
		GetDlgItem(IDC_MAXXSATONCE)->SetWindowText(strBuffer);
	}
	// End by Tarod
}

BOOL CPPgTweaks::OnApply()
{
	char buffer[510];
	if(GetDlgItem(IDC_MAXCON5SEC)->GetWindowTextLength())
	{
		GetDlgItem(IDC_MAXCON5SEC)->GetWindowText(buffer,20);
		app_prefs->SetMaxDownloadConperFive((atoi(buffer)) ?
			atoi(buffer) : 20);
	}
	if(IsDlgButtonChecked(IDC_VERBOSE))
		app_prefs->prefs->m_bVerbose = true;
	else
		app_prefs->prefs->m_bVerbose = false;

	if(IsDlgButtonChecked(IDC_UPDATEQUEUE))
		app_prefs->prefs->m_bupdatequeuelist = true;
	else
		app_prefs->prefs->m_bupdatequeuelist = false;

	// Barry
	app_prefs->prefs->autotakeed2klinks = (int8)IsDlgButtonChecked(IDC_AUTOTAKEED2KLINKS);

	// Added by Tarod -applying of new preferences
	app_prefs->prefs->disablexs = (int8)IsDlgButtonChecked(IDC_DISABLE_XS) ;
	app_prefs->prefs->throttleupload = (int8)IsDlgButtonChecked(IDC_THROTTLE_UPLOAD) ;

	if (GetDlgItem(IDC_QUEUESIZE)->GetWindowTextLength()) {
		GetDlgItem(IDC_QUEUESIZE)->GetWindowText(buffer, 20) ;
		if (atoi(buffer) < 300 && atoi(buffer) != 0) {
			app_prefs->prefs->queuesize = 300 ;
			sprintf(buffer, "%d", app_prefs->prefs->queuesize);
			GetDlgItem(IDC_QUEUESIZE)->SetWindowText(buffer) ;
		} else if (atoi(buffer) > 2000) {
			app_prefs->prefs->queuesize = 2000 ;
			sprintf(buffer, "%d", app_prefs->prefs->queuesize);
			GetDlgItem(IDC_QUEUESIZE)->SetWindowText(buffer) ;
		} else {
			app_prefs->prefs->queuesize = atoi(buffer) ;
		}
	}

	if (GetDlgItem(IDC_QR_LIMIT)->GetWindowTextLength()) {
		GetDlgItem(IDC_QR_LIMIT)->GetWindowText(buffer, 20) ;
		if (atoi(buffer) < 10 && atoi(buffer) != 0) {
			app_prefs->prefs->remotequeuelimit = 10 ;
			sprintf(buffer, "%d", app_prefs->prefs->queuesize);
			GetDlgItem(IDC_QR_LIMIT)->SetWindowText(buffer) ;
		} else if (atoi(buffer) > 5000) {
			app_prefs->prefs->remotequeuelimit = 5000 ;
			sprintf(buffer, "%d", app_prefs->prefs->remotequeuelimit);
			GetDlgItem(IDC_QR_LIMIT)->SetWindowText(buffer) ;
		} else {
			app_prefs->prefs->remotequeuelimit = (atoi(buffer) / 10) * 10; // convert to 10 multiple
			sprintf(buffer, "%d", app_prefs->prefs->remotequeuelimit);
			GetDlgItem(IDC_QR_LIMIT)->SetWindowText(buffer);
		}
	}

	if (GetDlgItem(IDC_URL_PING)->GetWindowTextLength()) {
		CString strBuffer;
		GetDlgItem(IDC_URL_PING)->GetWindowText(strBuffer);
		strBuffer.Trim();
		strcpy(app_prefs->prefs->url_ping, strBuffer.GetBuffer());
	}

	if (GetDlgItem(IDC_BREAKEVENMS)->GetWindowTextLength()) {
		GetDlgItem(IDC_BREAKEVENMS)->GetWindowText(buffer, 20) ;
		if (atoi(buffer) < 100) {
			app_prefs->prefs->base_ping = 100 ;
			sprintf(buffer, "%d", app_prefs->prefs->base_ping);
			GetDlgItem(IDC_BREAKEVENMS)->SetWindowText(buffer) ;
		} else if (atoi(buffer) > 800) {
			app_prefs->prefs->base_ping = 800 ;
			sprintf(buffer, "%d", app_prefs->prefs->base_ping);
			GetDlgItem(IDC_BREAKEVENMS)->SetWindowText(buffer);
		} else {
			app_prefs->prefs->base_ping = (atoi(buffer) / 100) * 100; // convert to 100 multiple
			sprintf(buffer, "%d", app_prefs->prefs->base_ping);
			GetDlgItem(IDC_BREAKEVENMS)->SetWindowText(buffer);
		}
	}
	GetDlgItem(IDC_URL_PING)->EnableWindow(IsDlgButtonChecked(IDC_DYNUP));
	GetDlgItem(IDC_BREAKEVENMS)->EnableWindow(IsDlgButtonChecked(IDC_DYNUP));

	if (GetDlgItem(IDC_MAXXSATONCE)->GetWindowTextLength()) {
		GetDlgItem(IDC_MAXXSATONCE)->GetWindowText(buffer, 20) ;
		if (atoi(buffer) < 50) {
			app_prefs->prefs->max_xs_at_once = 50 ;
			sprintf(buffer, "%d", app_prefs->prefs->max_xs_at_once);
			GetDlgItem(IDC_MAXXSATONCE)->SetWindowText(buffer) ;
		} else if (atoi(buffer) > 500) {
			app_prefs->prefs->max_xs_at_once = 500 ;
			sprintf(buffer, "%d", app_prefs->prefs->max_xs_at_once);
			GetDlgItem(IDC_MAXXSATONCE)->SetWindowText(buffer);
		} else {
			app_prefs->prefs->max_xs_at_once = (atoi(buffer) / 50) * 50; // convert to 50 multiple
			sprintf(buffer, "%d", app_prefs->prefs->max_xs_at_once);
			GetDlgItem(IDC_MAXXSATONCE)->SetWindowText(buffer);
		}
	}

	theApp.Dynup->StopDynup(); // Refresh Dynamic Upload
	app_prefs->prefs->dynamicupload = (int8)IsDlgButtonChecked(IDC_DYNUP) ;
	if ((bool)app_prefs->prefs->dynamicupload) {
		if (!theApp.Dynup->SetIP(1, app_prefs->GetMaxGraphUploadRate(), app_prefs->GetURLPing())) {
			MessageBox("URL is not valid! Please, enter new valid one.");
			GetDlgItem(IDC_URL_PING)->SetWindowText("");
		} else {
			theApp.Dynup->SetInitialPing();
			theApp.Dynup->StartDynup();
		}
	}
	// End by Tarod

	LoadSettings();
	SetModified(FALSE);
	theApp.emuledlg->serverwnd.ToggleDebugWindow();
	return CPropertyPage::OnApply();
}

void CPPgTweaks::Localize(void)
{	
	if(m_hWnd)
	{
		SetWindowText(GetResString(IDS_PW_TWEAK));
		GetDlgItem(IDC_MAXCON5SECLABEL )->SetWindowText(GetResString(IDS_MAXCON5SECLABEL));
		GetDlgItem(IDC_WARNING)->SetWindowText(GetResString(IDS_WARNING));
		GetDlgItem(IDC_UPDATEQUEUE)->SetWindowText(GetResString(IDS_UPDATEQUEUE));
		GetDlgItem(IDC_AUTOTAKEED2KLINKS)->SetWindowText(GetResString(IDS_AUTOTAKEED2KLINKS));
		GetDlgItem(IDC_VERBOSE)->SetWindowText(GetResString(IDS_VERBOSE));
	}
}

