#pragma once
#include "Gos.h"

namespace Gos2 {

class File : public GosFile, GosObj
{
protected:
	static GosClassInfo *pClassInfo;

private:
	_TCHAR				m_FileName[MAX_PATH];
	enum { Normal, Background, MemoryMapped } m_AccessMode;
	HANDLE				m_hFile;
	HANDLE				m_hFileMapping;
	DWORD				m_nOffset;
	DWORD				m_nMaxSize;
	bool				m_fPreAllocated;

	DWORD				m_nSizeBytes;
	bool				m_fReady;
	void *				m_pData;
	File *				m_pNextBackground;

	static CRITICAL_SECTION CritSectBackgroundFile;

	static HANDLE		hBackgroundThread;
	static File *		pFirstBackground;
	static File *		pLastBackground;
	static HANDLE		hFileEvent;
	static bool			killFileThread;

	void				ReadData();
	friend DWORD WINAPI FileBackgroundThread( LPVOID ThreadParam );
	static DWORD		BackgroundThread();



public:

	static GosFile *	Read( const TCHAR *pFileName, DWORD Offset=0, DWORD MaxSize=0xFFFFFFFF, void *Buffer=NULL );
	static GosFile *	Open( const TCHAR *pFileName );
	virtual void		Write( void *Buffer, DWORD Size );
	static GosFile *	BackgroundRead( const TCHAR *pFileName, DWORD Offset=0, DWORD MaxSize=0xFFFFFFFF, void *Buffer=NULL );
	static GosFile *	MemoryMap( const TCHAR *pFileName );
	static bool			Exists( const TCHAR *pFileName );

	static void			Shutdown();
	static void			GetFullPathName( TCHAR* FullPath, const TCHAR *pFileName );
	virtual				~File();
	virtual DWORD		Size() { return m_nSizeBytes; }
	virtual bool		Ready() { return m_fReady; }
	virtual void *		Data() { return m_pData; }
						File();

};

} using namespace Gos2;
