//this file is part of eMule
//Copyright (C)2002 Merkur ( devs@emule-project.net / http://www.emule-project.net )
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either
//version 2 of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "stdafx.h"
#include "emule.h"
#include "emuleDlg.h"
#include "PPgAckronicII.h"
#include "OtherFunctions.h"
#include "Preferences.h"
#include "Opcodes.h"
#include "Mod\Ip2Country.h"
// NEO: PP - [Family filter] -- Xanatos -->
#include "InputBox.h"
#include "MD5Sum.h" 
#include "Knownfile.h"
#include ".\ppgackronicii.h"
// NEO: PP END <-- Xanatos --

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


IMPLEMENT_DYNAMIC(CPPgAckronicII, CPropertyPage)

BEGIN_MESSAGE_MAP(CPPgAckronicII, CPropertyPage)
	//Ackronic START - Aggiunto da Aenarion[ITA] - Family Filter
	ON_BN_CLICKED(IDC_FF_CHK, OnClickButton)
	ON_BN_CLICKED(IDC_FF2_CHK, OnSettingsChange)
	ON_BN_CLICKED(IDC_CHANGE_PASSFF, OnBnClickedChangePassff)
	//ON_BN_CLICKED(IDC_FF_CHK, OnSettingsChange)
	//ON_EN_CHANGE(IDC_FF_TXT, OnSettingsChange)
	//Ackronic END - Aggiunto da Aenarion[ITA] - Family Filter
	ON_BN_CLICKED(IDC_CHKLOWIDNOTIFIER, OnSettingsChange)//Low-id notifier
	ON_BN_CLICKED(IDC_ABILITABANDIERINE, OnSettingsChange)//Ackronic - Aggiunto da Aenarion[ITA] - IP to country
	//ON_BN_CLICKED(IDC_SLOTFOCUS, OnBnClickedChkSlotFocus)//Ackronic - Slot Focus
	//ON_WM_HELPINFO()
	ON_WM_HSCROLL()
	ON_WM_DESTROY()
	
END_MESSAGE_MAP()

CPPgAckronicII::CPPgAckronicII()
	: CPropertyPage(CPPgAckronicII::IDD)
{
	m_iFileBufferSize = 0;//Buffer Size
	m_iBufferTimeLimit = 0; //FrankyFive: Buffer time limit
	m_iNiceHashLoadWeight = 100;//Ackronic - Aggiunto da Aenarion[ITA] - Nicehash
	UpDataratePerClient = 0;//Ackronic - Upload data rate
}

CPPgAckronicII::~CPPgAckronicII()
{
}

void CPPgAckronicII::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_BTL, m_ctlBufferTimeLimit); //FrankyFive: Buffer Time Limit
	DDX_Control(pDX, IDC_FILEBUFFERSIZE, m_ctlFileBuffSize);//BufferSize
	DDX_Control(pDX, IDC_NICEHASH, m_ctlNiceHashLoadWeight);//Ackronic - Aggiunto da Aenarion[ITA] - Nicehash
	DDX_Control(pDX, IDC_UP_SLOTS_SLIDER, m_ctlUpDataratePerClient);
}

