#include "CAdsBanner.h"

CAdsBanner::CAdsBanner()
{

}

std::vector<std::string> CAdsBanner::GetBannerlinkFromJson(std::wstring st)
{
	auto SaveFolder = FolderCore::GetOrCreateFolderPath(st) + L"/" + BannerCore::smName_BannerJson;
	HRESULT Hfile = URLDownloadToFile(NULL, BannerCore::URI_BannerJson.c_str(), SaveFolder.c_str(), 0,NULL);

	//¿í×Ö·û×ª¶à×Ö½Ú×Ö·û
	char* pStr;
	int iwstrLen = WideCharToMultiByte(CP_ACP, 0, SaveFolder.c_str(), -1, 0, 0, 0, 0);
	pStr = new char[iwstrLen + 1];
	memset(pStr, 0, sizeof(char) * (iwstrLen + 1));
	WideCharToMultiByte(CP_ACP, 0, SaveFolder.c_str(), -1, pStr, iwstrLen, 0, 0);

	return ReadJsonFromFile(pStr);
}

std::vector<std::string> CAdsBanner::ReadJsonFromFile(const char* file)
{
	try {
		Json::Reader reader;
		Json::Value root;

		std::ifstream is;
		is.open(file, std::ios::binary);
		BannerCollection.clear();
		if (reader.parse(is, root))
		{
			Json::Value val_image;
			LanguageType type = CLanguage::GetLocalLanguage(false);
			switch (type)
			{
			case LanguageType::EN_US:
				val_image = root["url"]["en"];
				break;
			case LanguageType::ZH_CN:
				val_image = root["url"]["zh-cn"];
				break;
			case LanguageType::ZH_TW:
				val_image = root["url"]["zh-tw"];
				break;
			default:
				val_image = root["url"]["en"];
				break;
			}

			int image_size = val_image.size();
			for (int j = 0; j < image_size; ++j)
			{
				std::string pic_url = val_image[j].asString();
				BannerCollection.push_back(pic_url);
			}
		}
		is.close();

	}
	catch (exception& e) {

	}
	
	return BannerCollection;
}

void CAdsBanner::GetBannerJson(std::wstring st)
{

}

bool CAdsBanner::GetImage(std::wstring url, std::wstring saveFolder, int i)
{

	HRESULT Hfile = URLDownloadToFile(NULL, url.c_str(), saveFolder.c_str(), 0, NULL);
	if (Hfile == S_OK)
		return true;
	else
		return false;
}
