/*----------------------------------------------------------
  PROJECT		: mldkgui

  FILE			: cMLDonkeyAction.cpp
  VERSION		: 1.0

  CREATION		: 08.03.2004
  AUTHOR		: JLac
  LANGUAGE		: cpp
  AIM			: implementation of the cMLDonkeyAction class.

  HISTORIC  	: -

-----------------------------------------------------------*/
#include <list> 
#include <vector> 
#include <string> 

using namespace std;

#include "typedef.h"
#include "defines.h"

#include "osd.h"

#include "cFileInfo.h"
#include "cItemAction.h"
#include "cStreamData.h"
#include "cNetworkInfo.h"

#include "cServerInfo.h"
#include "cLstServerInfo.h"
#include "cMLDonkeyStatus.h"

#include "cOsdBasicDisplay.h"
#include "cOsdListReportMenu.h"
#include "cOsdDownloadingFilesMenu.h"

#include "cMLDonkeyAction.h"

#include "LogMsg.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

char *Size2Str(int sz,char *out)
{
	if(sz > 1024*1024)
		sprintf(out,"%dMB",sz/(1024*1024));
	else
	{
		if(sz > 1024)
			sprintf(out,"%dkB",sz/1024);
		else
			sprintf(out,"%dB",sz);
	}

	return(out);
}

cMLDonkeyAction::cMLDonkeyAction()
{

}

cMLDonkeyAction::~cMLDonkeyAction()
{

}

/*******************************************************************
 *              
 *  FUNCTION:   ProcessAction
 *              
 *  RETURNS:    cOsdBasicDisplay *
 *              
 *  PARAMETERS: 
 *		Input  :
 *          cItemAction *act
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
cOsdBasicDisplay *cMLDonkeyAction::ProcessAction(cItemAction *act)
{
	cOsdBasicDisplay *ptmp = NULL;

    if( act != NULL )
    {
	    switch(act->m_Type)
	    {
		    case ACTION_VIEW_MAINMENU:
			    ptmp = GetMainMenu();
			    break;

		    case ACTION_VIEW_DOWNLOADING:
			    ptmp = GetMenuOfDownloadingFiles();
			    break;

		    case ACTION_VIEW_DETAILOFDOWNLOADINGFILES:
			    ptmp = GetMenuOfDetailOfDownloadingFiles(act->pParam);
			    break;

			case ACTION_VIEW_SERVER:
			    ptmp = GetMenuOfViewServers();
				break;

		    case ACTION_VIEW_DOWNLOADED:
			    ptmp = GetMainMenu();
			    break;

			case ACTION_VIEW_STATISTIC:
			    ptmp = GetMenuOfStatistic();
			    break;

				

	    }
    }

	return(ptmp);
}

/*******************************************************************
 *              
 *  FUNCTION:   GetMainMenu
 *              
 *  RETURNS:    cOsdBasicDisplay *
 *              
 *  PARAMETERS: 
 *		Input  :
 *          -
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
cOsdBasicDisplay *cMLDonkeyAction::GetMainMenu()
{
	COsdMainMenu.Clear();
    COsdMainMenu.SetTitle(string("MLDonkey GUI"));

    COsdMainMenu.AddColumn(string(""),0,750);

	COsdMainMenu.AddRaw(string("Downloads"),ACTION_VIEW_DOWNLOADING);
	COsdMainMenu.AddRaw(string("Uploads"));
	COsdMainMenu.AddRaw(string("Search"));
	COsdMainMenu.AddRaw(string("Servers"),ACTION_VIEW_SERVER);
	COsdMainMenu.AddRaw(string("Statistic"),ACTION_VIEW_STATISTIC);
//	COsdMainMenu.AddRaw(string("Downloaded"));

	COsdMainMenu.SetBackAction( ACTION_EXIT );

	return(&COsdMainMenu);
}

/*******************************************************************
 *              
 *  FUNCTION:   GetMenuOfDownloadingFiles
 *              
 *  RETURNS:    cOsdBasicDisplay *
 *              
 *  PARAMETERS: 
 *		Input  :
 *          -
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
cOsdBasicDisplay *cMLDonkeyAction::GetMenuOfDownloadingFiles()
{
    int i, curaw, s;
	char txt[40];

	COsdCurrentMenu.Clear();

    COsdCurrentMenu.SetTitle(string("Transfert/Downloading"));

    COsdCurrentMenu.AddColumn(string("Name"),0,370);
    COsdCurrentMenu.AddColumn(string("Size"),0,70);
    COsdCurrentMenu.AddColumn(string("Dld%"),0,60);
    COsdCurrentMenu.AddColumn(string("Rate"),0,60);

//    COsdCurrentMenu.AddColumn(string("Dled"),0,60);
//    COsdCurrentMenu.AddColumn(string("%"),0,60);
    
    /*    COsdCurrentMenu.AddColumn(string("Src"),0,50);
    COsdCurrentMenu.AddColumn(string("Speed"),0,50);
    COsdCurrentMenu.AddColumn(string("ETA"),0,50);
	COsdCurrentMenu.AddColumn(string("Status"),0,50);
	COsdCurrentMenu.AddColumn(string("Network"),0,50);
    COsdCurrentMenu.AddRaw(string("File01"),ACTION_VIEW_DETAILOFDOWNLOADINGFILES,0);
    COsdCurrentMenu.AddRaw(string("File02"));
    COsdCurrentMenu.AddRaw(string("File03"));
    COsdCurrentMenu.AddRaw(string("File04"));*/

	LogMsg(LOGLEVEL_MAIN,"MlDkGUI : cMLDonkeyStatus::GetMenuOfDownloadingFiles m_iNbDownloadingFiles=%d\n",m_iNbDownloadingFiles);


    for(i=0;i<m_iNbDownloadingFiles;i++)
	{
        curaw = COsdCurrentMenu.AddRaw(m_lstDownloadingFiles[i].FilePreferredName,ACTION_VIEW_DETAILOFDOWNLOADINGFILES,(int*)i);

		s = m_lstDownloadingFiles[i].FileSize;
		sprintf(txt,"%d",s);
		if(s > 1024)
		{
			sprintf(txt,"%dk",s/1024);
			if(s > 1024*1024)
			{
				sprintf(txt,"%dM",s/1024/1024);
			}	
		}
		
		COsdCurrentMenu.InsertItem(curaw,1, string(txt) );
/*
		s = m_lstDownloadingFiles[i].FileDownloaded;
		sprintf(txt,"%d",s);
		if(s > 1024)
		{
			sprintf(txt,"%dk",s/1024);
			if(s > 1024*1024)
			{
				sprintf(txt,"%dM",s/1024/1024);
			}	
		}
		COsdCurrentMenu.InsertItem(curaw,2, string(txt) );
*/
		sprintf(txt,"%3.1f",(100.f * m_lstDownloadingFiles[i].FileDownloaded) / m_lstDownloadingFiles[i].FileSize);
		COsdCurrentMenu.InsertItem(curaw,2, string(txt) );

		COsdCurrentMenu.InsertItem(curaw,3, m_lstDownloadingFiles[i].DownloadRate );

	}


	COsdCurrentMenu.SetBackAction( ACTION_VIEW_MAINMENU );
	
	return(&COsdCurrentMenu);
}