BOOL CPPgAckronicII::OnInitDialog()
{
	CPropertyPage::OnInitDialog();
	InitWindowStyles(this);
//Ackronic - Upload data rate
	//uint32 MaxUpSpeed = (thePrefs.maxupload==UNLIMITED)?thePrefs.maxGraphUploadRate:thePrefs.maxupload;
	UpDataratePerClient = thePrefs.UpDataratePerClient;
	m_ctlUpDataratePerClient.SetRange(1, 30, TRUE);
	m_ctlUpDataratePerClient.SetPos(UpDataratePerClient);
//Ackronic END - Upload data rate

	//FrankyFive: Buffer Time Limit - start
	m_iBufferTimeLimit = thePrefs.m_iBufferTimeLimit;
	m_ctlBufferTimeLimit.SetRange(1, 30, TRUE);
	m_ctlBufferTimeLimit.SetPos(m_iBufferTimeLimit);
	m_ctlBufferTimeLimit.SetTicFreq(1);
	m_ctlBufferTimeLimit.SetPageSize(1);
    //FrankyFive: Buffer Time Limit - end

	m_iFileBufferSize = thePrefs.m_iFileBufferSize;
	m_ctlFileBuffSize.SetRange(16, /*1024+512*/10*1024, TRUE);//Ackronic - Modificato da Aenarion[ITA] - Ott.Opzioni
	int iMin, iMax;
	m_ctlFileBuffSize.GetRange(iMin, iMax);
	m_ctlFileBuffSize.SetPos(m_iFileBufferSize/1024);
	int iPage = 256;//Ackronic - Modificato da Aenarion[ITA] - Ott.Opzioni
	for (int i = ((iMin+iPage-1)/iPage)*iPage; i < iMax; i += iPage)
		m_ctlFileBuffSize.SetTic(i);
	m_ctlFileBuffSize.SetPageSize(iPage);

	//Ackronic START - Aggiunto da Aenarion[ITA] - Nicehash
	m_iNiceHashLoadWeight = thePrefs.m_iNiceHashLoadWeight;
	m_ctlNiceHashLoadWeight.SetRange(1, 10);
	m_ctlNiceHashLoadWeight.SetPos((m_iNiceHashLoadWeight/10));
	//Ackronic END - Aggiunto da Aenarion[ITA] - Nicehash

	LoadSettings();
	Localize();

	return TRUE;  // return TRUE unless you set the focus to a control
				  // EXCEPTION: OCX Property Pages should return FALSE
}
void CPPgAckronicII::LoadSettings(void)
{
	//DkD - [lowid notifier] by chamblard
	CheckDlgButton(IDC_CHKLOWIDNOTIFIER, thePrefs.m_bShowLowID);
	//
	//Ackronic - Aggiunto da Aenarion[ITA] - IP to country
	CheckDlgButton(IDC_ABILITABANDIERINE, thePrefs.AbilitaBandierine);
	//
	//Ackronic START - Aggiunto da Aenarion[ITA] - Family Filter
	CheckDlgButton(IDC_FF_CHK, thePrefs.m_bFamilyFilter);
	CheckDlgButton(IDC_FF2_CHK, thePrefs.m_bFakeFilter);
	GetDlgItem(IDC_CHANGE_PASSFF)->EnableWindow(thePrefs.m_isPWProtShow);
	//GetDlgItem(IDC_FF_TXT)->SetWindowText(thePrefs.m_cFamilyFilter);
	//Ackronic END - Aggiunto da Aenarion[ITA] - Family Filter
	
}

BOOL CPPgAckronicII::OnApply()
{
	if (!UpdateData())
		return FALSE;

///////////
	thePrefs.UpDataratePerClient = UpDataratePerClient;
///////////

	GetDlgItem(IDC_CHANGE_PASSFF)->EnableWindow(thePrefs.m_isPWProtShow);

	
	//DkD - [lowid notifier] by chamblard
	thePrefs.m_bShowLowID = IsDlgButtonChecked(IDC_CHKLOWIDNOTIFIER)!=0;
	//
	//Ackronic - Aggiunto da Aenarion[ITA] - IP to country
	thePrefs.AbilitaBandierine = IsDlgButtonChecked(IDC_ABILITABANDIERINE)!=0;
	//
	//Ackronic START - Aggiunto da Aenarion[ITA] - Family Filter
	//thePrefs.m_bFamilyFilter = IsDlgButtonChecked(IDC_FF_CHK) !=0;//nn "applicarlo" se usi la pass
	thePrefs.m_bFakeFilter = IsDlgButtonChecked(IDC_FF2_CHK) !=0;
	//GetDlgItem(IDC_FF_TXT)->GetWindowText(thePrefs.m_cFamilyFilter,511);
	//Ackronic END - Aggiunto da Aenarion[ITA] - Family Filter

	thePrefs.m_iBufferTimeLimit = m_iBufferTimeLimit; //FrankyFive: Buffer Time Limit
	thePrefs.m_iFileBufferSize = m_iFileBufferSize;//BufferSize
	thePrefs.m_iNiceHashLoadWeight = m_iNiceHashLoadWeight;//Ackronic - Aggiunto da Aenarion[ITA] - Nicehash

	if(thePrefs.GetAbilitaBandierine() && !theApp.ip2country->ShowCountryFlag())
		theApp.ip2country->Load();

	if(!thePrefs.GetAbilitaBandierine() && EnableCountryFlag/* == true*/)
		theApp.ip2country->Unload();

	LoadSettings();
	SetModified(FALSE);
	return CPropertyPage::OnApply();
}

