from matterlab_valves import RunzeSelectionValve, create_runze_selection_sim_transport_factory
from matterlab_valves.runze_valve import RunzeMotorEventType


def test_sim_transport_reports_initial_port():
    transport_factory = create_runze_selection_sim_transport_factory(address=1, num_port=10, initial_port=2)
    valve = RunzeSelectionValve(com_port="SIM::runze-valve", address=1, num_port=10, transport_factory=transport_factory)

    assert valve.port == 2


def test_sim_transport_moves_and_homes_with_public_api():
    transport_factory = create_runze_selection_sim_transport_factory(address=1, num_port=10)
    valve = RunzeSelectionValve(
        com_port="SIM::runze-valve",
        address=1,
        num_port=10,
        ports={"home": 1, "collect": 2, "waste": 3},
        transport_factory=transport_factory,
    )

    valve.port = 3
    assert valve.port == 3

    valve.switch_port("home")
    assert valve.port == 1
    assert transport_factory.state.current_port == 1


def test_sim_transport_check_motor_uses_simulated_state():
    transport_factory = create_runze_selection_sim_transport_factory(
        address=1,
        num_port=10,
        motor_status=RunzeMotorEventType.Normal_status,
    )
    valve = RunzeSelectionValve(com_port="SIM::runze-valve", address=1, num_port=10, transport_factory=transport_factory)

    assert valve.check_motor() == RunzeMotorEventType.Normal_status.value


def test_sim_transport_reset_returns_home():
    transport_factory = create_runze_selection_sim_transport_factory(address=1, num_port=10, initial_port=4)
    valve = RunzeSelectionValve(com_port="SIM::runze-valve", address=1, num_port=10, transport_factory=transport_factory)

    valve.reset()

    assert valve.port == 1
    assert transport_factory.state.current_port == 1


def test_sim_transport_between_ports_tracks_state():
    transport_factory = create_runze_selection_sim_transport_factory(address=1, num_port=10, initial_port=4)
    valve = RunzeSelectionValve(com_port="SIM::runze-valve", address=1, num_port=10, transport_factory=transport_factory)

    valve.set_between_ports(4)

    assert transport_factory.state.between_ports_after == 4
