#include "stdafx.h"
#include "CServerList.h"






CServerList::CServerList() 
{
}

CServerList::~CServerList()
{
}

bool CServerList::Init(std::string data)
{
	
	try
	{
		//std::vector<std::string> tags;
		vectlistmode.clear();
		nlohmann::json j = nlohmann::json::parse(data.begin(), data.end());

		if (j["code"].get<int>() == 0)
		{
			
			auto data = j["data"];
			for (int i = 0; i < data.size(); i++) {
				
				CServerListMode m_serverlist_mode;
				m_serverlist_mode.type = data[i]["type"].get<std::string>();
				m_serverlist_mode.id = data[i]["id"].get<int>();
			
				m_serverlist_mode.name = data[i]["name"].get<std::string>();
				m_serverlist_mode.host = data[i]["host"].get<std::string>();
				m_serverlist_mode.port = data[i]["port"].get<int>();
				m_serverlist_mode.created_at = data[i]["created_at"].get<int>();
				m_serverlist_mode.rate = data[i]["rate"].get<std::string>();

				if (!data[i]["tags"].is_null())
				{
					auto tgass = data[i]["tags"];

					for (int i = 0; i < tgass.size(); i++)
					{
						if (!tgass[i].is_null())
						{
							m_serverlist_mode.tags.push_back(tgass[i].get<std::string>());
						}
						
					}


				}
			

				if (m_serverlist_mode.type == "shadowsocks")
				{
					if (!data[i]["cipher"].is_null())
					{
						m_serverlist_mode.method = data[i]["cipher"].get<std::string>();
					}
				}
			
				if (m_serverlist_mode.type == "v2ray")
				{
					if (!data[i]["tlsSettings"].is_null())
					{
						m_serverlist_mode.serverName = data[i]["tlsSettings"]["serverName"].get<std::string>();
						m_serverlist_mode.allowInsecure = data[i]["tlsSettings"]["allowInsecure"].get<std::string>();
					}

					if (!data[i]["tls"].is_null())
					{
						m_serverlist_mode.tls = data[i]["tls"].get<int>();
					}
					
				}
				
				if (m_serverlist_mode.type == "trojan") {
					if (!data[i]["server_name"].is_null())
					{
						m_serverlist_mode.serverName = data[i]["server_name"].get<std::string>();
						
					}

					if (!data[i]["allow_insecure"].is_null())
					{
						m_serverlist_mode.allowInsecure = std::to_string(data[i]["allow_insecure"].get<int>());
					}
				}
			
				vectlistmode.push_back(m_serverlist_mode);
			}

		
			/*for (auto it = vectlistmode.begin(); it != vectlistmode.end(); ++it)
			{

				if (CApp::getSingletonPtr()->GetSelectNodeById() != (*it).created_at)
				{
					CApp::getSingletonPtr()->SetSelectNodeById((*it).created_at);
					break;
				}
			}*/
			return true;

		}
		if (j["code"].get<int>() == 445 || j["code"].get<int>() == 446)
		{
		 
			this->m_error_msg = S_CA2W(j["message"].get<std::string>().c_str(), CP_UTF8).GetBuffer(0);
			return false;
		}
		else
		{
			this->m_error_msg = S_CA2W(j["message"].get<std::string>().c_str(), CP_UTF8).GetBuffer(0);
			return false;
		}


	}
	catch (...)
	{
		this->m_error_msg = L"解析数据错误,点击右上角刷新";
		return false;
	}
}

//bool GreaterSort(CServerListMode a, CServerListMode b)
//{
//	return a.online_users < b.online_users;
//}

void CServerList::MinimumSelectNode()
{

	/*if (vectlistmode.empty())
	{
		return;
	}
	auto t = vectlistmode;
	std::sort(t.begin(), t.end(), GreaterSort);

	if (!t.empty())
	{
		for each (CServerListMode itme in t)
		{
			if (itme.online_users > 0 && strcmp(itme.country_code.c_str(),"hk") == 0)
			{
				node_name = itme.name;
				break;
			}
		}
	}*/
}
