//this file is part of eMule
//Copyright (C)2002 Merkur ( merkur-@users.sourceforge.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.

// ClientDetailDialog.cpp : implementation file
//

#include "stdafx.h"
#include "emule.h"
#include "ClientDetailDialog.h"
#include "otherfunctions.h"


// CClientDetailDialog dialog

IMPLEMENT_DYNAMIC(CClientDetailDialog, CDialog)
CClientDetailDialog::CClientDetailDialog(CUpDownClient* client)
	: CDialog(CClientDetailDialog::IDD, 0)
{
	m_client = client;
}

CClientDetailDialog::~CClientDetailDialog()
{
}

void CClientDetailDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BOOL CClientDetailDialog::OnInitDialog(){
	CDialog::OnInitDialog();
	Localize();

	// Track loaded sources name [Tarod]
	bool loadedSourceName = false ;
	if (m_client->GetUserName()) {
		loadedSourceName = (strstr(m_client->GetUserName(), IDS_LOADEDSOURCE) != (char*)NULL)
							|| (strstr(m_client->GetUserName(), IDS_EXCHANGEDSOURCE) != (char*)NULL) ;
	}
	char buffer[100];
	if (m_client->GetUserName())
		GetDlgItem(IDC_DNAME)->SetWindowText(m_client->GetUserName());
	else
		GetDlgItem(IDC_DNAME)->SetWindowText("?");
	
	if (m_client->GetUserName() && !loadedSourceName){
		buffer[0] = 0;
		GetDlgItem(IDC_DHASH)->SetWindowText(EncodeBase16(m_client->GetUserHash(), 16));
	}
	else
		GetDlgItem(IDC_DHASH)->SetWindowText("?");
	
	switch(m_client->GetClientSoft()){
		case SO_UNKNOWN:
			GetDlgItem(IDC_DSOFT)->SetWindowText("?");
			GetDlgItem(IDC_DVERSION)->SetWindowText("?");
			break;
		case SO_EMULE:
		case SO_OLDEMULE:
			GetDlgItem(IDC_DSOFT)->SetWindowText("eMule");
			if (m_client->GetMuleVersion()){
				sprintf(buffer,"v0.%02X",m_client->GetMuleVersion());
				if (m_client->IsTarodVersion()) {
					if (m_client->GetTarodVersionNumber() > 0) {
						sprintf(buffer, "%s [Tarod.%03x", buffer, m_client->GetTarodVersionNumber());
						// if last version digit is 0 just trim it off
						if (buffer[strlen(buffer) - 1] == '0') {
							buffer[strlen(buffer) - 1] = ']';
						} else {
							strcat(buffer, "]");
						}
					} else {
						sprintf(buffer, "%s [Tarod/eMule Plus]", buffer);
					}
				}
				GetDlgItem(IDC_DVERSION)->SetWindowText(buffer);
			}
			else
				GetDlgItem(IDC_DVERSION)->SetWindowText("?");
			break;
		case SO_EDONKEY:
			GetDlgItem(IDC_DSOFT)->SetWindowText("eDonkey");
			sprintf(buffer,"v%i",m_client->GetVersion());
			GetDlgItem(IDC_DVERSION)->SetWindowText(buffer);
			break;
		case SO_MLDONKEY:
			GetDlgItem(IDC_DSOFT)->SetWindowText("mlDonkey");
			sprintf(buffer,"v%i",m_client->GetVersion());
			GetDlgItem(IDC_DVERSION)->SetWindowText(buffer);
			break;
	}

	sprintf(buffer,"%u (%s)",m_client->GetUserID(),(m_client->HasLowID() ? GetResString(IDS_PRIOLOW):GetResString(IDS_PRIOHIGH)));
	GetDlgItem(IDC_DID)->SetWindowText(buffer);
	
	sprintf(buffer,"%s:%i",m_client->GetFullIP(),m_client->GetUserPort());
	GetDlgItem(IDC_DIP)->SetWindowText(buffer);

	if (m_client->GetServerIP()){
		in_addr server;
		server.S_un.S_addr = m_client->GetServerIP();
		GetDlgItem(IDC_DSIP)->SetWindowText(inet_ntoa(server));
		
		CServer* cserver = theApp.serverlist->GetServerByAddress(inet_ntoa(server), m_client->GetServerPort()); 
		if (cserver)
			GetDlgItem(IDC_DSNAME)->SetWindowText(cserver->GetListName());
		else
			GetDlgItem(IDC_DSNAME)->SetWindowText("?");
	}
	else{
		GetDlgItem(IDC_DSIP)->SetWindowText("?");
		GetDlgItem(IDC_DSNAME)->SetWindowText("?");
	}

	CKnownFile* file = theApp.sharedfiles->GetFileByID(m_client->reqfileid);
	if (file)
		GetDlgItem(IDC_DDOWNLOADING)->SetWindowText(MakeStringEscaped(file->GetFileName()));
	else
		GetDlgItem(IDC_DDOWNLOADING)->SetWindowText("-");


	CastItoXBytes(m_client->GetTransferedDown(),buffer);
	GetDlgItem(IDC_DDUP)->SetWindowText(buffer);

	CastItoXBytes(m_client->GetTransferedUp(),buffer);
	GetDlgItem(IDC_DDOWN)->SetWindowText(buffer);

	sprintf(buffer,"%.1f %s",(float)m_client->GetDownloadDatarate()/1024,GetResString(IDS_KBYTESEC));
	GetDlgItem(IDC_DAVUR)->SetWindowText(buffer);

	sprintf(buffer,"%.1f %s",(float)m_client->GetDatarate()/1024,GetResString(IDS_KBYTESEC));
	GetDlgItem(IDC_DAVDR)->SetWindowText(buffer);
	
	if (m_client->Credits()){
		CastItoXBytes(m_client->Credits()->GetDownloadedTotal(),buffer);
		GetDlgItem(IDC_DUPTOTAL)->SetWindowText(buffer);
		CastItoXBytes(m_client->Credits()->GetUploadedTotal(),buffer);
		GetDlgItem(IDC_DDOWNTOTAL)->SetWindowText(buffer);
		sprintf(buffer,"%.1f",(float)m_client->Credits()->GetScoreRatio());
		GetDlgItem(IDC_DRATIO)->SetWindowText(buffer);
	}
	else{
		GetDlgItem(IDC_DDOWNTOTAL)->SetWindowText("?");
		GetDlgItem(IDC_DUPTOTAL)->SetWindowText("?");
		GetDlgItem(IDC_DRATIO)->SetWindowText("?");
	}

	if (m_client->GetUserName() && !loadedSourceName){
		sprintf(buffer,"%.1f",(float)m_client->GetScore(false,m_client->IsDownloading(),true));
		GetDlgItem(IDC_DRATING)->SetWindowText(buffer);
	}
	else
		GetDlgItem(IDC_DRATING)->SetWindowText("?");;

	if (m_client->GetUploadState() != US_NONE){
		if (!m_client->GetFriendSlot()){
			sprintf(buffer,"%u",m_client->GetScore(false,m_client->IsDownloading(),false));
			GetDlgItem(IDC_DSCORE)->SetWindowText(buffer);
		}
		else
			GetDlgItem(IDC_DSCORE)->SetWindowText(GetResString(IDS_FRIENDDETAIL));
	}
	else
		GetDlgItem(IDC_DSCORE)->SetWindowText("-");
	return true;
}

