"""The tests for Octoptint binary sensor module."""

from datetime import UTC, datetime
from typing import Any

import pytest

from homeassistant.const import (
    STATE_UNAVAILABLE,
    STATE_UNKNOWN,
    Platform,
    UnitOfInformation,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

from . import DEFAULT_JOB


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


@pytest.fixture
def job() -> dict[str, Any]:
    """Job fixture."""
    return __standard_job()


@pytest.mark.parametrize(
    "printer",
    [
        {
            "state": {
                "flags": {"printing": True},
                "text": "Operational",
            },
            "temperature": {"tool1": {"actual": 18.83136, "target": 37.83136}},
        },
    ],
)
@pytest.mark.freeze_time(datetime(2020, 2, 20, 9, 10, 13, 543, tzinfo=UTC))
@pytest.mark.usefixtures("init_integration")
async def test_sensors(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test the underlying sensors."""
    state = hass.states.get("sensor.octoprint_job_percentage")
    assert state is not None
    assert state.state == "50"
    assert state.name == "OctoPrint Job Percentage"
    entry = entity_registry.async_get("sensor.octoprint_job_percentage")
    assert entry.unique_id == "Job Percentage-uuid"

    state = hass.states.get("sensor.octoprint_current_state")
    assert state is not None
    assert state.state == "operational"
    assert state.name == "OctoPrint Current State"
    entry = entity_registry.async_get("sensor.octoprint_current_state")
    assert entry.unique_id == "Current State-uuid"

    state = hass.states.get("sensor.octoprint_actual_tool1_temp")
    assert state is not None
    assert state.state == "18.83"
    assert state.name == "OctoPrint Actual tool1 temp"
    entry = entity_registry.async_get("sensor.octoprint_actual_tool1_temp")
    assert entry.unique_id == "actual tool1 temp-uuid"

    state = hass.states.get("sensor.octoprint_target_tool1_temp")
    assert state is not None
    assert state.state == "37.83"
    assert state.name == "OctoPrint Target tool1 temp"
    entry = entity_registry.async_get("sensor.octoprint_target_tool1_temp")
    assert entry.unique_id == "target tool1 temp-uuid"

    state = hass.states.get("sensor.octoprint_target_tool1_temp")
    assert state is not None
    assert state.state == "37.83"
    assert state.name == "OctoPrint Target tool1 temp"
    entry = entity_registry.async_get("sensor.octoprint_target_tool1_temp")
    assert entry.unique_id == "target tool1 temp-uuid"

    state = hass.states.get("sensor.octoprint_start_time")
    assert state is not None
    assert state.state == "2020-02-20T09:00:00+00:00"
    assert state.name == "OctoPrint Start Time"
    entry = entity_registry.async_get("sensor.octoprint_start_time")
    assert entry.unique_id == "Start Time-uuid"

    state = hass.states.get("sensor.octoprint_estimated_finish_time")
    assert state is not None
    assert state.state == "2020-02-20T10:50:00+00:00"
    assert state.name == "OctoPrint Estimated Finish Time"
    entry = entity_registry.async_get("sensor.octoprint_estimated_finish_time")
    assert entry.unique_id == "Estimated Finish Time-uuid"

    state = hass.states.get("sensor.octoprint_current_file")
    assert state is not None
    assert state.state == "Test_File_Name.gcode"
    assert state.name == "OctoPrint Current File"
    entry = entity_registry.async_get("sensor.octoprint_current_file")
    assert entry.unique_id == "Current File-uuid"

    state = hass.states.get("sensor.octoprint_current_file_size")
    assert state is not None
    assert state.state == "123.456789"
    assert state.attributes.get("unit_of_measurement") == UnitOfInformation.MEGABYTES
    assert state.name == "OctoPrint Current File Size"
    entry = entity_registry.async_get("sensor.octoprint_current_file_size")
    assert entry.unique_id == "Current File Size-uuid"


@pytest.mark.parametrize("job", [DEFAULT_JOB])
@pytest.mark.parametrize(
    "printer",
    [
        {
            "state": {
                "flags": {"printing": True, "paused": False},
                "text": "Operational",
            },
            "temperature": {"tool1": {"actual": 18.83136, "target": None}},
        },
    ],
)
@pytest.mark.freeze_time(datetime(2020, 2, 20, 9, 10, 0))
@pytest.mark.usefixtures("init_integration")
async def test_sensors_no_target_temp(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test the underlying sensors."""
    state = hass.states.get("sensor.octoprint_actual_tool1_temp")
    assert state is not None
    assert state.state == "18.83"
    assert state.name == "OctoPrint Actual tool1 temp"
    entry = entity_registry.async_get("sensor.octoprint_actual_tool1_temp")
    assert entry.unique_id == "actual tool1 temp-uuid"

    state = hass.states.get("sensor.octoprint_target_tool1_temp")
    assert state is not None
    assert state.state == STATE_UNKNOWN
    assert state.name == "OctoPrint Target tool1 temp"
    entry = entity_registry.async_get("sensor.octoprint_target_tool1_temp")
    assert entry.unique_id == "target tool1 temp-uuid"

    state = hass.states.get("sensor.octoprint_current_file")
    assert state is not None
    assert state.state == STATE_UNAVAILABLE
    assert state.name == "OctoPrint Current File"
    entry = entity_registry.async_get("sensor.octoprint_current_file")
    assert entry.unique_id == "Current File-uuid"

    state = hass.states.get("sensor.octoprint_current_file_size")
    assert state is not None
    assert state.state == STATE_UNAVAILABLE
    assert state.name == "OctoPrint Current File Size"
    entry = entity_registry.async_get("sensor.octoprint_current_file_size")
    assert entry.unique_id == "Current File Size-uuid"


@pytest.mark.parametrize(
    "printer",
    [
        {
            "state": {
                "flags": {"printing": False},
                "text": "Operational",
            },
            "temperature": {"tool1": {"actual": 18.83136, "target": None}},
        },
    ],
)
@pytest.mark.freeze_time(datetime(2020, 2, 20, 9, 10, 0))
@pytest.mark.usefixtures("init_integration")
async def test_sensors_paused(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test the underlying sensors."""
    state = hass.states.get("sensor.octoprint_start_time")
    assert state is not None
    assert state.state == STATE_UNKNOWN
    assert state.name == "OctoPrint Start Time"
    entry = entity_registry.async_get("sensor.octoprint_start_time")
    assert entry.unique_id == "Start Time-uuid"

    state = hass.states.get("sensor.octoprint_estimated_finish_time")
    assert state is not None
    assert state.state == STATE_UNKNOWN
    assert state.name == "OctoPrint Estimated Finish Time"
    entry = entity_registry.async_get("sensor.octoprint_estimated_finish_time")
    assert entry.unique_id == "Estimated Finish Time-uuid"


@pytest.mark.parametrize("printer", [None])
@pytest.mark.freeze_time(datetime(2020, 2, 20, 9, 10, 0))
@pytest.mark.usefixtures("init_integration")
async def test_sensors_printer_disconnected(
    hass: HomeAssistant,
    entity_registry: er.EntityRegistry,
) -> None:
    """Test the underlying sensors."""
    state = hass.states.get("sensor.octoprint_job_percentage")
    assert state is not None
    assert state.state == "50"
    assert state.name == "OctoPrint Job Percentage"
    entry = entity_registry.async_get("sensor.octoprint_job_percentage")
    assert entry.unique_id == "Job Percentage-uuid"

    state = hass.states.get("sensor.octoprint_current_state")
    assert state is not None
    assert state.state == STATE_UNAVAILABLE
    assert state.name == "OctoPrint Current State"
    entry = entity_registry.async_get("sensor.octoprint_current_state")
    assert entry.unique_id == "Current State-uuid"

    state = hass.states.get("sensor.octoprint_start_time")
    assert state is not None
    assert state.state == STATE_UNKNOWN
    assert state.name == "OctoPrint Start Time"
    entry = entity_registry.async_get("sensor.octoprint_start_time")
    assert entry.unique_id == "Start Time-uuid"

    state = hass.states.get("sensor.octoprint_estimated_finish_time")
    assert state is not None
    assert state.state == STATE_UNKNOWN
    assert state.name == "OctoPrint Estimated Finish Time"
    entry = entity_registry.async_get("sensor.octoprint_estimated_finish_time")
    assert entry.unique_id == "Estimated Finish Time-uuid"


def __standard_job():
    return {
        "job": {
            "averagePrintTime": 6500,
            "estimatedPrintTime": 6000,
            "filament": {"tool0": {"length": 3000, "volume": 7}},
            "file": {
                "date": 1577836800,
                "display": "Test File Name",
                "name": "Test_File_Name.gcode",
                "origin": "local",
                "path": "Folder1/Folder2/Test_File_Name.gcode",
                "size": 123456789,
            },
            "lastPrintTime": 12345.678,
            "user": "testUser",
        },
        "progress": {"completion": 50, "printTime": 600, "printTimeLeft": 6000},
        "state": "Printing",
    }
