//this file is part of eMule
//Copyright (C)2002-2006 Merkur ( strEmail.Format("%s@%s", "devteam", "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.
#pragma once
#include "BarShader.h"
#include "StatisticFile.h"
#include "AbstractFile.h"
#include "Sion/FilePreferences.h" // MOD: FCFG - [FileConfiguration] <-- Loulach --
#include <list>

class CxImage;
class CUpDownClient;
class CSafeMemFile; // MOD: IPS - [InteligentPartSharing] <-- Loulach --
class Packet;
class CFileDataIO;
class CAICHHashTree;
class CAICHHashSet;
class CCollection;

typedef CTypedPtrList<CPtrList, CUpDownClient*> CUpDownClientPtrList;

#ifdef OPTIMISATION //--Uploadslist--Colours-and fix-Loulach-->>
#define POWERSHARED_BACK_NORMAL				RGB(255, 255, 170) //jaune
#define POWERSHARED_BACK_SELECTED			RGB(255, 255, 80)
#define POWERSHARED_BACK_SELECTED_UF		RGB(255, 255, 120)
/*
#define POWERSHARED_BACK_NORMAL				RGB(255, 210, 210) // rouge
#define POWERSHARED_BACK_SELECTED			RGB(255, 102, 102)
#define POWERSHARED_BACK_SELECTED_UF		RGB(255, 51, 51)
*/
#define SMALLPUSHED_BACK_NORMAL				RGB(190, 240, 255) // bleue
#define SMALLPUSHED_BACK_SELECTED			RGB(000, 200, 255)
#define SMALLPUSHED_BACK_SELECTED_UF		RGB(130, 230, 255)
#endif //--Uploadslist--Colours-and fix-Loulach--<<
// <CB Mod : SmallFilePush : Defines>
#define SMALLFILE_THRESHOLD (PARTSIZE)
#define SMALLFILE_MAX_BOOST 1000 // For very small files
// </CB Mod : SmallFilePush : Defines>

// MOD: IPS - [InteligentPartSharing] -- Loulach -->
struct TPartOL{
	UINT	Part;
	double	OL;
};
// MOD: IPS END <-- Loulach --

class CKnownFile : public CAbstractFile
{
	DECLARE_DYNAMIC(CKnownFile)

public:
	CKnownFile();
	virtual ~CKnownFile();

	virtual void SetFileName(LPCTSTR pszFileName, bool bReplaceInvalidFileSystemChars = false); // 'bReplaceInvalidFileSystemChars' is set to 'false' for backward compatibility!

	const CString& GetPath() const { return m_strDirectory; }
	void SetPath(LPCTSTR path);

	const CString& GetFilePath() const { return m_strFilePath; }
	void SetFilePath(LPCTSTR pszFilePath);

	bool	CreateFromFile(LPCTSTR directory, LPCTSTR filename, LPVOID pvProgressParam); // create date, hashset and tags from a file
	bool	LoadFromFile(CFileDataIO* file);	//load date, hashset and tags from a .met file
	bool	WriteToFile(CFileDataIO* file);
	bool	CreateAICHHashSetOnly();

	// last file modification time in (DST corrected, if NTFS) real UTC format
	// NOTE: this value can *not* be compared with NT's version of the UTC time
	CTime	GetUtcCFileDate() const { return CTime(m_tUtcLastModified); }
	uint32	GetUtcFileDate() const { return m_tUtcLastModified; }

	virtual void SetFileSize(EMFileSize nFileSize);

	// local available part hashs
	UINT	GetHashCount() const { return hashlist.GetCount(); }
	uchar*	GetPartHash(UINT part) const;
	const CArray<uchar*, uchar*>& GetHashset() const { return hashlist; }
	bool	SetHashset(const CArray<uchar*, uchar*>& aHashset);

	// nr. of part hashs according the file size wrt ED2K protocol
	uint16	GetED2KPartHashCount() const { return m_iED2KPartHashCount; }

	// nr. of 9MB parts (file data)
	__inline uint16 GetPartCount() const { return m_iPartCount; }

	// nr. of 9MB parts according the file size wrt ED2K protocol (OP_FILESTATUS)
	__inline uint16 GetED2KPartCount() const { return m_iED2KPartCount; }

	// size of part, all are 9 MB but the last part may be smaller
	uint32	GetPartSize(UINT part) const; // MOD: MOD [NeoPartTraffic]<-- Loulach --