BEGIN_MESSAGE_MAP(CClientDetailDialog, CDialog)
END_MESSAGE_MAP()


// CClientDetailDialog message handlers
void CClientDetailDialog::Localize(){
	GetDlgItem(IDC_STATIC30)->SetWindowText(GetResString(IDS_CD_GENERAL));
	GetDlgItem(IDC_STATIC31)->SetWindowText(GetResString(IDS_CD_UNAME));
	GetDlgItem(IDC_STATIC32)->SetWindowText(GetResString(IDS_CD_UHASH));
	GetDlgItem(IDC_STATIC33)->SetWindowText(GetResString(IDS_CD_CSOFT));
	GetDlgItem(IDC_STATIC34)->SetWindowText(GetResString(IDS_CD_UIP));
	GetDlgItem(IDC_STATIC35)->SetWindowText(GetResString(IDS_CD_SIP));
	GetDlgItem(IDC_STATIC36)->SetWindowText(GetResString(IDS_CD_VERSION));
	GetDlgItem(IDC_STATIC37)->SetWindowText(GetResString(IDS_CD_UID));
	GetDlgItem(IDC_STATIC38)->SetWindowText(GetResString(IDS_CD_SNAME));

	GetDlgItem(IDC_STATIC40)->SetWindowText(GetResString(IDS_CD_TRANS));
	GetDlgItem(IDC_STATIC41)->SetWindowText(GetResString(IDS_CD_CDOWN));
	GetDlgItem(IDC_STATIC42)->SetWindowText(GetResString(IDS_CD_DOWN));
	GetDlgItem(IDC_STATIC43)->SetWindowText(GetResString(IDS_CD_ADOWN));
	GetDlgItem(IDC_STATIC44)->SetWindowText(GetResString(IDS_CD_TDOWN));
	GetDlgItem(IDC_STATIC45)->SetWindowText(GetResString(IDS_CD_UP));
	GetDlgItem(IDC_STATIC46)->SetWindowText(GetResString(IDS_CD_AUP));
	GetDlgItem(IDC_STATIC47)->SetWindowText(GetResString(IDS_CD_TUP));

	GetDlgItem(IDC_STATIC50)->SetWindowText(GetResString(IDS_CD_SCORES));
	GetDlgItem(IDC_STATIC51)->SetWindowText(GetResString(IDS_CD_MOD));
	GetDlgItem(IDC_STATIC52)->SetWindowText(GetResString(IDS_CD_RATING));
	GetDlgItem(IDC_STATIC53)->SetWindowText(GetResString(IDS_CD_USCORE));

	GetDlgItem(IDOK)->SetWindowText(GetResString(IDS_FD_CLOSE));

	SetWindowText(GetResString(IDS_CD_TITLE));

}