#if BACKEND == SDL3

#include "backends/backend.h"
#include "state.h"
#include "mini/win/win.h"
#include "mini/surf/surf.h"

namespace backend
{
    state_t current_state = running;

    void init() {
        SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMEPAD);
        //video::init();
    }

    void quit() {
        SDL_Quit();
    }

    void poll_events() {
        //[TODO]
        //if (update_mode==UPDATE_WAIT)
        //    SDL_WaitEvent(NULL);
        //else if (update_mode==UPDATE_TIMEOUT)
        //    SDL_WaitEventTimeout(NULL, timeout);

        SDL_Event e;
        while(SDL_PollEvent(&e)) {
            if (e.type == SDL_EVENT_QUIT) {
                current_state=quitting;
                break;
            } 
            else if (e.type == SDL_EVENT_TEXT_INPUT) {
                SDL_strlcpy(input::key::text_input_buffer, e.text.text, sizeof(input::key::text_input_buffer));
                input::key::has_text_input = true;
            }
            else if (e.type == SDL_EVENT_KEY_DOWN) {
                #ifdef DEBUG
                    if (e.key.scancode == SDL_SCANCODE_F12) {
                        mini::surf::reloadsurfs();
                    //} else if (e.key.scancode == SDL_SCANCODE_F11) {
                    //    mini::lua::debug::toggle();
                    } else if (e.key.scancode == SDL_SCANCODE_F5) {
                        current_state=exiting;
                    } else {
                        input::key::just_pressed = e.key.scancode;
                    }
                #else
                    input::key::just_pressed = e.key.scancode;
                #endif
            }
            else if (e.type == SDL_EVENT_MOUSE_BUTTON_UP) {
                if (input::mouse::discard_buttons)
                    input::mouse::discard_buttons = false;
                else
                    if (e.button.clicks==2 && e.button.button==SDL_BUTTON_LEFT) {
                        input::mouse::double_click = true;
                    } else {
                        input::mouse::just_pressed = e.button.button;
                    }
            }
            else if (e.type == SDL_EVENT_MOUSE_WHEEL) {
                input::mouse::w = e.wheel.y;
            }
            else if (e.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
                input::pad::just_pressed = e.gbutton.button;
            }
        }

        input::key::keys = SDL_GetKeyboardState(NULL);

        // Update mouse
        float real_mouse_x, real_mouse_y;
        input::mouse::buttons = SDL_GetMouseState(&real_mouse_x, &real_mouse_y);
        float mx, my;
        SDL_RenderCoordinatesFromWindow(video::renderer, real_mouse_x, real_mouse_y, &mx, &my);
        input::mouse::x = int(mx/mini::win::state.zoom);
        input::mouse::y = int(my/mini::win::state.zoom);
    }

    const state_t& state() {
        return current_state;
    }

    char *clipboard() {
        return SDL_GetClipboardText();
    }

    void clipboard(const char* value) {
        SDL_SetClipboardText(value);
    }

}

#endif