#include "CMsgWnd.h"
#include <string>
DUI_BEGIN_MESSAGE_MAP(CMsgWnd, WindowImplBase)
DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK, OnClick)
DUI_END_MESSAGE_MAP()

CMsgWnd::CMsgWnd(void)
{
}

CMsgWnd::~CMsgWnd(void)
{
}

void CMsgWnd::SetTitle(LPCTSTR lpstrTitle)
{
	if (lstrlen(lpstrTitle) <= 0) return;

	CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("MessageTitle")));
	if (pControl) pControl->SetText(lpstrTitle);
}

void CMsgWnd::SetMsg(LPCTSTR lpstrMsg)
{
	if (lstrlen(lpstrMsg) <= 0) return;
	
	CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("MessageText")));
	if (pControl)
	{
		wstring str(lpstrMsg);
		auto count = str.find(L"\n", 0);//存在换行
		if (lstrlen(lpstrMsg) > 140 || count != -1)
		{
			RECT rect;
			rect.left = 0;
			rect.top = 30;
			rect.right = 0;
			rect.bottom = 0;
			pControl->SetPadding(rect);
		}
		pControl->SetText(lpstrMsg);
	}
		
}

void CMsgWnd::SetConfirmBtn(LPCTSTR lpstrConfirmBtn,bool isOnlyConfirmBtn)
{

	if (isOnlyConfirmBtn)
	{
		CControlUI* confirm_btn = static_cast<CControlUI*>(m_pm.FindControl(_T("confirm_btn")));
		if (confirm_btn) confirm_btn->SetVisible(false);

		CControlUI* paddControl = static_cast<CControlUI*>(m_pm.FindControl(_T("paddControl")));
		if (paddControl) paddControl->SetVisible(false);

		CControlUI* questionIcon = static_cast<CControlUI*>(m_pm.FindControl(_T("questionIcon")));
		if (questionIcon) questionIcon->SetVisible(false);

		CControlUI* warningIcon = static_cast<CControlUI*>(m_pm.FindControl(_T("warningIcon")));
		if (warningIcon) warningIcon->SetVisible(true);

		return;
	}

	if (lstrlen(lpstrConfirmBtn) <= 0) return;

	CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("confirm_btn")));
	if (pControl) pControl->SetText(lpstrConfirmBtn);
}

void CMsgWnd::SetCancelBtn(LPCTSTR lpstrCancelBtn)
{
	if (lstrlen(lpstrCancelBtn) <= 0) return;

	CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("cancel_btn")));
	if (pControl)
	{
		if (lstrlen(lpstrCancelBtn) > 16)
		{
			pControl->SetFixedWidth(150);
		}

		pControl->SetText(lpstrCancelBtn);
	}
}

void CMsgWnd::SetCancelBtn(LPCTSTR lpstrCancelBtn, UINT_PTR Tag)
{
	if (lstrlen(lpstrCancelBtn) <= 0) return;

	CControlUI* pControl = static_cast<CControlUI*>(m_pm.FindControl(_T("cancel_btn")));
	if (pControl)
	{
		if (lstrlen(lpstrCancelBtn) > 16)
		{
			pControl->SetFixedWidth(150);
		}
		pControl->SetTag(Tag);
		pControl->SetText(lpstrCancelBtn);
	}
}

void CMsgWnd::OnFinalMessage(HWND hWnd)
{
	__super::OnFinalMessage(hWnd);
	delete this;
}

DuiLib::CDuiString CMsgWnd::GetSkinFile()
{
	return _T("msg.xml");
}

LPCTSTR CMsgWnd::GetWindowClassName(void) const
{
	return _T("MsgWnd");
}

void CMsgWnd::OnClick(TNotifyUI& msg)
{
	CDuiString sName = msg.pSender->GetName();
	sName.MakeLower();

	if (msg.pSender == m_pCloseBtn) {
		Close(MSGID_CANCEL);
		return;
	}
	else if (msg.pSender == m_pMinBtn) {
		SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0); return;
	}
	else if (msg.pSender == m_pMaxBtn) {
		SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0); return;
	}
	else if (msg.pSender == m_pRestoreBtn) {
		SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0); return;
	}
	else if (msg.pSender == m_pMenuBtn) {
	}
	else if (sName.CompareNoCase(_T("confirm_btn")) == 0)
	{
		Close(MSGID_OK);
	}
	else if (sName.CompareNoCase(_T("cancel_btn")) == 0)
	{
		if (msg.pSender->GetTag()==1)/*临时的补丁，因为打开安装文件所在目录的按钮被定义为取消按钮*/
		{
			Close(MSGID_OK);
		}
		else
		{
			Close(MSGID_CANCEL);
		}
	}
}

LRESULT CMsgWnd::HandleCustomMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	bHandled = FALSE;
	return 0;
}

void CMsgWnd::Notify(TNotifyUI& msg)
{
	return WindowImplBase::Notify(msg);
}

LRESULT CMsgWnd::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	bHandled = FALSE;
	return 0L;
}

void CMsgWnd::InitWindow()
{
	m_pCloseBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("closebtn")));
}