/*******************************************************************
 *              
 *  FUNCTION:   GetMenuOfDetailOfDownloadingFiles
 *              
 *  RETURNS:    cOsdBasicDisplay *
 *              
 *  PARAMETERS: 
 *		Input  :
 *          int *param
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
cOsdBasicDisplay *cMLDonkeyAction::GetMenuOfDetailOfDownloadingFiles(int *param)
{
	int sy,py,px;
	int n = (int)param;
	cFileInfo	*pFileInfo;
	char txt[256];

	pFileInfo = &(m_lstDownloadingFiles[n]);

	COsdDetailFiles.Clear();
    COsdDetailFiles.SetTitle(string("Transfert/Downloading/Details"));

	px = 10; py = 70; sy = 30;
	COsdDetailFiles.AddText(px,py,pFileInfo->FilePreferredName);	py += sy;

	sprintf(txt,"FileID : %lx",pFileInfo->FileIdentifier);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;

//	strcpy(txt,"Size : ");
//	Size2Str(pFileInfo->FileDownloaded,txttmp);	strcat(txt,txttmp);	strcat(txt," / ");
//	Size2Str(pFileInfo->FileSize,txttmp);	strcat(txt,txttmp);
//	COsdDetailFiles.AddText(px,py,txt);	py += sy;

	sprintf(txt,"DLed/Size : %ld bytes of %ld bytes",pFileInfo->FileDownloaded,pFileInfo->FileSize);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;

	sprintf(txt,"Speed : %s",pFileInfo->DownloadRate.c_str());
	COsdDetailFiles.AddText(px,py,txt);	py += sy;

	sprintf(txt,"State : %d",pFileInfo->FileState);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;

	sprintf(txt,"Priority : %ld",pFileInfo->FilePriority);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;
	sprintf(txt,"Sources : %ld",pFileInfo->NumberOfSources);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;
//	sprintf(txt,"Chunks : %s",pFileInfo->Chunks);
//	COsdDetailFiles.AddText(px,py,txt);	py += sy;

//	sprintf(txt,"Md4 : %s",pFileInfo->FileMd4);
//	COsdDetailFiles.AddText(px,py,txt);	py += sy;

	COsdDetailFiles.SetBackAction( ACTION_VIEW_DOWNLOADING );

	return(&COsdDetailFiles);
}


/*******************************************************************
 *              
 *  FUNCTION:   GetMenuOfViewServers
 *              
 *  RETURNS:    cOsdBasicDisplay *
 *              
 *  PARAMETERS: 
 *		Input  :
 *          -
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
cOsdBasicDisplay *cMLDonkeyAction::GetMenuOfViewServers()
{
	int curaw;
	cServerInfo *ptr;
	char txt[256];

	COsdCurrentMenu.Clear();

    COsdCurrentMenu.SetTitle(string("View Connected Servers"));

    COsdCurrentMenu.AddColumn(string("Server Name"),0,370);
    COsdCurrentMenu.AddColumn(string("Net"),0,50);
    COsdCurrentMenu.AddColumn(string("Users"),0,70);
    COsdCurrentMenu.AddColumn(string("Files"),0,70);
//	COsdCurrentMenu.AddColumn(string("Sta"),0,50);

	m_lstServersInfo.RestartGetNextConnectedServer();
	
	do
	{
		ptr = m_lstServersInfo.GetNextConnectedServer();
		if(ptr != NULL)
		{
			if( ptr->ServerName == "<unknown>")
			{
				if(ptr->SrvAdrType == 0)
					sprintf(txt,"%lx",ptr->SrvAdrIP);
				else
					sprintf(txt,"%s",ptr->SrvAdrName.c_str());

				curaw = COsdCurrentMenu.AddRaw(txt);
			}
			else
				curaw = COsdCurrentMenu.AddRaw(ptr->ServerName);

			switch(ptr->ServerNetworkID)
			{
				case 3:
					sprintf(txt,"Gta");		// gnutella
					break;
				case 6:
					sprintf(txt,"Dky");		// Donkey
					break;
				default:
					sprintf(txt,"%ld",ptr->ServerNetworkID);
					break;
			}
			
			COsdCurrentMenu.InsertItem(curaw,1,txt);

			sprintf(txt,"%ld",ptr->NumberOfUsers);
			COsdCurrentMenu.InsertItem(curaw,2,txt);

			sprintf(txt,"%ld",ptr->NumberOfFilesShared);
			COsdCurrentMenu.InsertItem(curaw,3,txt);
/*
			switch(ptr->ConnectionState)
			{
				case 1:
					sprintf(txt,"Cg");		// 
					break;

				case 4:
					sprintf(txt,"Cd");		// 
					break;
				default:
					sprintf(txt,"%d",ptr->ConnectionState);
					break;
			}
			COsdCurrentMenu.InsertItem(curaw,4,txt);
*/
		}
	}
	while(ptr != NULL);

	COsdCurrentMenu.SetBackAction( ACTION_VIEW_MAINMENU );
	return(&COsdCurrentMenu);
}

