#include "stdafx.h"
#include "CManageNetWork.h"
#include "CNetWork.h"
#include "CApp.h"
#include "FileOperate.h"
#include <nlohmann/json.hpp>

CManageNetWork::CManageNetWork() : m_base_curl(nullptr), m_Asyntask(4)
{
}

CManageNetWork::~CManageNetWork()
{
	if (m_base_curl)
	{
		delete m_base_curl;
		m_base_curl = nullptr;
	}
}

void CManageNetWork::init()
{


	SNotifyCenter::getSingleton().addEvent(EVENTID(EventLogin));
	SNotifyCenter::getSingleton().addEvent(EVENTID(EventDoWNload));


	if (!m_base_curl)
	{
		m_base_curl = new CNetWork();
		//m_base_curl.Attach(new CNetWork());
	}

	LoadFileToData();
}

void CManageNetWork::Login(LPCSTR username, LPCSTR password)
{
	if (!m_base_curl)
	{
		return;
	}

	this->m_username = username;
	this->m_password = password;

	m_Asyntask.AddTask(&CManageNetWork::ThreadFun_login, this, (LPARAM)m_base_curl);

	
	 
}

void CManageNetWork::SetUrl(LPCSTR url)
{
	if (!m_base_curl)
	{
		return;
	}

	m_base_curl->SetUrl(url);
}

SStringA CManageNetWork::GetUsername()
{
	return m_username;
}

SStringA CManageNetWork::GetPassWord()
{
	return m_password;
}

void CManageNetWork::DonloadConfg()
{
	if (!m_base_curl)
	{
		return;
	}
	m_Asyntask.AddTask(&CManageNetWork::ThreadFun_Dowlon_Config, this, (LPARAM)m_base_curl);
}

void CManageNetWork::ThreadFun_login(LPARAM lParam)
{
	IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
	int code = 200;
	SStringW msg = L"";
	std::string data;
	HTTPRET httpstatus = m_base_curl->PostLogin(this->m_username, this->m_password, data);

	if (httpstatus == http_f) {
		code = 0;
		msg = m_base_curl->GetLastErrorW();
	}

	if (!data.empty())
	{
		if (!CApp::getSingletonPtr()->DataToUserInfo(data))
		{
			code = -1;
			msg = CApp::getSingletonPtr()->GetUserinfo()->GetLastErrorW();
		}
	}


	DataToFile();

	EventLogin* pEvt = new EventLogin(nullptr);
	pEvt->status = code;
	pEvt->msg = msg;
	SNotifyCenter::getSingleton().FireEventAsync(pEvt);
	pEvt->Release();
}

void CManageNetWork::ThreadFun_Dowlon_Config(LPARAM lParam)
{
	IBaseCurl* lpAsyncParam = reinterpret_cast<IBaseCurl*>(lParam);
	int code = 200;
	SStringW msg = L"";
	std::string data;
	if (!m_base_curl->Download(CApp::getSingletonPtr()->GetUserinfo()->clash_config)) {
		code = 0;
		msg = L"ÏÂÔØÎÄ¼þÊ§°Ü";
	}
	EventDoWNload* pEvt = new EventDoWNload(nullptr);
	pEvt->status = code;
	pEvt->msg = msg;
	SNotifyCenter::getSingleton().FireEventAsync(pEvt);
	pEvt->Release();
}

void CManageNetWork::LoadFileToData() {
	FileOperate f;
	std::string path = std::filesystem::current_path().string();
	if (f.open(path + "\\user.json", std::fstream::in | std::fstream::out | std::fstream::app)) {
		std::string data = f.readAll();
		if (data.empty())
		{
			return;
		}
		nlohmann::json j3 = nlohmann::json::parse(data.begin(), data.end());
		this->m_username = j3["username"].get<std::string>().c_str();
		this->m_password = j3["password"].get<std::string>().c_str();

	}
	f.close();
}

void CManageNetWork::DataToFile()
{

	nlohmann::json j3;
	j3["username"] = this->m_username;
	j3["password"] = this->m_password;

	auto str = j3.dump(4);

	FileOperate f;

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

	f.create(path + "\\user.json");

	if (f.open(path + "\\user.json", std::fstream::in | std::fstream::out | std::fstream::app)) {
		f.write(str);
	}

	f.close();
}