#pragma once

// Returns true if mouse activation should be skipped when no SDL window exists.
// Host always has a window, so this guard is unnecessary — return false.
// Libretro returns true since it runs without an SDL window.
static inline bool osdep_platform_require_window_for_mouse()
{
	return false;
}

static inline bool osdep_platform_use_event_pump()
{
	return true;
}

static inline bool osdep_platform_should_delay_on_pause()
{
	return true;
}

static inline bool osdep_platform_init_sdl()
{
	if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD | SDL_INIT_EVENTS))
	{
		write_log("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
		int num = SDL_GetNumVideoDrivers();
		for (int i = 0; i < num; ++i) {
			write_log("Video Driver %d: %s\n", i, SDL_GetVideoDriver(i));
		}
		return false;
	}

	// Enable native IME for international text input
	SDL_SetHint(SDL_HINT_IME_IMPLEMENTED_UI, "1");

#ifdef __ANDROID__
	// OpenSL ES is more reliable than AAudio on older Android versions
	// (SDL3 has improved AAudio support, but OpenSL ES remains a safe fallback)
	SDL_SetHint("SDL_AUDIODRIVER", "openslES");
#endif

#ifdef __ANDROID__
	// Trap the Android back button so SDL delivers it as SDL_SCANCODE_AC_BACK
	// instead of letting the system handle it (which would exit/minimize the app)
	SDL_SetHint(SDL_HINT_ANDROID_TRAP_BACK_BUTTON, "1");
#endif

	(void)atexit(SDL_Quit);
	return true;
}

static inline void osdep_platform_shutdown_sdl()
{
	SDL_Quit();
}

static inline void osdep_platform_init_ui()
{
	normalcursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
	if (!normalcursor)
		normalcursor = SDL_GetDefaultCursor();
	clipboard_init();
}

static inline void osdep_platform_sync_keyboard_leds()
{
#if defined(__linux__)
	if (!amiberry_led_console_get_flags(&kbd_flags))
		return;
	if (!amiberry_led_console_get_leds(&kbd_led_status))
		return;
	if (kbd_flags & 07 & LED_CAP)
	{
		kbd_led_status |= LED_CAP;
		inputdevice_do_keyboard(AK_CAPSLOCK, 1);
	}
	else
	{
		kbd_led_status &= ~LED_CAP;
		inputdevice_do_keyboard(AK_CAPSLOCK, 0);
	}
	amiberry_led_console_set_leds(kbd_led_status);
#else
	int caps = SDL_GetModState();
	caps = caps & SDL_KMOD_CAPS;
	if (caps == SDL_KMOD_CAPS)
		kbd_led_status |= ~0x04;
	else
		kbd_led_status &= ~0x04;
#endif
}

static inline void osdep_platform_update_clipboard()
{
	auto* clipboard_uae = uae_clipboard_get_text();
	if (clipboard_uae) {
		SDL_SetClipboardText(clipboard_uae);
		uae_clipboard_free_text(clipboard_uae);
	}
}

static inline void osdep_platform_call_real_main(int argc, char** argv)
{
#ifdef __APPLE__
	SDL_PumpEvents();
	SDL_Event event;
	if (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_EVENT_DROP_FILE, SDL_EVENT_DROP_FILE) > 0)
	{
		write_log("Intercepted SDL_EVENT_DROP_FILE event: %s\n", event.drop.data);
		char** new_argv = new char*[argc + 2];
		for (int i = 0; i < argc; ++i)
		{
			new_argv[i] = argv[i];
		}
		new_argv[argc] = const_cast<char*>(event.drop.data);
		new_argv[argc + 1] = nullptr;
		real_main(argc + 1, new_argv);
		delete[] new_argv;
	}
	else
	{
		real_main(argc, argv);
	}
#else
	real_main(argc, argv);
#endif
}
