#include "base_form.h"
#include "serial_port.h"

const std::wstring BasicForm::kClassName = L"Basic";

BasicForm::BasicForm():
	mMonitorNewSelect(nullptr),
	mRightShow(nullptr)
{

}

BasicForm::~BasicForm()
{

}

std::wstring BasicForm::GetSkinFolder()
{
	return L"basic";
}

std::wstring BasicForm::GetSkinFile()
{
	return L"basic.xml";
}

std::wstring BasicForm::GetWindowClassName() const
{
	return kClassName;
}

LRESULT BasicForm::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (uMsg == WM_ADD_UART_CLOSE) {
		wprintf(L"close %s\r\n", ((wstring*)wParam)->c_str());
		mRightShow->SetVisible(false);
		mRightSide->SetAutoDestroy(true);
		mUartForm[*(wstring*)(wParam)] = nullptr;
		int cnt = mMonitor->GetRootNode()->GetChildNode(0)->GetChildNodeCount();
		ui::TreeNode* p = nullptr;
		for (int i = 0; i < cnt; i++) {
			if (mMonitor->GetRootNode()->GetChildNode(0)->GetChildNode(i)->GetText() == *(wstring*)(wParam)) {
				p = mMonitor->GetRootNode()->GetChildNode(0)->GetChildNode(i);
				break;
			}
		}
		auto comname = *(wstring*)(wParam);
		
		mMonitor->GetRootNode()->GetChildNode(0)->RemoveChildNode(p);

		UINT PortNum = 0;
		for (int i = 3; comname[i] != L'\0'; i++)   //ת��Ϊ����
		{
			PortNum = PortNum * 10 + (comname[i] - L'0');
		}
		SerialPort::ClosePort(PortNum);
	}
	if (uMsg == WM_ADD_UART_RECVDATA) {
		printf("WM_ADD_UART_RECVDATA");
		//this->Paint();

	}
	if (uMsg == WM_USER_POS_CHANGED) {

	}
	if (uMsg == WM_ADD_UART_MONITOR) {
		printf("add monitor");
		UartInfo* p = (UartInfo*)wParam;
		wprintf(L"%s", p->name.c_str());

		ui::TreeNode* node = new ui::TreeNode;
		node->SetText(p->name);
		node->SetClass(L"listitem");
		node->SetFixedHeight(20);
		node->SetMargin({ 20, 0, 0, 0 });
		mMonitor->GetRootNode()->GetChildNode(0)->AddChildNode(node);

		if (mUartForm.find(p->name) == mUartForm.end()) {
			auto form = new UartForm(this,p->name, p->baurate,
				p->data_bits, p->stop_bits, p->verify, p->flow_control);
			form->SetChildLayoutXML(L"basic/uart_form.xml");
			form->SetName(p->name);
			form->SetVisible(false);
			mUartForm[p->name] = form;
			if (!mRightSide->Add(form))
				printf("erroer 1");
		}
		node->AttachAllEvents([this](ui::EventArgs* ev){
			if (ui::EventType::kEventSelect == ev->Type) {
				wprintf(L"%s\r\n",dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText().c_str());
				printf("GetCurSel %d\r\n", mRightSide->GetCurSel());
				UartForm* p = mUartForm[dynamic_cast<ui::TreeNode*> (ev->pSender)->GetText()];
				if (p != nullptr) {
					p->SetAutoDestroy(true);
					if (mRightShow != nullptr) {
						mRightShow->SetVisible(false);
						p->SetVisible(true);
						mRightShow = p;
						mRightSide->SelectItem(p->GetName());

					}
					else {
						p->SetVisible(true);

						mRightSide->SelectItem(p->GetName());
						mRightShow = p;
					}
				}
			}
			return true;
		});
	}
	return WindowImplBase::HandleMessage(uMsg, wParam, lParam);
}

void BasicForm::InitWindow()
{
	/* Show settings menu */
	ui::Button* btn_add_new = dynamic_cast<ui::Button*>(FindControl(L"add_new"));
	btn_add_new->SetText(L"�½�������");
	if (nullptr != btn_add_new) {
		btn_add_new->AttachClick([this](ui::EventArgs* args) {
			if (mMonitorNewSelect == nullptr) {
				mMonitorNewSelect = new NewMonitorForm(this);
				mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(),
					WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0);
				mMonitorNewSelect->CenterWindow();
				mMonitorNewSelect->ShowModalFake(this->GetHWND());
			}
			else {
				mMonitorNewSelect = new NewMonitorForm(this);
				mMonitorNewSelect->Create(this->GetHWND(), NewMonitorForm::kClassName.c_str(),
					WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & WS_SIZEBOX, 0);
				mMonitorNewSelect->CenterWindow();
				mMonitorNewSelect->ShowModalFake(this->GetHWND());
			}
			return true;
		});
	}
	mMonitor = dynamic_cast<ui::TreeView*>(FindControl(L"tree"));
	if (nullptr != mMonitor) {
		ui::TreeNode* node = new ui::TreeNode;
		node->SetText(nbase::StringPrintf(L"uart"));
		node->SetClass(L"listitem");
		node->SetFixedHeight(20);
		node->SetMargin({ 10, 0, 0, 0 });
		mMonitor->GetRootNode()->AddChildNode(node);

		node = new ui::TreeNode;
		node->SetText(nbase::StringPrintf(L"tcp client"));
		node->SetClass(L"listitem");
		node->SetFixedHeight(20);
		node->SetMargin({ 10, 0, 0, 0 });
		mMonitor->GetRootNode()->AddChildNode(node);


		node = new ui::TreeNode;
		node->SetText(nbase::StringPrintf(L"tcp server"));
		node->SetClass(L"listitem");
		node->SetFixedHeight(20);
		node->SetMargin({ 10, 0, 0, 0 });
		mMonitor->GetRootNode()->AddChildNode(node);

		node = new ui::TreeNode;
		node->SetText(nbase::StringPrintf(L"udp client"));
		node->SetClass(L"listitem");
		node->SetFixedHeight(20);
		node->SetMargin({ 10, 0, 0, 0 });
		mMonitor->GetRootNode()->AddChildNode(node);

		node = new ui::TreeNode;
		node->SetText(nbase::StringPrintf(L"udp server"));
		node->SetClass(L"listitem");
		node->SetFixedHeight(20);
		node->SetMargin({ 10, 0, 0, 0 });
		mMonitor->GetRootNode()->AddChildNode(node);

		node = new ui::TreeNode;
		node->SetText(nbase::StringPrintf(L"udp group"));
		node->SetClass(L"listitem");
		node->SetFixedHeight(20);
		node->SetMargin({ 10, 0, 0, 0 });
		mMonitor->GetRootNode()->AddChildNode(node);
	}

	mRightSide = dynamic_cast<ui::TabBox*>(FindControl(L"tab_side"));

	mRightSide->RemoveAll();

}

LRESULT BasicForm::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	PostQuitMessage(0L);
	return __super::OnClose(uMsg, wParam, lParam, bHandled);
}