// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MainDlg.h"
#include "StabtypeControl.h"
#include "CLashConfig.h"
CMainDlg::CMainDlg() : SHostWnd(_T("LAYOUT:XML_MAINWND")), m_base_clash(nullptr)
{
	m_bLayoutInited = FALSE;
}

CMainDlg::~CMainDlg()
{
	if (m_base_clash)
	{
		m_base_clash->StopClash();
		delete m_base_clash;
		m_base_clash = nullptr;
	}
}

int CMainDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	SetMsgHandled(FALSE);
	return 0;
}

BOOL CMainDlg::OnInitDialog(HWND hWnd, LPARAM lParam)
{
	m_bLayoutInited = TRUE;
	Init();
	return 0;
}

void CMainDlg::Init()
{

	//FindChildByName2<STabCtrl>(L"tabmain");
	if (!m_base_clash)
	{
		m_base_clash = new CLashConfig();
		m_base_clash->InitClash();
		m_base_clash->StartClash();
	}

	SysMode list;
	list.id = 0;
	list.name = L"系统代理";
	list.proxy_mode = PROXY_MODE::sys_mode;
	SysMode list1;
	list1.id = 1;
	list1.name = L"网卡代理";
	list1.proxy_mode = PROXY_MODE::tun_mode;
	m_sysmode_vctor.push_back(list);
	m_sysmode_vctor.push_back(list1);
	
	RouteMode route;
	route.id = 0;
	route.name = L"智能";
	route.route_mode = ROUT_MODE::cn_mode;

	RouteMode route2;
	route2.id = 1;
	route2.name = L"全局";
	route2.route_mode = ROUT_MODE::qg_mode;

	
	
	m_route_vctor.push_back(route);

	m_route_vctor.push_back(route2);

	

	StabtypeControl* tabtype = FindChildByName2<StabtypeControl>(L"sysmode");
	if (tabtype)
	{
		for (auto i = 0; i < m_sysmode_vctor.size(); i++)
		{
			tabtype->ItemCreateChildren(m_sysmode_vctor[i].id, m_sysmode_vctor[i].name,i == 0 ? true : false );
		}
		if (m_sysmode_vctor.size() > 0)
		{
			CApp::getSingletonPtr()->SetSysMode(m_sysmode_vctor[0].proxy_mode);
		}
		tabtype->GetEventSet()->subscribeEvent(EVT_TABTYPE_CONTROL,
			Subscriber(&CMainDlg::OnSysModeType, this));
	}

	StabtypeControl* routetype = FindChildByName2<StabtypeControl>(L"routemode");
	if (routetype)
	{
		for (auto i = 0; i < m_route_vctor.size(); i++)
		{
			routetype->ItemCreateChildren(m_route_vctor[i].id, m_route_vctor[i].name, i == 0 ? true : false);
		}
		if (m_route_vctor.size() > 0)
		{
			CApp::getSingletonPtr()->SetRouteMode(m_route_vctor[0].route_mode);
		}
		routetype->GetEventSet()->subscribeEvent(EVT_TABTYPE_CONTROL,
			Subscriber(&CMainDlg::OnRouteModeType, this));
	}


}

void CMainDlg::OnNode()
{
	STabCtrl* m_tab_main = FindChildByName2<STabCtrl>(L"tabmain");
	if (m_tab_main)
	{
		m_tab_main->SetCurSel(1);
		m_tab_main->Invalidate();
	}

}

void CMainDlg::OnBackNode()
{
	STabCtrl* m_tab_main = FindChildByName2<STabCtrl>(L"tabmain");
	if (m_tab_main)
	{
		m_tab_main->SetCurSel(0);
		m_tab_main->Invalidate();
	}
}

bool CMainDlg::OnSysModeType(SOUI::EventArgs* pEvt)
{
	SOUI::EventTabtypeControl* pTestControlEvent =
		SOUI::sobj_cast<SOUI::EventTabtypeControl>(pEvt);
	if (!pTestControlEvent)
	{
		return false;
	}
	CApp::getSingletonPtr()->SetSysMode(m_sysmode_vctor[pTestControlEvent->nIndex].proxy_mode);
	return true;
}

bool CMainDlg::OnRouteModeType(SOUI::EventArgs* pEvt)
{
	SOUI::EventTabtypeControl* pTestControlEvent =
		SOUI::sobj_cast<SOUI::EventTabtypeControl>(pEvt);
	if (!pTestControlEvent)
	{
		return false;
	}
	CApp::getSingletonPtr()->SetRouteMode(m_route_vctor[pTestControlEvent->nIndex].route_mode);


	return true;
}


//TODO:消息映射
void CMainDlg::OnClose()
{
	OnMinimize();
}

void CMainDlg::OnMaximize()
{
	SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE);
}
void CMainDlg::OnRestore()
{
	SendMessage(WM_SYSCOMMAND, SC_RESTORE);
}
void CMainDlg::OnMinimize()
{
	SendMessage(WM_SYSCOMMAND, SC_MINIMIZE);
}

void CMainDlg::OnCommand(UINT uNotifyCode, int nID, HWND wndCtl) {

	if (uNotifyCode == 0)
	{
		switch (nID)
		{
			case 6:
			{
				//CApp::getSingletonPtr()->Is_out(1);
				//if (m_clash)
				//{
				//	m_clash->stop();
				//	m_clash->ClashStop();
				//	delete m_clash;
				//	m_clash = nullptr;
				//}
				//DisableSystemProxy();
				//CApp::getSingletonPtr()->KillProcess(L"syscode.exe");
				////CApp::getSingletonPtr()->Is_out(1);
				CSimpleWnd::DestroyWindow();
				////PostMessage(WM_CLOSE);
				break;
			}


		default:
			break;
		}
	}
}