void CPPgAckronicII::Localize(void)
{
	if (m_hWnd)
	{

		
		//DkD - [lowid notifier] by chamblard
        GetDlgItem(IDC_CHKLOWIDNOTIFIER)->SetWindowText(GetResString(IDS_ACTIVATELOWIDNOTIFIER));
		if(UpDataratePerClient ==1)
		GetDlgItem(IDC_UP_DR_PER_CLIENT2)->SetWindowText(_T(""));
		else
		GetDlgItem(IDC_UP_DR_PER_CLIENT2)->SetWindowText(_T("bps"));
		//
		//Ackronic - Aggiunto da Aenarion[ITA] - IP to country
		GetDlgItem(IDC_ABILITABANDIERINE)->SetWindowText(GetResString(IDS_ABILITABANDIERINE));
		//
		//Ackronic START - Aggiunto da Aenarion[ITA] - Family Filter
		GetDlgItem(IDC_FF_CHK)->SetWindowText(GetResString(IDS_ACTIVATE_FF));
		GetDlgItem(IDC_FF_LBL)->SetWindowText(GetResString(IDS_FILTERED_EXPRESSIONS_FF));
        GetDlgItem(IDC_FF_GROUP)->SetWindowText(GetResString(IDS_FF_GROUP));
		GetDlgItem(IDC_CHANGE_PASSFF)->SetWindowText(GetResString(IDS_CHANGE_PASSFF));
		GetDlgItem(IDC_FF2_CHK)->SetWindowText(GetResString(IDS_ACTIVATE_FF2));
		//Ackronic START - Aggiunto da Aenarion[ITA] - Family Filter

		//Upload slots
		GetDlgItem(IDC_UP_SLOTS_LABEL)->SetWindowText(GetResString(IDS_UP_SLOTS));
		GetDlgItem(IDC_UP_DR_PER_CLIENT_LABEL)->SetWindowText(GetResString(IDS_UP_SPEED_PER_CLIENT));		
		//end up slots
		
		CString temp;
		uint32 MaxUpSpeed = (thePrefs.maxupload==UNLIMITED)?thePrefs.maxGraphUploadRate:thePrefs.maxupload;
		//FrankyFive: Buffer Time Limit - start
		temp.Format(_T("%s: %i min"), GetResString(IDS_BTL_TEXT), m_iBufferTimeLimit);
		GetDlgItem(IDC_BTL_TEXT)->SetWindowText(temp);
		//FrankyFive: Buffer Time Limit - end
		temp.Format(_T("%s: %s"), GetResString(IDS_FILEBUFFERSIZE), CastItoXBytes(m_iFileBufferSize, false, false));
		GetDlgItem(IDC_FILEBUFFERSIZE_STATIC)->SetWindowText(temp);
		//Ackronic START - Aggiunto da Aenarion[ITA] - Nicehash
		temp.Format(_T("%s: %s%%"), GetResString(IDS_NICEHASH_CPULOAD), GetFormatedUInt(m_iNiceHashLoadWeight));
		GetDlgItem(IDC_NICEHASH_STATIC)->SetWindowText(temp);
		GetDlgItem(IDC_FAST_HASH)->SetWindowText(GetResString(IDS_FAST_HASH));
		GetDlgItem(IDC_LOW_HASH)->SetWindowText(GetResString(IDS_LOW_HASH));
		//Ackronic END - Aggiunto da Aenarion[ITA] - Nicehash

		temp.Format(_T("%u"), UpDataratePerClient);
		GetDlgItem(IDC_UP_SLOTS)->SetWindowText(temp);
		if(UpDataratePerClient ==1)
		temp.Format(GetResString(IDS_SLOTFOCUS));
		else
		temp.Format(_T("%u"),(MaxUpSpeed*1024)/UpDataratePerClient);
		GetDlgItem(IDC_UP_DR_PER_CLIENT)->SetWindowText(temp);
	}
}
void CPPgAckronicII::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{//Quando muovi col mouse gli indicatori delle barre succede questo:
	SetModified(TRUE);
	//Ackronic START - Aggiunto da Aenarion[ITA] - Nicehash
	if (pScrollBar->GetSafeHwnd() == m_ctlNiceHashLoadWeight.m_hWnd) {
		m_iNiceHashLoadWeight = ((CSliderCtrl*)pScrollBar)->GetPos()*10;
		CString temp;
		temp.Format(_T("%s: %s%%"), GetResString(IDS_NICEHASH_CPULOAD), GetFormatedUInt(m_iNiceHashLoadWeight));
		GetDlgItem(IDC_NICEHASH_STATIC)->SetWindowText(temp);
		SetModified(TRUE);
	}
	//Ackronic END - Aggiunto da Aenarion[ITA] - Nicehash
	//FrankyFive: Buffer Time Limit - start
	else if (pScrollBar->GetSafeHwnd() == m_ctlBufferTimeLimit.m_hWnd) 
	{
		m_iBufferTimeLimit = ((CSliderCtrl*)pScrollBar)->GetPos();
	        CString temp;
		temp.Format(_T("%s: %i min"), GetResString(IDS_BTL_TEXT), m_iBufferTimeLimit);
		GetDlgItem(IDC_BTL_TEXT)->SetWindowText(temp);
		SetModified(TRUE);
        }
	//FrankyFive: Buffer Time Limit - end
	else if (pScrollBar->GetSafeHwnd() == m_ctlFileBuffSize.m_hWnd)
	{
		m_iFileBufferSize = m_ctlFileBuffSize.GetPos() * 1024;
        CString temp;
		temp.Format(_T("%s: %s"), GetResString(IDS_FILEBUFFERSIZE), CastItoXBytes(m_iFileBufferSize, false, false));
		GetDlgItem(IDC_FILEBUFFERSIZE_STATIC)->SetWindowText(temp);
		SetModified(TRUE);
	}
	else if (pScrollBar->GetSafeHwnd() == m_ctlUpDataratePerClient.m_hWnd)
	{
		UpDataratePerClient = m_ctlUpDataratePerClient.GetPos();
		CString temp;
		
		
		uint32 MaxUpSpeed = (thePrefs.maxupload==UNLIMITED)?thePrefs.maxGraphUploadRate:thePrefs.maxupload;
		temp.Format(_T("%u"),UpDataratePerClient);
		GetDlgItem(IDC_UP_SLOTS)->SetWindowText(temp);
		if(UpDataratePerClient ==1){
		GetDlgItem(IDC_UP_DR_PER_CLIENT2)->SetWindowText(_T(""));
		temp.Format(GetResString(IDS_SLOTFOCUS));
		}else{
		GetDlgItem(IDC_UP_DR_PER_CLIENT2)->SetWindowText(_T("bps"));
		temp.Format(_T("%u"),(MaxUpSpeed*1024)/UpDataratePerClient);
		}
		GetDlgItem(IDC_UP_DR_PER_CLIENT)->SetWindowText(temp);
		SetModified(TRUE);
	}
	
	UpdateData(false); 
	CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar);
	
}
void CPPgAckronicII::OnClickButton()
{
	if(thePrefs.m_sPassFF.IsEmpty() || thePrefs.m_isPWProtShow==false){
		 
	InputBox inputbox1;
	CString pass1 = _T("");
	
	inputbox1.SetLabels(GetResString(IDS_FAMILY_FILTER),GetResString(IDS_NEWPASS), _T(""));
	inputbox1.SetPassword(true);
	
	if (inputbox1.DoModal() == IDOK)
		pass1=inputbox1.GetInput();
	else
		return;
	
	if(pass1.GetLength() > 0)
	{
		thePrefs.SetPWProtShow(true);
		thePrefs.m_sPassFF=MD5Sum(pass1).GetHash().GetBuffer(0);
	}
	else
		return;
	
	}

	if(thePrefs.m_isPWProtShow==true)
	{
	InputBox inputbox;
	CString pass;
	inputbox.SetLabels(GetResString(IDS_FAMILY_FILTER),GetResString(IDS_CONFIRMPASS), _T(""));	
	inputbox.SetPassword(true);
	if (inputbox.DoModal() == IDOK && inputbox.GetInput().GetLength() > 0){
		pass=inputbox.GetInput();
		pass=MD5Sum(pass).GetHash().GetBuffer(0);

			if(thePrefs.GetPassFF() == pass){
				thePrefs.m_bFamilyFilter = IsDlgButtonChecked(IDC_FF_CHK) !=0;
				SetModified(TRUE);
			}
			else SetModified(FALSE);
		}
	else SetModified(FALSE);
	}
	
}
void CPPgAckronicII::OnBnClickedChangePassff()
{
	if(thePrefs.m_sPassFF.IsEmpty() || thePrefs.m_isPWProtShow==false)
	{
		AfxMessageBox(GetResString(IDS_NONCEPASS) ,MB_OK);
		return;
	}
	InputBox inputbox;
	CString pass;
	inputbox.SetLabels(GetResString(IDS_FAMILY_FILTER),GetResString(IDS_CHANGEPASS), _T(""));	
	inputbox.SetPassword(true);
	if (inputbox.DoModal() == IDOK) {
		
		if(inputbox.GetInput().GetLength() > 0){
		pass=inputbox.GetInput();
		pass=MD5Sum(pass).GetHash().GetBuffer(0);

			if(thePrefs.GetPassFF() == pass){
				InputBox inputbox1;
				CString pass1 = _T("");
				inputbox1.SetLabels(GetResString(IDS_FAMILY_FILTER),GetResString(IDS_CHANGEORDELETEPASS), _T(""));
				inputbox1.SetPassword(true);

				if (inputbox1.DoModal() == IDOK){
					pass1=inputbox1.GetInput();
				}
				else
					return;
	
				if(pass1.GetLength() > 0)
				{
				thePrefs.m_sPassFF=MD5Sum(pass1).GetHash().GetBuffer(0);
				}
				/*else
				if(pass1.GetLength() == 0){

					AfxMessageBox(GetResString(IDS_DELETEPASS) ,MB_OK);
					thePrefs.m_sPassFF=_T("");
					thePrefs.SetPWProtShow(false);
					GetDlgItem(IDC_CHANGE_PASSFF)->EnableWindow(false);

				}*/

			}
			
	}
		
	}

}