	// file upload priority
	uint8	GetUpPriority(void) const { return m_iUpPriority; }
	void	SetUpPriority(uint8 iNewUpPriority, bool bSave = true);
	bool	IsAutoUpPriority(void) const { return m_bAutoUpPriority; }
	void	SetAutoUpPriority(bool NewAutoUpPriority) { m_bAutoUpPriority = NewAutoUpPriority; }
	void	UpdateAutoUpPriority();

	// This has lost it's meaning here.. This is the total clients we know that want this file..
	// Right now this number is used for auto priorities..
	// This may be replaced with total complete source known in the network..
	uint32	GetQueuedCount() { return m_ClientUploadList.GetCount();}

	// MOD: IPS - [InteligentPartSharing] -- Loulach -->
	uint8	GetPartState(UINT part) const;// [NeoPartTraffic] // -- [InteligentPartSharing] -- Loulach --
	bool	WritePartSelection(CSafeMemFile* file, CUpDownClient* client = NULL);
	uint8	GetIPSPartStatus(UINT part) const;
	void	SetIPSPartStatus(UINT part, uint8 state) { m_IPSPartStatus[part] = state; }
	void	ResetIPSList();
	void	GetHideMap(CUpDownClient* client, CMap<UINT, UINT, BOOL, BOOL> &GetHideMap);
	void	ReCalculateIPS();
	void	CalculateIPS();
	double	GetPartShared(uint16 part);
	// MOD: IPS END <-- Loulach --

	// MOD: SSP - [ShowSharePermissions] -- Loulach -->
	uint8	GetPermissions(void) const	{ return m_iPermissions; };
	void	SetPermissions(uint8 iNewPermissions) {m_iPermissions = iNewPermissions;};
	// MOD: SSP END <-- Loulach --

	bool	LoadHashsetFromFile(CFileDataIO* file, bool checkhash);

	void	AddUploadingClient(CUpDownClient* client);
	void	RemoveUploadingClient(CUpDownClient* client);
	virtual void	UpdatePartsInfo(bool bFlag = false); // MOD: IPS - [InteligentPartSharing] <-- Loulach --
//	virtual void	UpdatePartsInfo();
	virtual	void	DrawShareStatusBar(CDC* dc, LPCRECT rect, bool onlygreyrect, bool bFlat) const;
	virtual	void	DrawShareStatusBarNew(CDC* dc, LPCRECT rect, bool onlygreyrect, bool bFlat) const; // MOD: MOD [NeoPartTraffic]<-- Loulach --

	virtual uint16	GetPartAvailibility(uint16 i) const			{return (m_AvailPartFrequency.IsEmpty()			? 0 : m_AvailPartFrequency[i]);			} // MOD: NPT - [NeoPartTraffic]  <-- Loulach --

	// comment
	void	SetFileComment(LPCTSTR pszComment);

	void	SetFileRating(UINT uRating);

	bool	GetPublishedED2K() const { return m_PublishedED2K; }
	void	SetPublishedED2K(bool val);

	uint32	GetKadFileSearchID() const { return kadFileSearchID; }
	void	SetKadFileSearchID(uint32 id) { kadFileSearchID = id; } //Don't use this unless you know what your are DOING!! (Hopefully I do.. :)

	const Kademlia::WordList& GetKadKeywords() const { return wordlist; }

	uint32	GetLastPublishTimeKadSrc() const { return m_lastPublishTimeKadSrc; }
	void	SetLastPublishTimeKadSrc(uint32 time, uint32 buddyip) { m_lastPublishTimeKadSrc = time; m_lastBuddyIP = buddyip;}
	uint32	GetLastPublishBuddy() const { return m_lastBuddyIP; }
	void	SetLastPublishTimeKadNotes(uint32 time) {m_lastPublishTimeKadNotes = time;}
	uint32	GetLastPublishTimeKadNotes() const { return m_lastPublishTimeKadNotes; }

	bool	PublishSrc();
	bool	PublishNotes();

	// file sharing
	virtual Packet* CreateSrcInfoPacket(const CUpDownClient* forClient) const;
	UINT	GetMetaDataVer() const { return m_uMetaDataVer; }
	void	UpdateMetaDataTags();
	void	RemoveMetaDataTags();

	// preview
	bool	IsMovie() const;
	virtual bool GrabImage(uint8 nFramesToGrab, double dStartTime, bool bReduceColor, uint16 nMaxWidth, void* pSender);
	virtual void GrabbingFinished(CxImage** imgResults, uint8 nFramesGrabbed, void* pSender);