/*******************************************************************
 *              
 *  FUNCTION:   GetMenuOfStatistic
 *              
 *  RETURNS:    cOsdBasicDisplay *
 *              
 *  PARAMETERS: 
 *		Input  :
 *          -
 *
 *		Output :
 *              
 *  COMMENTS:   -
 *              
 *******************************************************************/
cOsdBasicDisplay *cMLDonkeyAction::GetMenuOfStatistic()
{
	int sy,py,px;
	char txt[256];

	COsdDetailFiles.Clear();

    COsdDetailFiles.SetTitle(string("Statistic"));

	px = 10; py = 70; sy = 30;
	
	sprintf(txt,"Total Downloaded : %lld bytes / Total Uploaded : %lld bytes",TotalUploaded,TotalDownloaded);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;

	sprintf(txt,"Tcp Download Rate : %ld / Tcp Upload Rate : %ld ",TcpDownloadRate,TcpUploadRate);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;
	
	sprintf(txt,"Udp Download Rate : %ld / Udp Upload Rate : %ld ",UdpDownloadRate,UdpUploadRate);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;

	sprintf(txt,"Number Of Current Downloads : %ld",NumberOfCurrentDownloads);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;

	sprintf(txt,"Number Of Downloads Finished : %ld",NumberOfDownloadsFinished);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;

	sprintf(txt,"Number Of Shared Files : %ld",NumberOfSharedFiles);
	COsdDetailFiles.AddText(px,py,txt);	py += sy;


	COsdDetailFiles.SetBackAction( ACTION_VIEW_MAINMENU );
	return(&COsdDetailFiles);
}
