#include "CFrameWnd.h"

#pragma region 初始化

void CFrameWnd::InitHomeControls()
{

}

LRESULT CFrameWnd::HomeMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	LRESULT lRes = 0;
	BOOL bHandled1 = TRUE;

	if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes;
	return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
}

#pragma endregion


/// <summary>
/// 主页控件绑定事件
/// </summary>
void CFrameWnd::NotifyHome(TNotifyUI& msg)
{
	CDuiString control = msg.pSender->GetName();

	if (msg.sType == _T("click"))
	{
		//打开路径选择
		if (control == UI_BtnTextSelectFolder || control == UI_BtnIconSelectFolder)
		{
			SetSelectedFolder();
		}

		if (control == UI_BtnTermsService)
		{
			GoToLink(TermsOfServiceLink);
		}

		if (control == UI_BtnPrivacyPolicy)
		{
			GoToLink(PrivacyPolicyLink);
		}

		//点击安装按钮
		if (control == UI_BtnInstall)
		{
			Install_Click();
		}

		//显示隐藏自定义安装路径区域
		if (control == UI_CheckInstallSetting || control == UI_Lbicon) {
			{
				if (control == UI_Lbicon)
				{
					m_InstallationPage->CheckInstallSetting->SetCheck(!m_InstallationPage->CheckInstallSetting->IsSelected());
				}

				CustomInstallWndSize(m_InstallationPage->CheckInstallSetting->IsSelected());
			}
		}
		
	}

}

/// <summary>
/// 用户隐私协议等链接
/// </summary>
void CFrameWnd::GoToLink(std::wstring linkStr)
{
	const TCHAR szOperation[] = _T("open");
	HINSTANCE hRslt = ShellExecute(NULL, szOperation,linkStr.c_str(), NULL, NULL, SW_SHOWNORMAL);
	assert(hRslt > (HINSTANCE)HINSTANCE_ERROR);
	
}

//点击安装按钮
void CFrameWnd::Install_Click()
{
	//收起自定义安装的布局
	if (ischeckCustom)
		CustomInstallWndSize(!ischeckCustom);

	BOOL m_bOnline = NetWorkState::IsNetWorking();
	if (m_bOnline)
	{
		//磁盘是否有足够的空间
		if (DiskCore::IsEnoughDisk(PathCore::smPath_Install) == false)
		{
			MessageBoxWnd::Show(m_hWnd, MessageContentType::DiskNoSpace);
			return;
		}

		//获取本机版本号和网络版本号进行判断，返回是否需要安装产品，true为要安装
		m_isCanInstall = GetVersionState();

		if (m_isCanInstall)
		{
			SelectLayout(LayoutType::Installing);
			//进度条左下角的文案，包括安装中和断网的提示文案
			m_InstallingPage->installNotWork->SetText(CLanguage::GetDuiText(TextType::Text_Installing));
			m_InstallingPage->NetWorkUI(m_bOnline);

			KillAllTimer();
			StartTimer(Timer_Banner_ID);
			StartTimer(Timer_Installing_ID);

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

	}
	else
	{
		WaitForNetwork();

		m_InstallingPage->NetWorkUI(m_bOnline);
		SelectLayout(LayoutType::Installing);
	}
	
}

/// <summary>
/// 选择安装路径
/// </summary>
void CFrameWnd::SetSelectedFolder()
{
	wstring sFolder = FolderCore::FolderBrowser(m_hWnd);
	if (sFolder.empty() != true && sFolder != L"")
	{
		if (FolderCore::IsRootDirectoryW(sFolder) == true)
		{
			sFolder = FolderCore::AddSubFolderFromRootDirW(sFolder, Product::smName_Product);
		}

		m_InstalledPage->RefreshInstalledPath(sFolder);

		PathCore::smPath_Install = sFolder;
		m_InstallationPage->EdFilepath->SetText(sFolder.c_str());
	}

}

void CFrameWnd::Thread_GetVerstion(ThreadData threadData)
{
	threadData.pFrame->m_isCanInstall = threadData.pFrame->GetVersionState();
}


bool CFrameWnd::GetVersionState()
{
	ComparVersion comparVersion;
	VertionResultType state = comparVersion.GetProductInfo(Product::smName_Product, Product::URI_AppXml);
	m_State.m_NetFrameworkType = comparVersion.NetFramework;
	m_State.m_VcPlusPlusType = comparVersion.VcPlusPlus;
	int stateBtn;
	bool result = false;

	switch (state)
	{
	case VertionResultType::None:
		result = true;
		break;
	case VertionResultType::Low:
		stateBtn = MessageBoxWnd::Show(m_hWnd, MessageContentType::LowVersion);
		result = (stateBtn == IDOK ? true : false);
		break;
	case VertionResultType::Same:
		MessageBoxWnd::Show(m_hWnd, MessageContentType::SameVersion);
		result = false;
		break;
	case VertionResultType::Hight:
		MessageBoxWnd::Show(m_hWnd, MessageContentType::HighVertion);
		result = false;
		break;
	default:
		break;
	}
	return result;
}


