#include "stdafx.h"
#include "CConnectMage.h"

CConnectMage::CConnectMage() : m_clash_Api(nullptr), m_clash_config(nullptr), m_connect_status(ConnectState::Stop), m_Asyntask(4)
{
	SNotifyCenter::getSingleton().addEvent(EVENTID(EventConnect));
	m_isclash_config = false;
}

CConnectMage::~CConnectMage()
{
	if (m_clash_Api)
	{
		delete m_clash_Api;
		m_clash_Api = nullptr;
	}
}

void CConnectMage::Stop()
{
	m_connect_status = ConnectState::Stop;
	m_Asyntask.AddTask(&CConnectMage::ThreadFun_ChangeClash_Stop, this, (LPARAM)nullptr);
}

void CConnectMage::ReqConfig()
{
	m_Asyntask.AddTask(&CConnectMage::ThreadFun_ChangeClash_Config, this, (LPARAM)nullptr);
}

void CConnectMage::ChangeClashNode()
{
	m_Asyntask.AddTask(&CConnectMage::ThreadFun_ChangeClash_Node, this, (LPARAM)nullptr);
}

void CConnectMage::ChangeClashInit()
{
}

void CConnectMage::Init()
{
	if (!m_clash_Api)
	{
		m_clash_Api = new ClashApi();
		m_clash_Api->SetProt(m_clash_config->GetClasApiPort());

	}
}

void CConnectMage::SetNodeName(std::string name)
{
	m_name = name;
}

void CConnectMage::SetClashConfig(CLashConfig* m)
{
	m_clash_config = m;
}

ConnectState CConnectMage::GetConnectStatus()
{
	return m_connect_status;
}

void CConnectMage::ThreadFun_ChangeClash_Config(LPARAM lParam)
{
	if (!CApp::getSingletonPtr()->GetClashRuning())
	{
		m_connect_status = ConnectState::Stop;
		m_error = L"内核没有启动，重启电脑或者软件";
		EventConnect* pEvt = new EventConnect(nullptr);
		pEvt->status = m_connect_status;
		pEvt->msg = m_error;
		SNotifyCenter::getSingleton().FireEventAsync(pEvt);
		pEvt->Release();
		return;
	}
 
	if (m_clash_config == nullptr || m_clash_Api == nullptr)
	{
		m_connect_status = ConnectState::Stop;
		m_error = L"错误,重启电脑";
		EventConnect* pEvt = new EventConnect(nullptr);
		pEvt->status = m_connect_status;
		pEvt->msg = m_error;
		SNotifyCenter::getSingleton().FireEventAsync(pEvt);
		pEvt->Release();
		return;
	}
	//创建配置
	if (!m_clash_config->MakeClash())
	{
		m_connect_status = ConnectState::Stop;
		m_error = L"没有生成配置,检查系统是否有管理员";
		EventConnect* pEvt = new EventConnect(nullptr);
		pEvt->status = m_connect_status;
		pEvt->msg = m_error;
		SNotifyCenter::getSingleton().FireEventAsync(pEvt);
		pEvt->Release();
		return;
	}

	/*std::filesystem::path config;

	if (m_connect_status == ConnectState::Stop && CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode)
	{
		config = CApp::getSingletonPtr()->GetConfigPath() / DSPROXY_CONFIG_INIT_ClASH_NAME;
	}*/
	 

	//切换请求
	if (m_clash_Api->RequestConfigUpdate(m_clash_config->GetRunConfig()))
	{
		m_isclash_config = true;
		if (m_name.empty())
		{
			CServerList* serverlist = CApp::getSingletonPtr()->GetServerList();
			if (serverlist)
			{
				serverlist->MinimumSelectNode();

				if (!serverlist->node_name.empty())
				{
					if (m_clash_Api->UpdateProxyGroup("Proxy", serverlist->node_name))
					{
						m_connect_status = ConnectState::SwitchNodeSuccEss;
						m_error = L"连接成功";
						m_clash_config->SetProxy();
					}
					else {
						m_connect_status = ConnectState::Stop;
						m_error = L"连接失败,可以重新尝试";
					}
				}
			}

		}
		else {
			if (m_clash_Api->UpdateProxyGroup("Proxy", m_name))
			{
				m_connect_status = ConnectState::SwitchNodeSuccEss;
				m_error = L"连接成功";
				m_clash_config->SetProxy();
			}
			else {
				m_connect_status = ConnectState::Stop;
				m_error = L"连接失败,可以重新尝试";
			}
		}
	}
	else {
		m_connect_status = ConnectState::Stop;
		m_error = L"连接失败,可以重新尝试";
	}
 
	
	


	EventConnect* pEvt = new EventConnect(nullptr);
	pEvt->status = m_connect_status;
	pEvt->msg = m_error;
	SNotifyCenter::getSingleton().FireEventAsync(pEvt);
	pEvt->Release();
}

