//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.

#pragma once
#include "types.h"
#include "opcodes.h"
#include "updownclient.h"
#include "preferences.h"

class CUploadQueue{
public:
	CUploadQueue(CPreferences* in_prefs);
	~CUploadQueue();
	void	Process();
	void	AddClientToQueue(CUpDownClient* client,bool bIgnoreTimelimit = false);
	bool	RemoveFromUploadQueue(CUpDownClient* client,bool updatewindow = true);
	bool	RemoveFromWaitingQueue(CUpDownClient* client,bool updatewindow = true);
	bool	IsOnUploadQueue(CUpDownClient* client)	{return GetWaitingClient(client);}
	bool	IsDownloading(CUpDownClient* client)	{return GetDownloadingClient(client);}
	uint32	GetDatarate()							{return datarate;}
	bool	CheckForTimeOver(CUpDownClient* client);
	int		GetWaitingUserCount()					{return waitinglist.GetCount();}
	int		GetUploadQueueLength()					{return uploadinglist.GetCount();}
	void	UpdateBanCount();
	
	uint32	GetPrefetchStatus(); //Rectencle Connection-Prefetching
	void	SetPrefetchStatus(); //Rectencle Connection-Prefetching
	bool	GetIsOneClientAtZeroUp()				{return IsOneClientAtZeroUpload;} //Rectencle

	CUpDownClient*	GetWaitingClientByIP(uint32 dwIP);
	CUpDownClient*	GetNextClient(CUpDownClient* update);

	
	void	DeleteAll();
	uint16	GetWaitingPosition(CUpDownClient* client);
	void	SetBanCount(uint32 in_count)			{bannedcount = in_count;}
	uint32	GetBanCount()							{return bannedcount;}
	uint32	GetSuccessfullUpCount()					{return successfullupcount;}
	uint32	GetFailedUpCount()						{return failedupcount;}
	uint32	GetAverageUpTime();
	void	FindSourcesForFileById(CTypedPtrList<CPtrList, CUpDownClient*>* srclist, uchar* filehash);
	void	AddUpDataRateMSOverhead(uint32 data, uint32 header)	{ m_nUpDataRateMSOverhead = m_nUpDataRateMSOverhead+data+header;}
	uint32	GetDatarateOverhead()		{return m_nUpDatarateOverhead;}
	void	CompDatarateOverhead();
protected:
	void	RemoveFromWaitingQueue(POSITION pos, bool updatewindow);
	POSITION	GetWaitingClient(CUpDownClient* client);
	POSITION	GetWaitingClientByID(CUpDownClient* client);
	POSITION	GetDownloadingClient(CUpDownClient* client);
	bool		AcceptNewClient();
	void		AddUpNextClient(CUpDownClient* directadd = 0);
private:
	CList<int,int> avarage_dr_list;
	CTypedPtrList<CPtrList, CUpDownClient*> waitinglist;
	CTypedPtrList<CPtrList, CUpDownClient*> uploadinglist;
	uint32	datarate;   //datarate 
	uint32	dataratems;	// datarate last 100ms
	uint32	datarateave; //datarage average (since progstart) *unused*
	sint32	estadatarate; // esta. max datarate	

	bool	bDataRateMaxed; // Added by Tarod [LoneStar]
	uint32	uLastUploadStart; // Added by Tarod [LoneStar]

	CPreferences* app_prefs;
	UINT_PTR h_timer;
	uint32	bannedcount;
	uint32	successfullupcount;
	uint32	failedupcount;
	uint32	totaluploadtime;

	uint32		m_nUpDatarateOverhead;
	uint32		m_nUpDataRateMSOverhead;
	CList<int,int>									 m_AvarageUDRO_list;

	bool	IsOneClientAtZeroUpload; //Rectencle Connection-Prefetching refinement
	uint32	PrefetchStatus; //Rectencle Connection-Prefetching
	DWORD	lasttimechecked; // last time that we checked to remove a client from upload queue (Added by Rectencle)
	bool	LeavePrefetchSection; //Rectencle


};
