#pragma once

#include "qt/MyQUI.h"
#include <spdlog/sinks/base_sink.h>



#include <spdlog/sinks/base_sink.h>
#include <QObject>

class QtLogSink : public QObject, public spdlog::sinks::base_sink<std::mutex>
{
	Q_OBJECT
public:
	QtLogSink(QObject* parent = nullptr) : QObject(parent) {}

protected:
	void sink_it_(const spdlog::details::log_msg& msg) override {
		spdlog::memory_buf_t formatted;
		spdlog::sinks::base_sink<std::mutex>::formatter_->format(msg, formatted);
		QString logMessage = QString::fromStdString(fmt::to_string(formatted));
		// 发射信号，传递日志消息
		emit logMessageReceived(logMessage);
	}

	void flush_() override {}

signals:
	void logMessageReceived(const QString& message);
};


class QWHome : public MyWidget
{
	Q_OBJECT
public:
	QWHome(QWidget* parent);
	~QWHome();

	void addPair(string name, QWidget* parent, int x, int y, string k, string v, bool readonly = true);


	// 创建自定义sink
	//std::shared_ptr<QtTextSink> qtSink;
	std::map<string, MyPairLabelLine> mapPairs;
	std::shared_ptr<QGroupBox> groupSys;
	std::shared_ptr<QGroupBox> groupHttp;
	std::shared_ptr<QGroupBox> groupMqtt;
	std::shared_ptr<QGroupBox> groupDB;
	std::shared_ptr<QTableWidget> table {};
	std::shared_ptr<QTextEdit> texteditLog;
	MyPairLabelLine logFilter {};
	std::shared_ptr<QPushButton> btnLogClean;
	//std::shared_ptr<QtLogSink> myqtSink;

	void onTimer() override;
	void onLogMessageReceived(const QString& message);

	void slotBtnClearLog();
};