#include "CDownLoadFile.h"

double DownloadProgress::Currentpercentage = 0;
double DownloadProgress::pro_value = 0;
double DownloadProgress::pro_Sum = 0;
bool CDownLoadFile::GetInstallPackage(std::wstring SaveFolder)
{
	FolderCore::GetOrCreateFolderPath(PathCore::smPath_Install.c_str());
	DownloadProgress progress;
	IBindStatusCallback* callback = (IBindStatusCallback*)&progress;

	SaveFolder = FolderCore::GetOrCreateFolderPath(SaveFolder) + L"/" + Product::smName_InstallPackage;
	//清除缓存资源
	DeleteUrlCacheEntry(Product::URI_InstallPackge.c_str());
	//下载安装包资源
	HRESULT Hfile = URLDownloadToFile(NULL, Product::URI_InstallPackge.c_str(), SaveFolder.c_str(), 0, static_cast<IBindStatusCallback*>(&progress));
	if (Hfile == S_OK)
	{
		if (DownloadProgress::Currentpercentage > 99.0)
		{
			while (DownloadProgress::pro_value < DownloadProgress::pro_Sum -2)
			{
				int i = 0;
			}

			return true;
		}
	}
	else
	{
		
	}
	
	return false;
}


#pragma region 检查网络状态
bool NetWorkState::IsNetWorking()
{
	DWORD   flags;
	return InternetGetConnectedState(&flags, 0);
}

bool NetWorkState::IsProxyEnable()
{
	long lRet;
	HKEY hKey;
	TCHAR szBuffer[255] = { 0 };
	TCHAR tchData[64];
	DWORD dwSize = 255;
	lRet = RegOpenKeyEx(HKEY_CURRENT_USER,_T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
		0,KEY_READ | KEY_WOW64_64KEY,&hKey);

	if (lRet == ERROR_SUCCESS)
	{
		lRet = RegQueryValueEx(hKey,_T("ProxyEnable"),
			NULL,NULL,(LPBYTE)szBuffer,&dwSize);
		if (lRet == ERROR_SUCCESS)
		{
			std::wstring poxyEnable(szBuffer);
			if (poxyEnable == L"")
			{
				return false;
			}
		}
	
	}
	RegCloseKey(hKey);
	return true;
}

bool NetWorkState::IsProxyNetError()
{
	wstring SaveFolder = FolderCore::GetOrCreateFolderPath(PathCore::smPath_Temp) + L"/" + L"test.xml";

	bool IsProxyChecked = false;
	long lRet;
	HKEY hKey;
	TCHAR szBuffer[255] = { 0 };
	TCHAR tchData[64];
	DWORD dwSize = 255;
	lRet = RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"),
		0, KEY_READ | KEY_WOW64_64KEY, &hKey);

	if (lRet == ERROR_SUCCESS)
	{
		lRet = RegQueryValueEx(hKey, _T("ProxyEnable"),
			NULL, NULL, (LPBYTE)szBuffer, &dwSize);
		if (lRet == ERROR_SUCCESS)
		{
			std::wstring poxyEnable(szBuffer);
			if (poxyEnable == L"")
			{
				IsProxyChecked = false;
			}
			else
			{
				IsProxyChecked = true;
			}
		}

	}
	RegCloseKey(hKey);

	//如果当前设备有勾选LAN代理服务器，则认为代理网址和端口不对，代理服务器连接失败
	return IsProxyChecked;
}
#pragma endregion




