#include "libs/http_client/curl_thread_pool_p.h"
#include "picodeparser.h"
#include "pidigest.h"
#include "pihttpclient.h"
#include "piliterals.h"
#include "pimqttclient.h"
#include "pip.h"
#include "piunits.h"
#include "pivaluetree_conversions.h"

using namespace PICoutManipulators;
using namespace PIHTTP;


PIKbdListener kbd;

MessageMutable createMessage(Code c, const char * path, const MessageConst & msg) {
	piCout << "path" << path << "args" << msg.pathArguments();
	return MessageMutable().setCode(c);
};
int main(int argc, char * argv[]) {
	// piCout << "start ...";
	// PIHTTPServer server;
	// server.registerUnhandled([](const MessageConst & msg) { return createMessage(Code::BadRequest, "unhadled", msg); });
	// server.registerPath("api/v1/status", Method::Get, [](const MessageConst & msg) {
	// 	return createMessage(Code::Accepted, "api/v1/status", msg);
	// });
	// server.registerPath("api/v1/plugins", Method::Get, [](const MessageConst & msg) {
	// 	return createMessage(Code::Accepted, "api/v1/plugins", msg);
	// });
	// server.registerPath("api/v1/task-status", Method::Get, [](const MessageConst & msg) {
	// 	return createMessage(Code::Accepted, "api/v1/task-status", msg);
	// });
	// server.registerPath("api/v1/task/{taskID}/status", Method::Get, [](const MessageConst & msg) {
	// 	return createMessage(Code::Accepted, "api/v1/task/{taskID}/status", msg);
	// });
	// server.registerPath("api/v1/bort/list", Method::Get, [](const MessageConst & msg) {
	// 	return createMessage(Code::Accepted, "api/v1/bort/list", msg);
	// });
	// server.registerPath("api/v1/all", Method::Get, [](const MessageConst & msg) {
	// 	return createMessage(Code::Accepted, "api/v1/all", msg);
	// });
	// server.registerPath("api/v1/all/bort{A}/f", Method::Get, [](const MessageConst & msg) {
	// 	return createMessage(Code::Accepted, "api/v1/all/*/f", msg);
	// });
	// server.registerPath("api/v1/all2/**", Method::Get, [](const MessageConst & msg) {
	// 	return createMessage(Code::Accepted, "api/v1/all2/**", msg);
	// });
	// server.listenAll(12345);

	// kbd.enableExitCapture('Q');
	// WAIT_FOR_EXIT
	// piCout << "exiting ...";
	// server.stop();

	// return 0;

	// PISystemMonitor mon;
	// mon.startOnSelf();
	// PISystemMonitor::totalRAM();
	// 2_s .sleep();

	// return 0;
	PIMQTT::Client cl;
	cl.setConnectTimeout(2_s);

	cl.subscribe("api/v1/all/bort{A}/f",
				 [](const PIMQTT::MessageConst & msg) { piCout << "1" << msg.topicList() << msg.pathArguments() << msg.body().size(); });
	cl.subscribe("api/v1/all/task{T}/f",
				 [](const PIMQTT::MessageConst & msg) { piCout << "2" << msg.topicList() << msg.pathArguments() << msg.body().size(); });
	cl.subscribe("api/v1/all/*/f",
				 [](const PIMQTT::MessageConst & msg) { piCout << "3" << msg.topicList() << msg.pathArguments() << msg.body().size(); });
	cl.subscribe("api/v1/all2/**",
				 [](const PIMQTT::MessageConst & msg) { piCout << "4" << msg.topicList() << msg.pathArguments() << msg.body().size(); });
	CONNECTL(&cl, connected, [&cl] {
		piCout << "connected";
		// cl.subscribe("api/v1/plugins");
		// cl.subscribe("api/v1/task-status");
		// cl.subscribe("api/v1/*/{taskID}/status");
		// cl.subscribe("api/v1/bort/list");
		// cl.subscribe("api/v1/all");
		// cl.subscribe("/zigbee2mqtt");
		// cl.subscribe("/zigbee2mqtt/+/status/");
		// cl.subscribe("/zigbee2mqtt/*/status/");
		// cl.subscribe("test/#");
		// cl.subscribe("#");
		// cl.unsubscribe("/zigbee2mqtt");
		// cl.unsubscribe("api/v1/all/bort{A}/f");
		// cl.unsubscribe("api/v1/all/*/f");
		// cl.publish("/zigbee2mqtt/abc", "hello from PIP"_a.toAscii());
	});
	CONNECTL(&cl, disconnected, [&cl](PIMQTT::Error code) {
		piCout << "disconnected code" << (int)code;
		cl.connect("localhost", "PIP");
	});
	CONNECTL(&cl, receivedUnhandled, [](const PIMQTT::MessageConst & message) {
		piCout << "receivedUnhandled" << message.topic() << message.pathArguments() << message.payload().size();
	});

	cl.connect("localhost", "PIP");

	piSleep(6.);
	cl.unsubscribeAll();

	kbd.enableExitCapture('Q');
	WAIT_FOR_EXIT

	piCout << "exiting ...";
}
