#pragma once
#include <wx/event.h>
#include <wx/thread.h>
#include <RumPi/AlertManager.h>

///Custom event used to carry a finished log line from the AlertManager (worker thread) to the UI (main thread).
wxDECLARE_EVENT(wxEVT_RUMPI_LOGGER_UPDATE, wxThreadEvent);

/**
* Bridges the library's AlertManager to the wxWidgets event system: it subscribes to the AlertManager once and
* forwards every alert to the given wxEvtHandler as a wxEVT_RUMPI_LOGGER_UPDATE carrying a ready-to-display
* LoggerLineData. This keeps the AlertManager subscription and the worker->UI thread hop out of the view code.
*
* @author Eddie O'Hagan
* @date 8/13/2026
*/
class LoggerEventForwarder
{
protected:
	wxEvtHandler* myWxEventHandler;	///< The UI event handler that receives forwarded log events.

	///Handle for the AlertManager subscription, so the destructor can unsubscribe.
	std::size_t mySubscriptionId;

public:
	/**
	* Subscribes to the AlertManager and stores the given UI handler so every alert is forwarded to it. Keeps the
	* subscription handle so the destructor can unsubscribe.
	*
	* @author Eddie O'Hagan
	* @date 8/13/2026
	*
	* @code
	*	LoggerEventForwarder forwarder(this);
	* @endcode
	*
	* @param [in] handler The UI event handler to forward alerts to.
	*/
	LoggerEventForwarder(wxEvtHandler* handler);

	//Owns an AlertManager subscription (unsubscribed in the destructor), so a copy would double-unsubscribe and tear
	//down the original's live subscription. Non-copyable.
	LoggerEventForwarder(const LoggerEventForwarder&) = delete;
	LoggerEventForwarder& operator=(const LoggerEventForwarder&) = delete;

	/**
	* Unsubscribes from the AlertManager so a later alert can't call into a destroyed forwarder.
	*
	* @author Eddie O'Hagan
	* @date 8/13/2026
	*
	* @code
	*	//Runs when the forwarder is destroyed.
	* @endcode
	*/
	~LoggerEventForwarder();

	/**
	* Builds a log line (pure data) from the alert and queues it to the UI handler as a wxEVT_RUMPI_LOGGER_UPDATE, so
	* the actual UI update happens on the main thread.
	*
	* @author Eddie O'Hagan
	* @date 8/13/2026
	*
	* @code
	*	//Invoked by the AlertManager on whichever thread reported the alert.
	* @endcode
	*
	* @param [in] loggerEvent The alert event to forward.
	*/
	void HandleLoggerEvent(const RumPi::AlertEvent& loggerEvent);
};
