from unittest.mock import Mock

import pytest

from fenrirscreenreader.core.eventData import FenrirEventType
from fenrirscreenreader.core.fenrirManager import FenrirManager


@pytest.mark.unit
def test_speech_history_plain_key_modal_command_is_dispatched():
    manager = FenrirManager.__new__(FenrirManager)
    manager.modifierInput = False
    manager.singleKeyCommand = False
    manager.command = ""

    event_manager = Mock(put_to_event_queue=Mock())
    input_manager = Mock(
        is_key_press=Mock(return_value=False),
        no_key_pressed=Mock(return_value=False),
        get_curr_shortcut=Mock(return_value=str([1, ["KEY_UP"]])),
        get_command_for_shortcut=Mock(return_value="SPEECH_HISTORY_PREV"),
    )
    speech_history_manager = Mock(is_active=Mock(return_value=True))
    diff_review_manager = Mock(is_active=Mock(return_value=False))
    vmenu_manager = Mock(get_active=Mock(return_value=False))

    manager.environment = {
        "input": {
            "key_forward": 0,
            "prev_input": ["KEY_UP"],
            "curr_input": ["KEY_UP"],
        },
        "runtime": {
            "InputManager": input_manager,
            "EventManager": event_manager,
            "VmenuManager": vmenu_manager,
            "DiffReviewManager": diff_review_manager,
            "SpeechHistoryManager": speech_history_manager,
        },
    }

    manager.detect_shortcut_command()

    event_manager.put_to_event_queue.assert_called_once_with(
        FenrirEventType.execute_command, "SPEECH_HISTORY_PREV"
    )


@pytest.mark.unit
def test_vmenu_plain_key_modal_command_is_dispatched():
    manager = FenrirManager.__new__(FenrirManager)
    manager.modifierInput = False
    manager.singleKeyCommand = False
    manager.command = ""

    event_manager = Mock(put_to_event_queue=Mock())
    input_manager = Mock(
        is_key_press=Mock(return_value=False),
        no_key_pressed=Mock(return_value=False),
        get_curr_shortcut=Mock(return_value=str([1, ["KEY_UP"]])),
        get_command_for_shortcut=Mock(return_value="PREV_VMENU_ENTRY"),
    )
    vmenu_manager = Mock(get_active=Mock(return_value=True))
    speech_history_manager = Mock(is_active=Mock(return_value=False))
    diff_review_manager = Mock(is_active=Mock(return_value=False))

    manager.environment = {
        "input": {
            "key_forward": 0,
            "prev_input": ["KEY_UP"],
            "curr_input": ["KEY_UP"],
        },
        "runtime": {
            "InputManager": input_manager,
            "EventManager": event_manager,
            "VmenuManager": vmenu_manager,
            "DiffReviewManager": diff_review_manager,
            "SpeechHistoryManager": speech_history_manager,
        },
    }

    manager.detect_shortcut_command()

    event_manager.put_to_event_queue.assert_called_once_with(
        FenrirEventType.execute_command, "PREV_VMENU_ENTRY"
    )
