#include "ComparVersion.h"

VertionResultType ComparVersion::GetProductInfo(wstring productName,wstring productUrl)
{
	DeviceProduct deviceProduct = DeviceProduct(productName);
	OnlineProduct onlineProduct = OnlineProduct();
	onlineProduct.GetInstallPageVersion(productUrl);
	int re = GetCompareNETFramework(deviceProduct.GetNETTargetVersion(), deviceProduct.GetNETVersion());
	if (re == 1)
	{
		NetFramework = InstallNetFrameworkType::V46;
	}
	else if (re == 2)
	{
		NetFramework = InstallNetFrameworkType::V40_46;
	}
	int vc = GetCompareVCPlusPlus(deviceProduct.GetVsCPulsPulsVersion());
	if (vc == 1) {
		VcPlusPlus = InstallVCPlusPlusType::V14_3;
	}
	return GetCompareVersion(deviceProduct.GetVersion(), onlineProduct.GetVersionW());
}
VertionResultType ComparVersion::GetCompareVersion(wstring deviceVersion,wstring onlineVersion)
{
	std::map<wstring, wstring> map{ {L"DeviceVersion", deviceVersion}, { L"OnlineVersion",onlineVersion }};
	LogIni::GetLogToIniMaps(map);

	VertionResultType state = VertionResultType::None;
	if (deviceVersion.find(onlineVersion) != -1)
	{
		state = VertionResultType::Same;
	}
	else
	{
		if (deviceVersion == L"")
		{
			state = VertionResultType::None;
		}
		else
		{
			int n = deviceVersion.compare(onlineVersion);
			if (n < 0)
			{
				state = VertionResultType::Low;
			}
			else
			{
				state = VertionResultType::Hight;
			}
		}
	}

	return state;
}

//值：0为已存在4.0和4.6以上版本；1为只存在4.0；2为4.0和4.6以上都不存在
int ComparVersion::GetCompareNETFramework(wstring minVersion, wstring maxVersion)
{
	if (minVersion == L"" || maxVersion == L"")
		return 0;

	wstring minVersionStr = minVersion.substr(0, 3);
	wstring maxVersionStr = maxVersion.substr(0, 3);

	int n = minVersionStr.compare(L"4.0");
	

	int n2 = maxVersion.compare(L"4.6");
	if (n < 0)
		return 2;

	if(n2 < 0)
		return 1;

	return 0;
}


int ComparVersion::GetCompareVCPlusPlus(wstring Version)
{
	if (Version == L"")
		return 0;

	wstring minVersionStr = Version.substr(0, 5);

	int n = minVersionStr.compare(L"v14.3");

	if (n ==0 )
		return 1;

	return 0;
}
