from pygui.backend.models.frame import Frame
from pygui.backend.models.frame_player import FramePlayer, all_sensor_series


def frames(n):
    return [Frame(seq=i, time=float(i), values=[149.0 + i])for i in range(n)]

def test_loads_and_reports_total():
    p = FramePlayer(); p.load(frames(3))
    assert p.total == 3 and p.index == 0
    current = p.current()
    assert current is not None
    assert current.values == [149.0]

def test_step_forward_and_back_clamps():
    p = FramePlayer(); p.load(frames(3))
    assert p.step(1).index == 1
    assert p.step(1).index == 2
    assert p.step(1).index == 2
    assert p.step(-5).index == 0

def test_seek():
    p = FramePlayer(); p.load(frames(5))
    assert p.seek(3).index == 3
    assert p.seek(99).index == 4

def test_at_end():
    p = FramePlayer(); p.load(frames(2))
    assert not p.at_end
    p.seek(1)
    assert p.at_end

def test_frames_property_exposes_loaded_frames():
    p = FramePlayer(); p.load(frames(3))
    assert p.frames == tuple(frames(3))


# ---- all_sensor_series (Graph tab whole-run assembly) ----

def test_all_sensor_series_empty_frames():
    assert all_sensor_series([]) == ([], [])


def test_all_sensor_series_names_and_columns():
    fs = [
        Frame(seq=0, time=0.0, values=[150.0, 148.0]),
        Frame(seq=1, time=1.0, values=[151.0, 147.5]),
        Frame(seq=2, time=2.0, values=[152.0, 147.0]),
    ]
    names, series = all_sensor_series(fs)
    assert names == ["Sensor 1", "Sensor 2"]
    assert series == [[150.0, 151.0, 152.0], [148.0, 147.5, 147.0]]
