from types import SimpleNamespace

from src.utils import display


def test_displays_keep_windows_number_and_capture_output(mocker):
    mocker.patch.object(display.win32api, 'EnumDisplayDevices', side_effect=[
        SimpleNamespace(
            DeviceName=r'\\.\DISPLAY1', DeviceString='AMD Display', StateFlags=5,
        ),
        SimpleNamespace(
            DeviceName=r'\\.\DISPLAY2', DeviceString='Intel Display', StateFlags=1,
        ),
        SimpleNamespace(
            DeviceName=r'\\.\DISPLAY10',
            DeviceString='USB Mobile Monitor Virtual Display', StateFlags=1,
        ),
        RuntimeError('done'),
    ])
    mocker.patch.object(display, '_currentMode', side_effect=[
        SimpleNamespace(
            Position_x=0, Position_y=0, PelsWidth=2560, PelsHeight=1440,
        ),
        SimpleNamespace(
            Position_x=2560, Position_y=0, PelsWidth=3840, PelsHeight=2160,
        ),
        SimpleNamespace(
            Position_x=6400, Position_y=0, PelsWidth=1920, PelsHeight=1080,
        ),
    ])

    displays = display.getDisplays()

    assert [item['number'] for item in displays] == [1, 2, 3]
    assert displays[2]['virtual'] is True
    assert displays[2]['outputIndex'] == 2


def test_selection_is_saved_outside_repository(tmp_path, mocker, monkeypatch):
    displays = [
        {'device': r'\\.\DISPLAY1', 'primary': True, 'virtual': False},
        {'device': r'\\.\DISPLAY3', 'primary': False, 'virtual': True},
    ]
    monkeypatch.setattr(display, 'SETTINGS_PATH', tmp_path / 'display.json')
    monkeypatch.setattr(display, 'LOCAL_ROOT', tmp_path)
    mocker.patch.object(display, 'getDisplays', return_value=displays)

    display.saveSelectedDisplay(r'\\.\DISPLAY3')

    assert display.getSelectedDisplay()['device'] == r'\\.\DISPLAY3'