void CConnectMage::ThreadFun_ChangeClash_Node(LPARAM lParam)
{
	if (m_clash_config == nullptr || m_clash_Api == nullptr)
	{
		m_connect_status = ConnectState::Stop;
		m_error = L"错误,重启电脑";
		EventConnect* pEvt = new EventConnect(nullptr);
		pEvt->status = m_connect_status;
		pEvt->msg = m_error;
		SNotifyCenter::getSingleton().FireEventAsync(pEvt);
		pEvt->Release();
		return;
	}
	
	if (m_clash_Api->UpdateProxyGroup("Proxy", m_name))
	{
		m_connect_status = ConnectState::SwitchNodeSuccEss;
		m_error = L"切换成功..点击断开";
		m_clash_config->SetProxy();
	}
	else {
		m_connect_status = ConnectState::Stop;
		m_error = L"切换失败,可以重新尝试";
	}

	EventConnect* pEvt = new EventConnect(nullptr);
	pEvt->status = m_connect_status;
	pEvt->msg = m_error;
	SNotifyCenter::getSingleton().FireEventAsync(pEvt);
	pEvt->Release();
}

void CConnectMage::ThreadFun_ChangeClash_Stop(LPARAM lParam)
{
	if (!CApp::getSingletonPtr()->GetClashRuning())
	{
		m_connect_status = ConnectState::Stop;
		m_error = L"内核没有启动，重启电脑或者软件";
		EventConnect* pEvt = new EventConnect(nullptr);
		pEvt->status = m_connect_status;
		pEvt->msg = m_error;
		SNotifyCenter::getSingleton().FireEventAsync(pEvt);
		pEvt->Release();
		return;
	}

	if (m_clash_config == nullptr || m_clash_Api == nullptr)
	{
		m_connect_status = ConnectState::Stop;
		m_error = L"错误,重启电脑";
		EventConnect* pEvt = new EventConnect(nullptr);
		pEvt->status = m_connect_status;
		pEvt->msg = m_error;
		SNotifyCenter::getSingleton().FireEventAsync(pEvt);
		pEvt->Release();
		return;
	}
	//创建配置
	if (!m_clash_config->MakeClash())
	{
		m_connect_status = ConnectState::Stop;
		m_error = L"没有生成配置,检查系统是否有管理员";
		EventConnect* pEvt = new EventConnect(nullptr);
		pEvt->status = m_connect_status;
		pEvt->msg = m_error;
		SNotifyCenter::getSingleton().FireEventAsync(pEvt);
		pEvt->Release();
		return;
	}

	/*std::filesystem::path config;

	if (m_connect_status == ConnectState::Stop && CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode)
	{
		config = CApp::getSingletonPtr()->GetConfigPath() / DSPROXY_CONFIG_INIT_ClASH_NAME;
	}*/


	//切换请求
	if (m_clash_Api->RequestConfigUpdate(CApp::getSingletonPtr()->GetConfigPath() / DSPROXY_CONFIG_INIT_ClASH_NAME))
	{
		m_isclash_config = true;
		if (m_name.empty())
		{
			CServerList* serverlist = CApp::getSingletonPtr()->GetServerList();
			if (serverlist)
			{
				serverlist->MinimumSelectNode();

				if (!serverlist->node_name.empty())
				{
					if (m_clash_Api->UpdateProxyGroup("Proxy", serverlist->node_name))
					{
						m_connect_status = ConnectState::SwitchNodeSuccEss;
						m_error = L"连接成功";
						m_clash_config->SetProxy();
					}
					else {
						m_connect_status = ConnectState::Stop;
						m_error = L"连接失败,可以重新尝试";
					}
				}
			}

		}
		else {
			if (m_clash_Api->UpdateProxyGroup("Proxy", m_name))
			{
				m_connect_status = ConnectState::SwitchNodeSuccEss;
				m_error = L"连接成功";
				m_clash_config->SetProxy();
			}
			else {
				m_connect_status = ConnectState::Stop;
				m_error = L"连接失败,可以重新尝试";
			}
		}
	}
	else {
		m_connect_status = ConnectState::Stop;
		m_error = L"连接失败,可以重新尝试";
	}

}
