"""Tests for remote module."""
from unittest.mock import Mock, call, patch

from samsungtvws.remote import SamsungTVWS


def test_send_key(connection: Mock) -> None:
    """Ensure simple data can be parsed."""
    open_response = (
        '{"data": {"token": 123456789}, "event": "ms.channel.connect", "from": "host"}'
    )

    connection.recv.side_effect = [open_response]
    tv = SamsungTVWS("127.0.0.1")
    tv.send_key("KEY_POWER")
    connection.send.assert_called_once_with(
        '{"method": "ms.remote.control", "params": {'
        '"Cmd": "Click", '
        '"DataOfCmd": "KEY_POWER", '
        '"Option": "false", '
        '"TypeOfRemote": "SendRemoteKey"'
        "}}"
    )


def test_app_list(connection: Mock) -> None:
    """Ensure valid app_list data can be parsed."""
    open_response = (
        '{"data": {"token": 123456789}, "event": "ms.channel.connect", "from": "host"}'
    )
    app_list_response = '{"data":{"data":[{"appId":"111299001912","app_type":2,"icon":"/opt/share/webappservice/apps_icon/FirstScreen/111299001912/250x250.png","is_lock":0,"name":"YouTube"},{"appId":"3201608010191","app_type":2,"icon":"/opt/share/webappservice/apps_icon/FirstScreen/3201608010191/250x250.png","is_lock":0,"name":"Deezer"}]},"event":"ed.installedApp.get","from":"host"}'

    connection.recv.side_effect = [open_response, app_list_response]
    tv = SamsungTVWS("127.0.0.1")
    assert tv.app_list() == [
        {
            "appId": "111299001912",
            "app_type": 2,
            "icon": "/opt/share/webappservice/apps_icon/FirstScreen/111299001912/250x250.png",
            "is_lock": 0,
            "name": "YouTube",
        },
        {
            "appId": "3201608010191",
            "app_type": 2,
            "icon": "/opt/share/webappservice/apps_icon/FirstScreen/3201608010191/250x250.png",
            "is_lock": 0,
            "name": "Deezer",
        },
    ]


def test_app_list_invalid(connection: Mock) -> None:
    """Ensure simple data can be parsed."""
    open_response = (
        '{"data": {"token": 123456789}, "event": "ms.channel.connect", "from": "host"}'
    )
    app_list_response = '{"data": 200, "event": "ed.apps.launch", "from": "host"}'

    connection.recv.side_effect = [open_response, app_list_response]
    tv = SamsungTVWS("127.0.0.1")
    assert tv.app_list() is None
    connection.send.assert_called_once_with(
        '{"method": "ms.channel.emit", "params": {"event": "ed.installedApp.get", "to": "host"}}'
    )


def test_send_hold_key(connection: Mock) -> None:
    """Ensure simple data can be parsed."""
    open_response = (
        '{"data": {"token": 123456789}, "event": "ms.channel.connect", "from": "host"}'
    )
    connection.recv.side_effect = [open_response]

    tv = SamsungTVWS("127.0.0.1")
    with patch("samsungtvws.connection.time.sleep") as patch_sleep:
        tv.hold_key("KEY_POWER", 3)

    assert patch_sleep.call_count == 3
    assert patch_sleep.call_args_list == [call(1), call(3), call(1)]

[((14, 9, 14, 33), 'samsungtvws.remote.SamsungTVWS', 'SamsungTVWS', ({(14, 21, 14, 32): '"""127.0.0.1"""'}, {}), "('127.0.0.1')", False, 'from samsungtvws.remote import SamsungTVWS\n'), ((34, 9, 34, 33), 'samsungtvws.remote.SamsungTVWS', 'SamsungTVWS', ({(34, 21, 34, 32): '"""127.0.0.1"""'}, {}), "('127.0.0.1')", False, 'from samsungtvws.remote import SamsungTVWS\n'), ((61, 9, 61, 33), 'samsungtvws.remote.SamsungTVWS', 'SamsungTVWS', ({(61, 21, 61, 32): '"""127.0.0.1"""'}, {}), "('127.0.0.1')", False, 'from samsungtvws.remote import SamsungTVWS\n'), ((75, 9, 75, 33), 'samsungtvws.remote.SamsungTVWS', 'SamsungTVWS', ({(75, 21, 75, 32): '"""127.0.0.1"""'}, {}), "('127.0.0.1')", False, 'from samsungtvws.remote import SamsungTVWS\n'), ((76, 9, 76, 51), 'unittest.mock.patch', 'patch', ({(76, 15, 76, 50): '"""samsungtvws.connection.time.sleep"""'}, {}), "('samsungtvws.connection.time.sleep')", False, 'from unittest.mock import Mock, call, patch\n'), ((80, 42, 80, 49), 'unittest.mock.call', 'call', ({(80, 47, 80, 48): '(1)'}, {}), '(1)', False, 'from unittest.mock import Mock, call, patch\n'), ((80, 51, 80, 58), 'unittest.mock.call', 'call', ({(80, 56, 80, 57): '(3)'}, {}), '(3)', False, 'from unittest.mock import Mock, call, patch\n'), ((80, 60, 80, 67), 'unittest.mock.call', 'call', ({(80, 65, 80, 66): '(1)'}, {}), '(1)', False, 'from unittest.mock import Mock, call, patch\n')]