"""Tests for platform/controls input mapping."""

import ctypes
from unittest.mock import MagicMock

import sdl2

from r36s_dlna_browser.platform.controls import Action, map_key


def _make_key_event(sym: int) -> sdl2.SDL_Event:
    event = sdl2.SDL_Event()
    event.type = sdl2.SDL_KEYDOWN
    event.key.keysym.sym = sym
    return event


def _make_button_event(button: int) -> sdl2.SDL_Event:
    event = sdl2.SDL_Event()
    event.type = sdl2.SDL_CONTROLLERBUTTONDOWN
    event.cbutton.button = button
    return event


class TestKeyMapping:
    def test_up(self):
        assert map_key(_make_key_event(sdl2.SDLK_UP)) == Action.UP

    def test_down(self):
        assert map_key(_make_key_event(sdl2.SDLK_DOWN)) == Action.DOWN

    def test_confirm_return(self):
        assert map_key(_make_key_event(sdl2.SDLK_RETURN)) == Action.CONFIRM

    def test_confirm_space(self):
        assert map_key(_make_key_event(sdl2.SDLK_SPACE)) == Action.CONFIRM

    def test_back_escape(self):
        assert map_key(_make_key_event(sdl2.SDLK_ESCAPE)) == Action.BACK

    def test_quit(self):
        assert map_key(_make_key_event(sdl2.SDLK_q)) == Action.QUIT

    def test_hud_mode(self):
        assert map_key(_make_key_event(sdl2.SDLK_h)) == Action.HUD_MODE

    def test_page_up(self):
        assert map_key(_make_key_event(sdl2.SDLK_PAGEUP)) == Action.PAGE_UP

    def test_page_down(self):
        assert map_key(_make_key_event(sdl2.SDLK_PAGEDOWN)) == Action.PAGE_DOWN

    def test_unmapped_key(self):
        assert map_key(_make_key_event(sdl2.SDLK_z)) is None

    def test_wrong_event_type(self):
        event = sdl2.SDL_Event()
        event.type = sdl2.SDL_MOUSEMOTION
        assert map_key(event) is None


class TestButtonMapping:
    def test_dpad_up(self):
        assert map_key(_make_button_event(sdl2.SDL_CONTROLLER_BUTTON_DPAD_UP)) == Action.UP

    def test_button_a(self):
        assert map_key(_make_button_event(sdl2.SDL_CONTROLLER_BUTTON_A)) == Action.CONFIRM

    def test_button_b(self):
        assert map_key(_make_button_event(sdl2.SDL_CONTROLLER_BUTTON_B)) == Action.BACK

    def test_start(self):
        assert map_key(_make_button_event(sdl2.SDL_CONTROLLER_BUTTON_START)) == Action.QUIT

    def test_left_shoulder(self):
        assert map_key(_make_button_event(sdl2.SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) == Action.PAGE_UP

    def test_button_y(self):
        assert map_key(_make_button_event(sdl2.SDL_CONTROLLER_BUTTON_Y)) == Action.HUD_MODE
