import pytest

from bluetooth_mesh.messages.silvair.emergency_lighting import (
    ElState,
    EmergencyLightingParams,
    EmergencyLightingProperty,
    EmergencyLightingSubOpcode,
)

valid = [
    pytest.param(
        bytes.fromhex("00"),
        EmergencyLightingSubOpcode.EL_INHIBIT_ENTER,
        {},
    ),
    pytest.param(
        bytes.fromhex("01"),
        EmergencyLightingSubOpcode.EL_INHIBIT_ENTER_UNACKNOWLEDGED,
        {},
    ),
    pytest.param(
        bytes.fromhex("02"),
        EmergencyLightingSubOpcode.EL_INHIBIT_EXIT,
        {},
    ),
    pytest.param(
        bytes.fromhex("03"),
        EmergencyLightingSubOpcode.EL_INHIBIT_EXIT_UNACKNOWLEDGED,
        {},
    ),
    pytest.param(
        bytes.fromhex("04"),
        EmergencyLightingSubOpcode.EL_STATE_GET,
        {},
    ),
    pytest.param(
        bytes.fromhex("0500"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.TRANSITION_FROM_INHIBIT_TO_NORMAL,
        ),
    ),
    pytest.param(
        bytes.fromhex("0501"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.TRANSITION_FROM_FUNCTIONAL_TEST_TO_NORMAL,
        ),
    ),
    pytest.param(
        bytes.fromhex("0502"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.TRANSITION_FROM_DURATION_TEST_TO_NORMAL,
        ),
    ),
    pytest.param(
        bytes.fromhex("0503"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.NORMAL,
        ),
    ),
    pytest.param(
        bytes.fromhex("0504"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.TRANSITION_FROM_REST_TO_EMERGENCY,
        ),
    ),
    pytest.param(
        bytes.fromhex("0505"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.EMERGENCY,
        ),
    ),
    pytest.param(
        bytes.fromhex("0506"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.EXTENDED_EMERGENCY,
        ),
    ),
    pytest.param(
        bytes.fromhex("0507"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.TRANSITION_FROM_EMERGENCY_TO_REST,
        ),
    ),
    pytest.param(
        bytes.fromhex("0508"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.REST,
        ),
    ),
    pytest.param(
        bytes.fromhex("0509"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.TRANSITION_FROM_NORMAL_TO_INHIBIT,
        ),
    ),
    pytest.param(
        bytes.fromhex("050A"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.INHIBIT,
        ),
    ),
    pytest.param(
        bytes.fromhex("050B"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.TRANSITION_FROM_NORMAL_TO_DURATION_TEST,
        ),
    ),
    pytest.param(
        bytes.fromhex("050C"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.DURATION_TEST_IN_PROGRESS,
        ),
    ),
    pytest.param(
        bytes.fromhex("050D"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.TRANSITION_FROM_NORMAL_TO_FUNCTIONAL_TEST,
        ),
    ),
    pytest.param(
        bytes.fromhex("050E"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.FUNCTIONAL_TEST_IN_PROGRESS,
        ),
    ),
    pytest.param(
        bytes.fromhex("050F"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.BATTERY_DISCHARGED,
        ),
    ),
    pytest.param(
        bytes.fromhex("0510"),
        EmergencyLightingSubOpcode.EL_STATE_STATUS,
        dict(
            el_state=ElState.UNKNOWN,
        ),
    ),
    pytest.param(
        bytes.fromhex("0680FF"),
        EmergencyLightingSubOpcode.EL_PROPERTY_GET,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS,
        ),
    ),
    pytest.param(
        bytes.fromhex("0681FF"),
        EmergencyLightingSubOpcode.EL_PROPERTY_GET,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS_RANGE_MIN,
        ),
    ),
    pytest.param(
        bytes.fromhex("0682FF"),
        EmergencyLightingSubOpcode.EL_PROPERTY_GET,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS_RANGE_MAX,
        ),
    ),
    pytest.param(
        bytes.fromhex("0683FF"),
        EmergencyLightingSubOpcode.EL_PROPERTY_GET,
        dict(
            property_id=EmergencyLightingProperty.EL_PROLONG_TIME,
        ),
    ),
    pytest.param(
        bytes.fromhex("0780FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_SET,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS,
            el_lightness=dict(perceived_lightness=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0781FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_SET,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS_RANGE_MIN,
            el_lightness_range_min=dict(perceived_lightness=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0782FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_SET,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS_RANGE_MAX,
            el_lightness_range_max=dict(perceived_lightness=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0783FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_SET,
        dict(
            property_id=EmergencyLightingProperty.EL_PROLONG_TIME,
            el_prolong_time=dict(seconds=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0880FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_SET_UNACKNOWLEDGED,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS,
            el_lightness=dict(perceived_lightness=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0881FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_SET_UNACKNOWLEDGED,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS_RANGE_MIN,
            el_lightness_range_min=dict(perceived_lightness=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0882FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_SET_UNACKNOWLEDGED,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS_RANGE_MAX,
            el_lightness_range_max=dict(perceived_lightness=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0883FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_SET_UNACKNOWLEDGED,
        dict(
            property_id=EmergencyLightingProperty.EL_PROLONG_TIME,
            el_prolong_time=dict(seconds=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0980FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_STATUS,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS,
            el_lightness=dict(perceived_lightness=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0981FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_STATUS,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS_RANGE_MIN,
            el_lightness_range_min=dict(perceived_lightness=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0982FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_STATUS,
        dict(
            property_id=EmergencyLightingProperty.EL_LIGHTNESS_RANGE_MAX,
            el_lightness_range_max=dict(perceived_lightness=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0983FF3412"),
        EmergencyLightingSubOpcode.EL_PROPERTY_STATUS,
        dict(
            property_id=EmergencyLightingProperty.EL_PROLONG_TIME,
            el_prolong_time=dict(seconds=0x1234),
        ),
    ),
    pytest.param(
        bytes.fromhex("0A"),
        EmergencyLightingSubOpcode.EL_LAMP_OPERATION_TIME_GET,
        {},
    ),
    pytest.param(
        bytes.fromhex("0B"),
        EmergencyLightingSubOpcode.EL_LAMP_OPERATION_TIME_CLEAR,
        {},
    ),
    pytest.param(
        bytes.fromhex("0C"),
        EmergencyLightingSubOpcode.EL_LAMP_OPERATION_TIME_CLEAR_UNACKNOWLEDGED,
        {},
    ),
    pytest.param(
        bytes.fromhex("0D67452301EFCDAB89"),
        EmergencyLightingSubOpcode.EL_LAMP_OPERATION_TIME_STATUS,
        dict(
            el_lamp_total_operation_time=0x01234567,
            el_lamp_emergency_time=0x89ABCDEF,
        ),
    ),
    pytest.param(
        bytes.fromhex("0E"),
        EmergencyLightingSubOpcode.EL_REST_ENTER,
        {},
    ),
    pytest.param(
        bytes.fromhex("0F"),
        EmergencyLightingSubOpcode.EL_REST_ENTER_UNACKNOWLEDGED,
        {},
    ),
    pytest.param(
        bytes.fromhex("10"),
        EmergencyLightingSubOpcode.EL_REST_EXIT,
        {},
    ),
    pytest.param(
        bytes.fromhex("11"),
        EmergencyLightingSubOpcode.EL_REST_EXIT_UNACKNOWLEDGED,
        {},
    ),
]


@pytest.mark.parametrize("encoded, subopcode, payload", valid)
def test_parse(encoded, subopcode, payload):
    decoded = EmergencyLightingParams.parse(encoded)
    assert decoded.subopcode == subopcode
    assert decoded.payload == payload


@pytest.mark.parametrize("encoded, subopcode, payload", valid)
def test_build(encoded, subopcode, payload):
    assert EmergencyLightingParams.build(dict(subopcode=subopcode, payload=payload)) == encoded
