

#if !defined CURLINET_H
#define CURLINET_H


#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

//-----------------------------------------------------------
// 4/12/2021 - VS2010
// Using curl-7.76.0 with openssl 1_1_1k
// X32 dll release build
// Uses:
//  Curl_Local\bin			libcurl.dll
//  Curl_Local\dependencies	libcrypto-1_1.dll
//	Curl_Local\dependencies	libssl-1_1.dll
// Copy above 3 to debug/release folders.
//-----------------------------------------------------------
// Curl header
//#include "Curl_Local\\include\\curl\\curl.h"
// Curl lib.
//#pragma comment(lib, "Curl_Local\\lib\\libcurl.lib") // release


//-----------------------------------------------------------
// 7/18/2021 - VS2010
// Using curl-7.76.0 with wolfssl 4.8.0
// X32 dll release build
// Uses:
//  Curl_Local\bin			libcurl.dll
//  Curl_Local\dependencies	wolfssl.dll
//-----------------------------------------------------------
// Curl header
#include "Curl_Local\\include\\curl\\curl.h"
// Curl lib.
#pragma comment(lib, "Curl_Local\\lib\\libcurl.lib") // release



////////////////////////////////////////////////////
class CCurlinet
{
public:
	CCurlinet();
	~CCurlinet();

	// Download an internet URL to file.
	BOOL GetInternetData(CString strUrl, CString & strErr);
	BOOL GetInternetData(CString strUrl, CString filePath, CString & strErr);
	BOOL GetInternetDataUnicode(CString strUrl, CString filePath, 
								CString & strErr, UINT maxBytes = 0);

	// Convert CString,
	char * GetAsciiStringFromCString(CString str);

	// Static callback used by Curl.
	static size_t CurlCallback(char *data, size_t size, size_t nmemb, void * f);

	// Process callbacks from Curl.
	size_t CurlResults(char *data, size_t size, size_t nmemb);


	// Curl in a worker thread:
	//  Provide a progress callback.
	//  Post messages to thread owner and a CDialog.
	//  Use a CEvent to teminate download
	//
	// Get internet data in worker thread.
	BOOL GetInternetDataUnicodeThread(CString strUrl, CString tempFilePath, CString & strErr,
									  HWND hwndMain, HWND hwndDlg, CEvent * pEvent);
	// Thread message to dialog.
	void ShowDialogStatus(CString strStatus);

	// Progress ctrl message to dlg.
	void SetProgressPos(int pos);


	// Static Progress callback used by Curl.
	static size_t CurlProgressCallback(void * f,   
									   curl_off_t dltotal,
									   curl_off_t dlnow,
									   curl_off_t ultotal,
									   curl_off_t ulnow);

	// Process Progress callbacks from Curl.
	size_t CurlProgress(curl_off_t dltotal,
						curl_off_t dlnow,
						curl_off_t ultotal,
						curl_off_t ulnow);

	// Thread members.
	HWND m_hwndMain;
	HWND m_hwndDlg;
    CEvent * m_pEvent;
//	BOOL m_inThread;
	int m_mbDownloaded;

private:
	CFile m_file;

	// 9/25/2021 - test fix 
	// Test fix runaway .m3u download
	UINT m_nBytesDownloaded;
	UINT m_nMaxDownloadBytes;

};

#endif // CURLINET_H