"""Test the OctoPrint buttons."""

from unittest.mock import patch

from pyoctoprintapi import OctoprintPrinterInfo
import pytest

from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.components.octoprint import OctoprintDataUpdateCoordinator
from homeassistant.components.octoprint.button import InvalidPrinterState
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry


@pytest.fixture
def platform() -> Platform:
    """Fixture to specify platform."""
    return Platform.BUTTON


async def test_pause_job(
    hass: HomeAssistant, init_integration: MockConfigEntry
) -> None:
    """Test the pause job button."""
    coordinator: OctoprintDataUpdateCoordinator = init_integration.runtime_data

    # Test pausing the printer when it is printing
    with patch("pyoctoprintapi.OctoprintClient.pause_job") as pause_command:
        coordinator.data["printer"] = OctoprintPrinterInfo(
            {"state": {"flags": {"printing": True}}, "temperature": []}
        )
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {
                ATTR_ENTITY_ID: "button.octoprint_pause_job",
            },
            blocking=True,
        )

        assert len(pause_command.mock_calls) == 1

    # Test pausing the printer when it is paused
    with patch("pyoctoprintapi.OctoprintClient.pause_job") as pause_command:
        coordinator.data["printer"] = OctoprintPrinterInfo(
            {"state": {"flags": {"printing": False, "paused": True}}, "temperature": []}
        )
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {
                ATTR_ENTITY_ID: "button.octoprint_pause_job",
            },
            blocking=True,
        )

        assert len(pause_command.mock_calls) == 0

    # Test pausing the printer when it is stopped
    with patch("pyoctoprintapi.OctoprintClient.pause_job") as pause_command:
        coordinator.data["printer"] = OctoprintPrinterInfo(
            {
                "state": {"flags": {"printing": False, "paused": False}},
                "temperature": [],
            }
        )
        with pytest.raises(InvalidPrinterState):
            await hass.services.async_call(
                BUTTON_DOMAIN,
                SERVICE_PRESS,
                {
                    ATTR_ENTITY_ID: "button.octoprint_pause_job",
                },
                blocking=True,
            )


async def test_resume_job(
    hass: HomeAssistant, init_integration: MockConfigEntry
) -> None:
    """Test the resume job button."""
    coordinator: OctoprintDataUpdateCoordinator = init_integration.runtime_data

    # Test resuming the printer when it is paused
    with patch("pyoctoprintapi.OctoprintClient.resume_job") as resume_command:
        coordinator.data["printer"] = OctoprintPrinterInfo(
            {"state": {"flags": {"printing": False, "paused": True}}, "temperature": []}
        )
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {
                ATTR_ENTITY_ID: "button.octoprint_resume_job",
            },
            blocking=True,
        )

        assert len(resume_command.mock_calls) == 1

    # Test resuming the printer when it is printing
    with patch("pyoctoprintapi.OctoprintClient.resume_job") as resume_command:
        coordinator.data["printer"] = OctoprintPrinterInfo(
            {"state": {"flags": {"printing": True, "paused": False}}, "temperature": []}
        )
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {
                ATTR_ENTITY_ID: "button.octoprint_resume_job",
            },
            blocking=True,
        )

        assert len(resume_command.mock_calls) == 0

    # Test resuming the printer when it is stopped
    with patch("pyoctoprintapi.OctoprintClient.resume_job") as resume_command:
        coordinator.data["printer"] = OctoprintPrinterInfo(
            {
                "state": {"flags": {"printing": False, "paused": False}},
                "temperature": [],
            }
        )
        with pytest.raises(InvalidPrinterState):
            await hass.services.async_call(
                BUTTON_DOMAIN,
                SERVICE_PRESS,
                {
                    ATTR_ENTITY_ID: "button.octoprint_resume_job",
                },
                blocking=True,
            )


async def test_stop_job(hass: HomeAssistant, init_integration: MockConfigEntry) -> None:
    """Test the stop job button."""
    coordinator: OctoprintDataUpdateCoordinator = init_integration.runtime_data

    # Test stopping the printer when it is paused
    with patch("pyoctoprintapi.OctoprintClient.cancel_job") as stop_command:
        coordinator.data["printer"] = OctoprintPrinterInfo(
            {"state": {"flags": {"printing": False, "paused": True}}, "temperature": []}
        )
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {
                ATTR_ENTITY_ID: "button.octoprint_stop_job",
            },
            blocking=True,
        )

        assert len(stop_command.mock_calls) == 1

    # Test stopping the printer when it is printing
    with patch("pyoctoprintapi.OctoprintClient.cancel_job") as stop_command:
        coordinator.data["printer"] = OctoprintPrinterInfo(
            {"state": {"flags": {"printing": True, "paused": False}}, "temperature": []}
        )
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {
                ATTR_ENTITY_ID: "button.octoprint_stop_job",
            },
            blocking=True,
        )

        assert len(stop_command.mock_calls) == 1

    # Test stopping the printer when it is stopped
    with patch("pyoctoprintapi.OctoprintClient.cancel_job") as stop_command:
        coordinator.data["printer"] = OctoprintPrinterInfo(
            {
                "state": {"flags": {"printing": False, "paused": False}},
                "temperature": [],
            }
        )
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {
                ATTR_ENTITY_ID: "button.octoprint_stop_job",
            },
            blocking=True,
        )

        assert len(stop_command.mock_calls) == 0


@pytest.mark.freeze_time("2023-01-01 00:00")
@pytest.mark.usefixtures("init_integration")
async def test_shutdown_system(hass: HomeAssistant) -> None:
    """Test the shutdown system button."""
    entity_id = "button.octoprint_shutdown_system"

    # Test shutting down the system
    with patch(
        "homeassistant.components.octoprint.coordinator.OctoprintClient.shutdown"
    ) as shutdown_command:
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {ATTR_ENTITY_ID: entity_id},
            blocking=True,
        )

        assert len(shutdown_command.mock_calls) == 1

        state = hass.states.get(entity_id)
        assert state
        assert state.state == "2023-01-01T00:00:00+00:00"


@pytest.mark.freeze_time("2023-01-01 00:00")
@pytest.mark.usefixtures("init_integration")
async def test_reboot_system(hass: HomeAssistant) -> None:
    """Test the reboot system button."""
    entity_id = "button.octoprint_reboot_system"

    # Test rebooting the system
    with patch(
        "homeassistant.components.octoprint.coordinator.OctoprintClient.reboot_system"
    ) as reboot_command:
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {
                ATTR_ENTITY_ID: entity_id,
            },
            blocking=True,
        )

        assert len(reboot_command.mock_calls) == 1

        state = hass.states.get(entity_id)
        assert state
        assert state.state == "2023-01-01T00:00:00+00:00"


@pytest.mark.freeze_time("2023-01-01 00:00")
@pytest.mark.usefixtures("init_integration")
async def test_restart_octoprint(hass: HomeAssistant) -> None:
    """Test the restart octoprint button."""
    entity_id = "button.octoprint_restart_octoprint"

    # Test restarting octoprint
    with patch(
        "homeassistant.components.octoprint.coordinator.OctoprintClient.restart"
    ) as restart_command:
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {
                ATTR_ENTITY_ID: entity_id,
            },
            blocking=True,
        )

        assert len(restart_command.mock_calls) == 1

        state = hass.states.get(entity_id)
        assert state
        assert state.state == "2023-01-01T00:00:00+00:00"
