#include "CFrameWnd.h"
//说明：初始化窗口，但对运行的程序不常操作
#pragma region 初始化

SilenceInstallingType CFrameWnd::m_SilenceInstallingType = SilenceInstallingType::None;

CFrameWnd::CFrameWnd(LPCTSTR pszXMLPath)
	: m_strXMLPath(pszXMLPath)
{

}

CFrameWnd::CFrameWnd()
{
	m_MainPage.SetPaintMagager(&m_pm);
	AddVirtualWnd(_T("MainWnd"), &m_MainPage);
	banner = new CAdsBanner();
	m_threadData.pFrame = this;
}

CFrameWnd::~CFrameWnd(void)
{
	RemoveVirtualWnd(_T("MainWnd"));
}

LPCTSTR CFrameWnd::GetWindowClassName() const
{
	return _T("MainWnd");
}

CDuiString CFrameWnd::GetSkinFolder()
{
	return _T("");
}

void CFrameWnd::InitControls()
{
	m_InstallationPage = new InstallationPage(&m_pm);
	m_InstallingPage = new InstallingPage(&m_pm);
	m_InstalledPage = new InstalledPage(&m_pm);
	InitHomeControls();
	InitInstallingControls();
	InitInstalledControls();

}

void CFrameWnd::InitAsscessKey()
{
	ComparVersion comparVersion;
	VertionResultType state = comparVersion.GetProductInfo(Product::smName_Product, Product::URI_AppXml);
	
}

void CFrameWnd::InitWindow()
{
	SetIcon(IDI_ICON1);
	SetWindowLong(m_hWnd, GWL_STYLE, GetWindowLong(m_hWnd, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME);//禁止最大化窗口
	this->CenterWindow();
	InitControls();
	//AdaptationDPI();
	SelectLayout(LayoutType::Home);
	ShowWindow();
	//MessageBoxWnd::Show(m_hWnd, MessageContentType::InstalledIsExitApp);
}


void CFrameWnd::AdaptationDPI()
{
	HWND hd = FindWindow(L"Progman", L"Program Manager");

	int dpiValue = GetDpiForWindow(hd);//当前DPI

	double currentZoom = dpiValue / 96.0;//当前显示比例
	double tempDPI = currentZoom;

	int dpiOffer = (int)(96 - (currentZoom - 1) * 96.0);//DPI偏移量

	if (dpiValue == 96)
	{
		dpi = 1;
		CurrentDPI = 96;
	}
	else
	{
		dpi = dpiOffer / 96.0;//比例偏移量
		CurrentDPI = 96 - dpiOffer;
		dpi = 1 - dpi;

	}

	//m_pm.SetDPI(CurrentDPI);
}

void CFrameWnd::setDPI(int DPI)
{
	//m_pm.SetDPI(DPI);
}
//使用OnCreate就会不触发GetSkinFile事件
//LRESULT CFrameWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
//{
//
//	return 1;
//}


LRESULT CFrameWnd::OnDPIChanged(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	m_pm.SetDPI(LOWORD(wParam));
	m_pm.ResetDPIAssets();
	int g_dpi = HIWORD(wParam);

	RECT* const prcNewWindow = (RECT*)lParam;
	SetWindowPos(m_hWnd,
		NULL,
		prcNewWindow->left,
		prcNewWindow->top,
		prcNewWindow->right - prcNewWindow->left,
		prcNewWindow->bottom - prcNewWindow->top,
		SWP_NOZORDER | SWP_NOACTIVATE);
	if (m_pm.GetRoot() != NULL) m_pm.GetRoot()->NeedUpdate();

	bHandled = false;

	wstring optionName;
	wstringstream wss;
	wss << L"DPI";
	wss << m_pm.GetDPIObj()->GetDPI();
	wss >> optionName;
	COptionUI* option = static_cast<COptionUI*>(m_pm.FindControl(optionName.c_str()));
	option->Selected(true);

	return 0;
}

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

	switch (uMsg)
	{
	case WM_CREATE:break;
	
	case WM_DESTROY:
	{
		::PostQuitMessage(0);
		//清除缓存文件（缓存文件夹InstallTemp,Install包含Banner图片，xml产品信息，config.ini等）
		//安装包
		FolderCore::ClearAllTempFolder();
		break;
	}

	case WM_NCCALCSIZE:
		return 0;
		/*case WM_DPICHANGED:
			lRes = OnDPIChanged(uMsg, wParam, lParam, bHandled);
			break;*/
	case WM_TIMER:
		lRes = OnTimer(uMsg, wParam, lParam, bHandled);
		break;
	case WM_KEYDOWN:
		lRes = OnKeyDown(uMsg, wParam, lParam, bHandled);
	default:
		break;
	}

	return lRes;

	//以下代码的实现，会导致编辑框失去焦点之后而无法再次获取焦点
	/*if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes;
	return CWindowWnd::HandleMessage(uMsg, wParam, lParam);*/

}

