#pragma once

#define SDL3 1
#define SFML 2
#define EMSCRIPTEN 3

#ifndef BACKEND
    #define BACKEND SDL3
#endif

#include <stdint.h>

namespace backend
{
    enum state_t { running=0, exiting=1, quitting=2 };
    extern state_t current_state;

    void init();
    void quit();
    void poll_events();
    const state_t& state();
    void exit();
    uint32_t get_time_ms();
    char* clipboard();
    void clipboard(const char* value);

    namespace video
    {
        void init();
        void quit();
        void render();
        void raise_window();
        void cursor(const bool value);
    }

    namespace input
    {
        void reset();

        namespace key
        {
            bool down(uint8_t i);
            bool press(uint8_t i);
            int press();
            bool any();
            void text(const bool enable);
            const char *utf8char();
        }

        namespace mouse
        {
            int posx();
            int posy();
            int wheel();
            bool down(uint8_t i);
            bool press(uint8_t i);
            bool dblclick();
            void discard();
            bool inside(int x, int y, int w, int h);
        }

        namespace pad
        {
            void init();
            bool down(int8_t i);
            bool press(int8_t i);
            int press();
        }
    }

    namespace audio
    {
        void init();
        void quit();

        namespace music
        {
            enum state { invalid, playing, paused, stopped, disabled };

            int load(const char* filename);
            int load(const uint8_t* buffer, uint32_t length);
            void play(int mus, int loop = -1);
            void pause();
            void resume();
            void stop();
            void fadeOut(int milliseconds);
            state getState();
            void destroy(int mus);
            float setVolume(float vol);
            void setPosition(float value);
            float getPosition();
            void enable(bool value);
            bool isEnabled();
        }

        namespace sound
        {
            int create(uint8_t* buffer, uint32_t length);
            int load(uint8_t* buffer, uint32_t length);
            int load(const char* filename);
            int play(int snd, int loop = 0);
            int playOnChannel(int snd, int chan, int loop = 0);
            void destroy(int snd);
            float setVolume(float vol);
            void enable(bool value);
            bool isEnabled();

            namespace channel
            {
                enum state { invalid, free, playing, paused, disabled };
                void pause(int chan);
                void resume(int chan);
                void stop(int chan);
                state getState(int chan);
            }
        }
    }

}
