#pragma once
#ifndef UNICODE
#define UNICODE
#endif

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#include <http.h>

#include <string>
#include <memory>

#include "Win32Buffer.h"

class HttpRequest;
class Win32TempFile;

class Win32WebRequest
{
public:
	Win32WebRequest();

	HttpRequest getRequest() const;

	PHTTP_REQUEST getHandle() const;

	unsigned getBufferSize() const;

	Win32Buffer* getEntityBuffer() const;

	unsigned getEntityBufferSize() const;

	unsigned getEntityTotalSize() const;

	void flushEntityBuffer(unsigned numBytes);

	Win32TempFile* getTempFile() const;

	bool isValid() const;

	void resizeBuffer(unsigned size);

	void zeroBufferMemory();

	bool startReceiveEntity();

private:
	std::string getUrlFromRequest() const;

	static const unsigned ENTITY_BUFFER_SIZE{ 2048 };
	std::unique_ptr<Win32Buffer> mEntityBuffer;
	std::unique_ptr<Win32TempFile> mTempFile;
	unsigned mEntityTotalSize{ 0 };

	static const unsigned DEFAULT_BUFFER_SIZE{ sizeof(HTTP_REQUEST) + 2048 };
	Win32Buffer mBuffer;
	bool mIsValid{ false };

	unsigned mWorkingBufferSize{ DEFAULT_BUFFER_SIZE };
	PHTTP_REQUEST mHandle{ nullptr };
};

