"""Common fixtures and objects for the LG webOS integration tests."""
from unittest.mock import AsyncMock, Mock, patch

import pytest

from homeassistant.components.webostv.const import LIVE_TV_APP_ID
from homeassistant.helpers import entity_registry

from . import CHANNEL_1, CHANNEL_2, FAKE_UUID

from tests.common import async_mock_service


@pytest.fixture
def calls(hass):
    """Track calls to a mock service."""
    return async_mock_service(hass, "test", "automation")


@pytest.fixture(name="client")
def client_fixture():
    """Patch of client library for tests."""
    with patch(
        "homeassistant.components.webostv.WebOsClient", autospec=True
    ) as mock_client_class:
        client = mock_client_class.return_value
        client.hello_info = {"deviceUUID": FAKE_UUID}
        client.software_info = {"major_ver": "major", "minor_ver": "minor"}
        client.system_info = {"modelName": "TVFAKE"}
        client.client_key = "0123456789"
        client.apps = {
            LIVE_TV_APP_ID: {
                "title": "Live TV",
                "id": LIVE_TV_APP_ID,
                "largeIcon": "large-icon",
                "icon": "icon",
            },
        }
        client.inputs = {
            "in1": {"label": "Input01", "id": "in1", "appId": "app0"},
            "in2": {"label": "Input02", "id": "in2", "appId": "app1"},
        }
        client.current_app_id = LIVE_TV_APP_ID

        client.channels = [CHANNEL_1, CHANNEL_2]
        client.current_channel = CHANNEL_1

        client.volume = 37
        client.sound_output = "speaker"
        client.muted = False
        client.is_on = True

        async def mock_state_update_callback():
            await client.register_state_update_callback.call_args[0][0](client)

        client.mock_state_update = AsyncMock(side_effect=mock_state_update_callback)

        yield client


@pytest.fixture(name="client_entity_removed")
def client_entity_removed_fixture(hass):
    """Patch of client library, entity removed waiting for connect."""
    with patch(
        "homeassistant.components.webostv.WebOsClient", autospec=True
    ) as mock_client_class:
        client = mock_client_class.return_value
        client.hello_info = {"deviceUUID": "some-fake-uuid"}
        client.connected = False

        def mock_is_conneted():
            return client.connected

        client.is_connected = Mock(side_effect=mock_is_conneted)

        async def mock_conneted():
            ent_reg = entity_registry.async_get(hass)
            ent_reg.async_remove("media_player.webostv_some_secret")
            client.connected = True

        client.connect = AsyncMock(side_effect=mock_conneted)

        yield client