#include "pihttpclient.h"

#include "curl_thread_pool_p.h"
#include "piliterals_bytes.h"
#include "piliterals_string.h"
#include "pisystemtime.h"

#include <curl/curl.h>

#if !defined(CURL_WRITEFUNC_ERROR)
#  define CURL_WRITEFUNC_ERROR 0xFFFFFFFF
#endif


int xfer_callback(void * ptr, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
	return reinterpret_cast<PIHTTPClientBase *>(ptr)->__infoFunc(dltotal, dlnow, ultotal, ulnow);
}

int debug_callback(CURL * handle, curl_infotype type, char * data, size_t size, void * ptr) {
	return reinterpret_cast<PIHTTPClientBase *>(ptr)->__debugFunc(type, data, size);
}


PRIVATE_DEFINITION_START(PIHTTPClient)
	CURLM * multi            = nullptr;
	CURL * handle            = nullptr;
	curl_slist * header_list = nullptr;
	bool isInit() const {
		return multi && handle;
	}
	bool init() {
		multi  = curl_multi_init();
		handle = curl_easy_init();
		if (!multi || !handle) {
			destroy();
			return false;
		}
		return true;
	}
	void destroy() {
		if (multi && handle) curl_multi_remove_handle(multi, handle);
		if (header_list) curl_slist_free_all(header_list);
		if (handle) curl_easy_cleanup(handle);
		if (multi) curl_multi_cleanup(multi);
		multi       = nullptr;
		handle      = nullptr;
		header_list = nullptr;
	}
PRIVATE_DEFINITION_END(PIHTTPClient)


PIHTTPClient::PIHTTPClient() {}


PIHTTPClient::~PIHTTPClient() {}


bool PIHTTPClient::init() {
	if (is_cancel) return false;
	CurlThreadPool::instance();
	if (!PRIVATE->init()) return false;
	auto ait = request.queryArguments().makeIterator();
	while (ait.next()) {
		if (!url.contains('?'))
			url.append('?');
		else
			url.append('&');
		url.append(ait.key().toPercentageEncoding());
		url.append('=');
		url.append(ait.value().toPercentageEncoding());
	}
	headers.clear();
	auto hit = request.headers().makeIterator();
	while (hit.next()) {
		headers << hit.key() + ": " + hit.value();
	}
	for (const auto & h: headers)
		PRIVATE->header_list = curl_slist_append(PRIVATE->header_list, h.dataAscii());
	curl_easy_setopt(PRIVATE->handle, CURLOPT_WRITEDATA, this);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_READDATA, this);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_XFERINFODATA, this);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_DEBUGDATA, this);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_HEADERDATA, this);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_WRITEFUNCTION, writeMemoryFunc);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_READFUNCTION, readMemoryFunc);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_XFERINFOFUNCTION, xfer_callback);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_DEBUGFUNCTION, debug_callback);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_HEADERFUNCTION, headerFunc);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_URL, url.dataUTF8());
	curl_easy_setopt(PRIVATE->handle, CURLOPT_CUSTOMREQUEST, PIHTTP::methodName(request.method()));
	curl_easy_setopt(PRIVATE->handle, CURLOPT_HTTPHEADER, PRIVATE->header_list);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_NOPROGRESS, 0L);
	// curl_easy_setopt(PRIVATE->handle, CURLOPT_VERBOSE, 1L);
	// curl_easy_setopt(PRIVATE->handle, CURLOPT_ERRORBUFFER, buffer_error.data());
	curl_easy_setopt(PRIVATE->handle, CURLOPT_SSL_VERIFYPEER, 0L);
	curl_easy_setopt(PRIVATE->handle, CURLOPT_SSL_VERIFYHOST, ignore_ssl_errors ? 0L : 1L);
	if (request.body().isNotEmpty()) {
		curl_easy_setopt(PRIVATE->handle, CURLOPT_UPLOAD, 1L);
		curl_easy_setopt(PRIVATE->handle, CURLOPT_INFILESIZE_LARGE, static_cast<curl_off_t>(request.body().size()));
	}

	return true;
}


void PIHTTPClient::perform() {
	if (!PRIVATE->isInit()) return;
	if (!is_cancel) {
		// piCout << "perform ...";
		PITimeMeasurer tm;
		// CURLcode res    = curl_easy_perform(PRIVATE->handle);

		curl_multi_add_handle(PRIVATE->multi, PRIVATE->handle);
		int running_cnt = 1;
		do {
			CURLMcode mc = curl_multi_perform(PRIVATE->multi, &running_cnt);
			// piCout << "curl_multi_perform" << mc << running_cnt;
			if (!mc) /* wait for activity, timeout or "nothing" */
				mc = curl_multi_poll(PRIVATE->multi, nullptr, 0, 50, nullptr);
			if (mc || is_cancel) {
				// piCout << "curl_multi_poll() failed";
				break;
			}

		} while (running_cnt > 0 && !is_cancel);

		if (is_cancel) {
			if (on_abort) on_abort(reply);
			PRIVATE->destroy();
			return;
		}

		CURLMsg * m  = nullptr;
		CURLcode res = (CURLcode)-1;
		do {
			int msgq = 0;
			m        = curl_multi_info_read(PRIVATE->multi, &msgq);
			if (m && (m->msg == CURLMSG_DONE)) {
				res = m->data.result;
				if (res == CURLE_OK) {
					reply.setBody(std::move(buffer_out));
					if (on_finish) on_finish(reply);
				} else {
					if (res == CURLE_ABORTED_BY_CALLBACK || is_cancel) {
						// piCerr << "curl_easy_perform() failed:" << curl_easy_strerror(res);
						if (on_abort) on_abort(reply);
					} else {
						last_error = curl_easy_strerror(res);
						if (on_error) on_error(reply);
					}
				}
				break;
			}
		} while (m && !is_cancel);

		if (res == (CURLcode)-1) {
			last_error = "CURL error";
			if (on_error) on_error(reply);
		}

		// piCout << "done" << (int)res << "in" << tm.elapsed_m() << ", bytes" << buffer_out.size();
	}
	// piCout << last_error;
	PRIVATE->destroy();
}