	// aich
	CAICHHashSet*	GetAICHHashset() const							{return m_pAICHHashSet;}
	void			SetAICHHashset(CAICHHashSet* val)				{m_pAICHHashSet = val;}
	// last file modification time in (DST corrected, if NTFS) real UTC format
	// NOTE: this value can *not* be compared with NT's version of the UTC time
	uint32	m_tUtcLastModified;

	CStatisticFile statistic;
	time_t m_nCompleteSourcesTime;
	uint16 m_nCompleteSourcesCount;
	uint16 m_nCompleteSourcesCountLo;
	uint16 m_nCompleteSourcesCountHi;
	CUpDownClientPtrList m_ClientUploadList;
	CArray<uint16, uint16> m_AvailPartFrequency;
	CCollection* m_pCollection;


	// MOD: XC - [ExtendedComments] -- Loulach -->
	bool	LoadComments(CFileDataIO* file);
	bool	SaveComments(CFileDataIO* file);
	bool	HasComments()	{ return !m_CommentList.IsEmpty(); }
	// MOD: XC END <-- Loulach --

	// MOD: MQ - [MultiQueue] -- Loulach -->
	uint32	GetStartUploadTime() const {return m_startUploadTime;}
	void	UpdateStartUploadTime() {m_startUploadTime = GetTickCount();}
	// MOD: MQ END <-- Loulach --

	CKnownPreferences KnownPrefs; // MOD: FCFG - [FileConfiguration] <-- Loulach --

#ifdef _DEBUG
	// Diagnostic Support
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

    void    SetPowerShared(bool newValue);

    bool    GetPowerShared() const { return m_powershared; };

	// <CB Mod : SmallFilePush : Show>
	bool IsSmallFile() const { return (m_nFileSize < SMALLFILE_THRESHOLD); };
	bool IsSFP() const { return m_bSFPEnabled; };
	void SetSFP(bool en) { m_bSFPEnabled = en; };
	// </CB Mod : SmallFilePush : Show>

protected:
	//preview
	bool	GrabImage(CString strFileName, uint8 nFramesToGrab, double dStartTime, bool bReduceColor, uint16 nMaxWidth, void* pSender);
	bool	LoadTagsFromFile(CFileDataIO* file);
	bool	LoadDateFromFile(CFileDataIO* file);
	void	CreateHash(CFile* pFile, uint64 uSize, uchar* pucHash, CAICHHashTree* pShaHashOut = NULL) const;
	bool	CreateHash(FILE* fp, uint64 uSize, uchar* pucHash, CAICHHashTree* pShaHashOut = NULL) const;
	bool	CreateHash(const uchar* pucData, uint32 uSize, uchar* pucHash, CAICHHashTree* pShaHashOut = NULL) const;
	virtual void	UpdateFileRatingCommentAvail();

	CArray<uchar*, uchar*>	hashlist;
	CString					m_strDirectory;
	CString					m_strFilePath;
	CAICHHashSet*			m_pAICHHashSet;

private:
	static CBarShader s_ShareStatusBar;
	uint16	m_iPartCount;
	uint16	m_iED2KPartCount;
	uint16	m_iED2KPartHashCount;
	uint8	m_iUpPriority;
	uint8	m_iPermissions; // MOD: SSP - [ShowSharePermissions] <-- Loulach --
	bool	m_bAutoUpPriority;
	bool	m_PublishedED2K;
	uint32	kadFileSearchID;
	uint32	m_lastPublishTimeKadSrc;
	uint32	m_lastPublishTimeKadNotes;
	uint32	m_lastBuddyIP;
	Kademlia::WordList wordlist;
	UINT	m_uMetaDataVer;

    bool    m_powershared;
	uint32  m_startUploadTime; // MOD: MQ - [MultiQueue] <-- Loulach --

	// MOD: IPS - [InteligentPartSharing] -- Loulach -->
	DWORD	m_uLastIPSCalcTime;
	uint16	m_uIPSCalcCount;
	CMap<UINT, UINT, uint8, uint8> m_IPSPartStatus;
	CArray<TPartOL,TPartOL> *m_IPSPartsInfo;
	// MOD: IPS END <-- Loulach --

	// <CB Mod : SmallFilePush : Show>
public:
	bool  m_bSFPEnabled;
	// <CB Mod : SmallFilePush : Show>
};
// MOD: SSP - [ShowSharePermissions] -- Loulach -->
#define PERM_DEFAULT	255 //-1
#define PERM_ALL		0
#define PERM_FRIENDS	1
#define PERM_NONE		2
#define PERM_COMMUNITY	3
// MOD: SSP END <-- Loulach --
