#include "CFrameWnd.h"

void CFrameWnd::Thread_LoadedBanner(ThreadData threadData)
{
	threadData.pFrame->GetBanner();
	threadData.pFrame->InitAsscessKey();
}

void CFrameWnd::GetBanner()
{
	std::wstring tempPath = FolderCore::GetAppTempPath();
	banner->GetBannerlinkFromJson(tempPath);

	auto imgFilePath = tempPath + L"/" + std::to_wstring(0) + L".png";

	//获取图片
	for (size_t i = 0; i < banner->BannerCollection.size(); i++)
	{
	
		if (i <= BannerCore::ShowBannerCount)
		{
			char* ch = (LPSTR)(LPCTSTR)banner->BannerCollection[i].c_str();
			std::wstring wch = CharConvert::GetWideCharW(ch);
			imgFilePath = tempPath + L"/" + to_wstring(i) + L".png";

			bool isSuccess = banner->GetImage(wch, imgFilePath, i);
			if (banner->IsEmptyBanner == true && isSuccess == true)
				banner->IsEmptyBanner = false;
				
		}
		else
		{
			break;
		}

		//由于下载图片需要时间，优先下载第一张后，就显示该图片
		if (i == 0)
		{
			HBITMAP hBitmap = NULL;

			m_InstallingPage->pTabSwitch->RemoveAll();

			{
				imgFilePath = tempPath + L"/" + std::to_wstring(i) + L".png";
				CDuiString ImagePath(imgFilePath.c_str());
				CreateBannerUI(ImagePath, i);
			}
			m_InstallingPage->pTabSwitch->SelectItem(0);
		}

	}

	HBITMAP hBitmap = NULL;

	int count = 0;
	if (BannerCore::ShowBannerCount < banner->BannerCollection.size())
	{
		count = BannerCore::ShowBannerCount;
	}
	else
	{
		count = banner->BannerCollection.size();
	}
	for (size_t i = 1; i < count; i++)
	{
		imgFilePath = tempPath + L"/" + std::to_wstring(i) + L".png";
		CDuiString ImagePath(imgFilePath.c_str());
		CreateBannerUI(ImagePath, i);
	}

}

void CFrameWnd::CreateBannerUI(CDuiString ImagePath, int i)
{
	std::wstring ss = L"Banner" + std::to_wstring(i);
	CControlUI* tem = new CControlUI();
	tem->SetName(ss.c_str());
	tem->SetFixedWidth(XML_BannerWidth);
	tem->SetFixedHeight(XML_BannerHeight);
	tem->SetMinWidth(XML_BannerWidth);
	tem->SetMaxHeight(XML_BannerHeight);
	m_pm.RemoveImage(ImagePath);
	m_pm.AddImage(ImagePath);
	tem->SetBkImage(ImagePath);
	m_InstallingPage->pTabSwitch->Add(tem);
}

void CFrameWnd::SetTimerBanner()
{
	bool m_bOnline = NetWorkState::IsNetWorking();

	if (m_bOnline)
	{
		KillAllTimer();
		StartTimer(Timer_Banner_ID);
		StartTimer(Timer_Installing_ID);
		CFrameWnd::m_SilenceInstallingType = SilenceInstallingType::DownLoading;
		m_InstallingPage->NetWorkUI(m_bOnline);

		if (banner->BannerCollection.size() == 0)
			GetBanner();

		thread installingThread(Thread_Installing, m_threadData);
		installingThread.detach();
		
	}
}

void CFrameWnd::SetTimerBannerSwitch()
{
	currentImageNum++;
	currentImageNum = currentImageNum >= BannerCore::ShowBannerCount ? 0 : currentImageNum;
	m_InstallingPage->pTabSwitch->SelectItem(currentImageNum);
}

void CFrameWnd::WaitForNetwork()
{
	KillAllTimer();
	StartTimer(Timer_WaitForNetwork_ID);
}

