using System;
using SDL2;

namespace SlatedGameToolkit.Framework.Exceptions
{
    /// <summary>
    /// An SDLException is defined as an exception that is thrown whenever an error occurrs in any SDL functions.
    /// </summary>
    [Serializable]
    public abstract class SDLException : Exception {
        public string SDLMessage { get; }

        /// <summary>
        /// Creates an SDL exception.
        /// </summary>
        /// <param name="Fetch">Whether or not to fetch the last error message that occurred in SDL.</param>
        public SDLException(bool autoFlush = true) : base(autoFlush ? SDL.SDL_GetError() : "SDL error has occurred.") {
            if (autoFlush) {
                SDLMessage = SDL.SDL_GetError();
                SDL.SDL_ClearError();
            }
        }

        public SDLException(string message, bool autoFlush = true) : base(message + " (" + (autoFlush ? SDL.SDL_GetError() : "SDL error has occurred.") + ")") {
            if (autoFlush) {
                SDLMessage = SDL.SDL_GetError();
                SDL.SDL_ClearError();
            }
        }

        public SDLException(string message, Exception inner, bool autoFlush = true) : base(message + " (" + (autoFlush ? SDL.SDL_GetError() : "SDL error has occurred.") + ")", inner) {
            if (autoFlush) {
                SDLMessage = SDL.SDL_GetError();
                SDL.SDL_ClearError();
            }
        }
    }
}