#pragma once

#include <SDL3/SDL.h>
#include <memory>
#include <stdexcept>

namespace quokka_gfx
{
//--------------------------------------------------------------
class SDLContext
{
  public:
  using pContext = std::shared_ptr<SDLContext>;     // Shared pointer type for SDLContext
  static pContext GetContext();                     // Get (create if necessary) the static singleton instance of SDLContext

  public:
  virtual ~SDLContext();                                          // Default destructor
  SDLContext(const SDLContext &obj)                = default;     // Copy constructor
  SDLContext(SDLContext &&obj) noexcept            = default;     // Move constructor
  SDLContext &operator=(const SDLContext &obj)     = default;     // Copy assignment operator
  SDLContext &operator=(SDLContext &&obj) noexcept = default;     // Move assignment operator

  protected:
  SDLContext();                                     // Default constructor
  static inline pContext m_singleton = nullptr;     // Shared pointer to the singleton instance of SDLContext
};
//--------------------------------------------------------------
/* Get (create if necessary) the static singleton instance of SDLContext */
inline SDLContext::pContext SDLContext::GetContext()
{
  if (!m_singleton)
    m_singleton = pContext(new SDLContext());

  return m_singleton;
}
//--------------------------------------------------------------
/* Default constructor */
inline SDLContext::SDLContext()
{
  // Initialize SDL with audio, video, and events subsystems
  if (!SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_EVENTS))
    throw std::runtime_error(SDL_GetError());
}
//--------------------------------------------------------------
/* Default destructor */
inline SDLContext::~SDLContext()
{
  // Quit SDL subsystems
  SDL_Quit();
}
//--------------------------------------------------------------
}     // namespace quokka_gfx