void PIHTTPClient::procHeaderLine(PIString & line) {
	if (line.startsWith("HTTP"_a)) {
		// HTTP/Версия КодСостояния Пояснение
		line.cutLeft(5);
		line.takeWord();
		int code = line.takeWord().toInt();
		// piCout << "code" << code;
		reply.setCode(static_cast<PIHTTP::Code>(code));
		return;
	}
	int ind = line.find(':');
	if (ind < 0) return;
	PIString hname = line.takeLeft(ind);
	line.cutLeft(1).trim();
	reply.addHeader(hname, line);
}


size_t PIHTTPClient::writeMemoryFunc(void * contents, size_t size, size_t nmemb, void * ptr) {
	size_t bytes = size * nmemb;
	// piCout << "writeMemoryFunc" << bytes;
	auto client  = reinterpret_cast<PIHTTPClient *>(ptr);
	if (client->is_cancel) return CURL_WRITEFUNC_ERROR;
	client->buffer_out.append(contents, bytes);
	return bytes;
}


size_t PIHTTPClient::readMemoryFunc(void * contents, size_t size, size_t nmemb, void * ptr) {
	size_t bytes = size * nmemb;
	// piCout << "readMemoryFunc" << bytes;
	auto client  = reinterpret_cast<PIHTTPClient *>(ptr);
	if (client->is_cancel) return CURL_READFUNC_ABORT;
	const auto & buffer(client->request.body());
	if (buffer.isEmpty()) return 0;
	// piCout << bytes;
	ssize_t ret = piClamp<ssize_t>(bytes, 0, buffer.size_s() - client->read_pos);
	if (ret < 0) ret = 0;
	if (ret > 0) memcpy(contents, buffer.data(client->read_pos), ret);
	return ret;
}


size_t PIHTTPClient::headerFunc(char * contents, size_t size, size_t nmemb, void * ptr) {
	size_t bytes = size * nmemb;
	auto client  = reinterpret_cast<PIHTTPClient *>(ptr);
	if (client->is_cancel) return CURL_WRITEFUNC_ERROR;
	PIString line = PIString::fromUTF8(contents, bytes).trim();
	if (line.isNotEmpty()) client->procHeaderLine(line);
	return bytes;
}


int PIHTTPClient::infoFunc(ssize_t dltotal, ssize_t dlnow, ssize_t ultotal, ssize_t ulnow) {
	// piCout << "infoFunc" << dltotal << dlnow << ultotal << ulnow;
	if (is_cancel) return 1;
	return 0;
}


int PIHTTPClient::debugFunc(int type, char * data, size_t size) {
	// piCout << "debugFunc" << type << PIString::fromUTF8(data, size);
	return 0;
}


PIHTTPClient * PIHTTPClient::create(const PIString & url_, PIHTTP::Method method, const PIHTTP::MessageConst & req) {
	PIHTTPClient * ret                                = new PIHTTPClient();
	static_cast<PIHTTP::MessageConst &>(ret->request) = req;
	ret->request.setMethod(method);
	ret->reply.setMethod(method);
	ret->url = url_;
	return ret;
}


PIHTTPClient * PIHTTPClient::onFinish(std::function<void()> f) {
	return onFinish([f](const PIHTTP::MessageConst &) { f(); });
}


PIHTTPClient * PIHTTPClient::onFinish(std::function<void(const PIHTTP::MessageConst &)> f) {
	on_finish = std::move(f);
	return this;
}


PIHTTPClient * PIHTTPClient::onError(std::function<void()> f) {
	return onError([f](const PIHTTP::MessageConst &) { f(); });
}


PIHTTPClient * PIHTTPClient::onError(std::function<void(const PIHTTP::MessageConst &)> f) {
	on_error = std::move(f);
	return this;
}


PIHTTPClient * PIHTTPClient::onAbort(std::function<void()> f) {
	return onAbort([f](const PIHTTP::MessageConst &) { f(); });
}


PIHTTPClient * PIHTTPClient::onAbort(std::function<void(const PIHTTP::MessageConst &)> f) {
	on_abort = std::move(f);
	return this;
}


PIHTTPClient * PIHTTPClient::ignoreSSLErrors() {
	ignore_ssl_errors = true;
	return this;
}


void PIHTTPClient::start() {
	CurlThreadPool::instance()->registerClient(this);
}


void PIHTTPClient::abort() {
	is_cancel = true;
	if (PRIVATE->isInit()) curl_multi_wakeup(PRIVATE->multi);
}


int PIHTTPClientBase::__infoFunc(ssize_t dltotal, ssize_t dlnow, ssize_t ultotal, ssize_t ulnow) {
	return reinterpret_cast<PIHTTPClient *>(this)->infoFunc(dltotal, dlnow, ultotal, ulnow);
}


int PIHTTPClientBase::__debugFunc(int type, char * data, size_t size) {
	return reinterpret_cast<PIHTTPClient *>(this)->debugFunc(type, data, size);
}
