#include "stdafx.h"
#include "CLashConfig.h"
#include "CApp.h"
#include "comm.h"
#include "FileOperate.h"


CLashConfig::CLashConfig() : m_socks_port(9200), m_http_port(9300), m_c_port(9090), m_process(nullptr), m_Asyntask(1), m_is_qut(false), m_log(nullptr)
{
}

CLashConfig::~CLashConfig(void)
{
	if (m_process)
	{
		delete m_process;
		m_process = nullptr;
	}
	if (m_log)
	{
		delete m_log;
		m_log = nullptr;
	}
}

BOOL CLashConfig::MakeClash()
{
    return 0;
}

BOOL CLashConfig::InitClash()
{
	if (!m_process)
	{
		m_process = new CProcess();
	}
	
	m_http_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9300,9500);
	m_socks_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9600,9800);
	m_c_port = CTool::getSingletonPtr()->FindAvailableTcpPort(9900);

    std::string path = std::filesystem::current_path().string();

	std::string dir = CLASHCONFIGDIR;

	auto name_file = dir + "\\" + CLASHCONFIINIT;

	std::string logfile = path + "\\config\\log.log";
	if (m_log == nullptr)
	{
		m_log = new Logger(Logger::file_and_terminal, Logger::info, logfile);
	}
	

	if (CApp::getSingletonPtr()->GetSysMode() == PROXY_MODE::tun_mode) {
		name_file = dir + "\\" + CLASHCONFIINIT;
	}
	YAML::Node root;
	root["port"] = m_http_port;
	root["socks-port"] = m_socks_port;
	root["allow-lan"] = true;
	root["external-controller"] = "127.0.0.1:" + std::to_string(m_c_port);


	try
	{
		std::ofstream ofstream(name_file);
		ofstream << root << std::endl;
		ofstream.close();
		return TRUE;
	}
	catch (const std::exception&)
	{
		return FALSE;
	}


    return TRUE;
}

BOOL CLashConfig::StartClash()
{
	std::wstring path = std::filesystem::current_path().wstring();
	std::wstring config = path + L"\\" + WCLASHCONFIGDIR;
	//args.push_back("-d");
	////args.push_back(std::filesystem::current_path().string() + "\\route");
	//args.push_back(path + "\\config");
	//args.push_back("-f");
	//args.push_back(path + "\\config\\configinit.yaml");
	std::wstring run_config = config + L"\\" + S_CA2W(CLASHCONFIINIT).GetBuffer(0);
	SStringW path_config;
	path_config.Format(L"%s\\%s -d %s -f %s", config.c_str(),CLASHEXE,config.c_str(), run_config.c_str());
	if (m_process)
	{
		m_process->Create(CProcess::ASYNC);
		if (m_process->Execute(path_config.GetBuffer(0)))
		{
			m_Asyntask.AddTask(&CLashConfig::ThreadFun_process_Config, this, (LPARAM)NULL);
		}

		
	}
    return 0;
}

BOOL CLashConfig::StopClash()
{
	m_is_qut = true;
	m_Asyntask.Close();
	char ch[MAX_PATH];
	memset(ch, 0, MAX_PATH);
	sprintf_s(ch, "ok\n");
	m_process->WriteSome(ch, sizeof(ch));
    return 0;
}

YAML::Node CLashConfig::buildShadowsocks()
{
    return YAML::Node();
}

YAML::Node CLashConfig::buildtrojan()
{
    return YAML::Node();
}

YAML::Node CLashConfig::buildv2ray()
{
    return YAML::Node();
}

std::vector<YAML::Node> CLashConfig::buildv2rayHost()
{
    return std::vector<YAML::Node>();
}

YAML::Node CLashConfig::builTunConfig()
{
    return YAML::Node();
}

YAML::Node CLashConfig::buildDnsConfig()
{
    return YAML::Node();
}

std::vector<YAML::Node> CLashConfig::buildProxyGroups()
{
    return std::vector<YAML::Node>();
}

std::vector<YAML::Node> CLashConfig::buildRules()
{
    return std::vector<YAML::Node>();
}

void CLashConfig::ThreadFun_process_Config(LPARAM lParam)
{
	if (m_process)
	{
		char ch[MAX_PATH];
		memset(ch, 0, MAX_PATH);
		do
		{
			size_t bytesRead = m_process->ReadLine(ch, sizeof(ch));
			
			if (bytesRead == 0)
			{
				
				break;
			}

			if (m_is_qut)
			{
				
				break;
			}

			if (m_log)
			{
				std::string log(ch);
				m_log->INFO(log);
			}

			/*if (ch[bytesRead - 1] == '\n') break;*/
		} while (true);
		
	}
}
