#pragma once

#include "volatileMessages.h"

#include <any>

//--------------------------------------------------------------
class GenericMessageEvent : public sdi_toolBox::desktop::eventBus::Message
  , public BasicMessage
{
  using MessageTypeID = sdi_toolBox::desktop::eventBus::MessageTypeID;

  public:
  std::any m_payload;

  public:
  GenericMessageEvent()          = delete;
  virtual ~GenericMessageEvent() = default;
  explicit GenericMessageEvent(const MessageTypeID messageTypeID,
                               std::any            payload = {})
    : Message(messageTypeID)
    , m_payload(std::move(payload))
  {
  }

  // Return the payload as a specific type if possible, otherwise return an empty optional
  template<class T>
  [[nodiscard]] std::optional<T> getPayloadAs() const
  {
    if (m_payload.has_value())
    {
      try
      {
        return std::any_cast<T>(m_payload);
      }
      catch (const std::bad_any_cast &)
      {
        return {};
      }
    }
    return {};
  }

  // Return a string representation of the message
  std::string debug() const override
  {
    return "";
  }
};
//--------------------------------------------------------------
