"""Test the Fully Kiosk Browser buttons."""

from unittest.mock import MagicMock

from homeassistant.components import button
from homeassistant.components.fully_kiosk.const import DOMAIN
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er

from tests.common import MockConfigEntry


async def test_buttons(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
    device_registry: dr.DeviceRegistry,
    mock_fully_kiosk: MagicMock,
    init_integration: MockConfigEntry,
) -> None:
    """Test standard Fully Kiosk buttons."""
    mock_fully_kiosk.reset_mock()
    entry = entity_registry.async_get("button.amazon_fire_restart_browser")
    assert entry
    assert entry.unique_id == "abcdef-123456-restartApp"
    await hass.services.async_call(
        button.DOMAIN,
        button.SERVICE_PRESS,
        {ATTR_ENTITY_ID: "button.amazon_fire_restart_browser"},
        blocking=True,
    )
    assert len(mock_fully_kiosk.restartApp.mock_calls) == 1
    assert len(mock_fully_kiosk.getDeviceInfo.mock_calls) == 1
    assert len(mock_fully_kiosk.getSettings.mock_calls) == 1

    mock_fully_kiosk.reset_mock()
    entry = entity_registry.async_get("button.amazon_fire_restart_device")
    assert entry
    assert entry.unique_id == "abcdef-123456-rebootDevice"
    await hass.services.async_call(
        button.DOMAIN,
        button.SERVICE_PRESS,
        {ATTR_ENTITY_ID: "button.amazon_fire_restart_device"},
        blocking=True,
    )
    assert len(mock_fully_kiosk.rebootDevice.mock_calls) == 1
    assert len(mock_fully_kiosk.getDeviceInfo.mock_calls) == 1
    assert len(mock_fully_kiosk.getSettings.mock_calls) == 1

    mock_fully_kiosk.reset_mock()
    entry = entity_registry.async_get("button.amazon_fire_bring_to_foreground")
    assert entry
    assert entry.unique_id == "abcdef-123456-toForeground"
    await hass.services.async_call(
        button.DOMAIN,
        button.SERVICE_PRESS,
        {ATTR_ENTITY_ID: "button.amazon_fire_bring_to_foreground"},
        blocking=True,
    )
    assert len(mock_fully_kiosk.toForeground.mock_calls) == 1
    assert len(mock_fully_kiosk.getDeviceInfo.mock_calls) == 1
    assert len(mock_fully_kiosk.getSettings.mock_calls) == 1

    mock_fully_kiosk.reset_mock()
    entry = entity_registry.async_get("button.amazon_fire_send_to_background")
    assert entry
    assert entry.unique_id == "abcdef-123456-toBackground"
    await hass.services.async_call(
        button.DOMAIN,
        button.SERVICE_PRESS,
        {ATTR_ENTITY_ID: "button.amazon_fire_send_to_background"},
        blocking=True,
    )
    assert len(mock_fully_kiosk.toBackground.mock_calls) == 1
    assert len(mock_fully_kiosk.getDeviceInfo.mock_calls) == 1
    assert len(mock_fully_kiosk.getSettings.mock_calls) == 1

    mock_fully_kiosk.reset_mock()
    entry = entity_registry.async_get("button.amazon_fire_load_start_url")
    assert entry
    assert entry.unique_id == "abcdef-123456-loadStartUrl"
    await hass.services.async_call(
        button.DOMAIN,
        button.SERVICE_PRESS,
        {ATTR_ENTITY_ID: "button.amazon_fire_load_start_url"},
        blocking=True,
    )
    assert len(mock_fully_kiosk.loadStartUrl.mock_calls) == 1
    assert len(mock_fully_kiosk.getDeviceInfo.mock_calls) == 1
    assert len(mock_fully_kiosk.getSettings.mock_calls) == 1

    mock_fully_kiosk.reset_mock()
    entry = entity_registry.async_get("button.amazon_fire_clear_browser_cache")
    assert entry
    assert entry.unique_id == "abcdef-123456-clearCache"
    await hass.services.async_call(
        button.DOMAIN,
        button.SERVICE_PRESS,
        {ATTR_ENTITY_ID: "button.amazon_fire_clear_browser_cache"},
        blocking=True,
    )
    assert len(mock_fully_kiosk.clearCache.mock_calls) == 1
    assert len(mock_fully_kiosk.getDeviceInfo.mock_calls) == 1
    assert len(mock_fully_kiosk.getSettings.mock_calls) == 1

    mock_fully_kiosk.reset_mock()
    entry = entity_registry.async_get(
        "button.amazon_fire_trigger_motion_activity",
    )
    assert entry
    assert entry.unique_id == "abcdef-123456-triggerMotion"
    await hass.services.async_call(
        button.DOMAIN,
        button.SERVICE_PRESS,
        {ATTR_ENTITY_ID: "button.amazon_fire_trigger_motion_activity"},
        blocking=True,
    )
    assert len(mock_fully_kiosk.triggerMotion.mock_calls) == 1
    assert len(mock_fully_kiosk.getDeviceInfo.mock_calls) == 0
    assert len(mock_fully_kiosk.getSettings.mock_calls) == 0

    assert entry.device_id
    device_entry = device_registry.async_get(entry.device_id)
    assert device_entry
    assert device_entry.configuration_url == "http://192.168.1.234:2323"
    assert device_entry.entry_type is None
    assert device_entry.hw_version is None
    assert device_entry.identifiers == {(DOMAIN, "abcdef-123456")}
    assert device_entry.manufacturer == "amzn"
    assert device_entry.model == "KFDOWI"
    assert device_entry.name == "Amazon Fire"
    assert device_entry.sw_version == "1.42.5"
