#pragma once

#include "IBaseCurl.h"
#include <cpr/cpr.h>
#include "comm.h"

#define TIMEOUTE 60 * 1000

class CNetWork : public IBaseCurl
{
public:
	CNetWork();
	~CNetWork(void);

	void SetUrlArray(std::vector<ServerListUrl> data);

	HTTPRET GetServerNode(std::string& data);

	HTTPRET PostLogin(LPCSTR username, LPCSTR password, std::string& data);
	HTTPRET PostReg(LPCSTR username, LPCSTR password, std::string& data);

	HTTPRET GetSysConfigFromUser(LPCSTR username, LPCSTR password, std::string& data);

	HTTPRET GetSysConfig(std::string& data);

	void SetUrl(LPCSTR url);

	SStringA  GetLastErrorA();
	SStringW  GetLastErrorW();

	bool Download(std::string path);

	//
	HTTPRET Auth(std::string& data);
	HTTPRET Refresh(std::string& data);

	int  GetHttpStatus();
	HTTPRET Version(std::string& data);
private:
	std::string GetUrl(std::string path, std::vector<cpr::Parameter> parame, std::string token = "");
	std::string PostUrl(std::string path, std::vector<cpr::Pair> parame,std::string token = "");
	std::string Retrying(std::string path, std::vector<cpr::Parameter> parame, std::string token = "");
	void AutoToggleNode();
	void Init();
	void UnInit();
	std::string UpdateError(cpr::ErrorCode code,std::string msg);

	void initSessionPool(int szie);

	void DestoryOneConnSessionPool();

	std::shared_ptr<cpr::Session> GetSession();

	void pullSession(std::shared_ptr<cpr::Session> &p);

private:
	std::mutex m_mutx;
	std::vector<std::string> vectorBaseurl;
	std::vector<std::shared_ptr<cpr::Session>> m_session_vect;
	std::unordered_map<std::string, std::string> keymap;
	cpr::Session m_session;
	std::string m_error_msg;

	HTTPRET m_http_ret;

	int m_http_status;

	std::string m_url;

	std::vector<ServerListUrl> m_url_list;

};