LRESULT CFrameWnd::OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	switch (wParam)
	{
	case _T('A'):
	{
		if (GetKeyState(VK_CONTROL) < 0)
		{
			if (m_InstallationPage->EdFilepath->IsFocused())
			{
				m_InstallationPage->EdFilepath->SetSelAll();
			}
		}
	}

	case _T('C'):
	{
		if (GetKeyState(VK_CONTROL) < 0)
		{
			if (m_InstallationPage->EdFilepath->IsFocused() &&
				m_InstallationPage->EdFilepath->GetSelText() == m_InstallationPage->EdFilepath->GetText())
			{
				m_InstallationPage->EdFilepath->Copy();
			}
		}
	}


	break;
	}
	return 0;
}



void CFrameWnd::Notify(TNotifyUI& msg)
{
	SlotMsg(msg);
	LayoutNotify(msg);
	return WindowImplBase::Notify(msg);
}

void CFrameWnd::LayoutNotify(TNotifyUI& msg)
{
	switch (m_State.m_LayoutType)
	{
	case LayoutType::Home:
		NotifyHome(msg);
		break;
	case LayoutType::Installing:
		NotifyInstalling(msg);
		break;
	case LayoutType::Installed:
		NotifyInstalled(msg);
		break;
	default:
		break;
	}
}

#pragma endregion


//自定义安装，收缩展开窗口大小
void CFrameWnd::CustomInstallWndSize(bool isExpend)
{
	try
	{
		if (isExpend)
		{
			std::wstring textcontent = CLanguage::GetText(TextType::Btn_CutomInstall);
			m_InstallationPage->CheckInstallSetting->SetText(textcontent.c_str());
			m_InstallationPage->Lbicon->SetText(L"▲");
			::GetClientRect(*this, &start_rect_);
			SetWindowPos(m_hWnd, NULL, 0, 0, XML_WndExpendWidth * dpi, XML_WndExpenHeight * dpi, SWP_NOMOVE);
			ischeckCustom = true;

		}
		else
		{
			std::wstring textcontent = CLanguage::GetText(TextType::Btn_CutomInstall);
			m_InstallationPage->Lbicon->SetText(L"▼");
			m_InstallationPage->CheckInstallSetting->SetText(textcontent.c_str());
			::GetClientRect(*this, &start_rect_);
			SetWindowPos(m_hWnd, NULL, 0, 0, XML_WndWidth * dpi, XML_WndHeight * dpi, SWP_NOMOVE);
			ischeckCustom = false;
		}
	}
	catch (exception ex)
	{

	}
	

	//刚开始点击自定义安装会崩溃
	//m_pm.SetDPI(CurrentDPI);

}
//if (msg.sType == _T("link"))
//{
//	if (control == UI_TextShowContentLink)
//	{
//		DuiLib::CTextUI* pText = (DuiLib::CTextUI*)msg.pSender;
//		int index = (msg.wParam == 0 ? 0 : 1);
//		DuiLib::CDuiString* str = pText->GetLinkContent(index);
//		ShellExecute(NULL, _T("open"), str->GetData(), NULL, NULL, SW_SHOWNORMAL);
//	}
//}
