import os.path

import helpers

from app.models import (
    NoteColours,
    Playdates,
    Playlists,
    Tracks,
)


def test_notecolours_get_colour(session):
    """Create a colour record and retrieve all colours"""

    print(">>>text_notcolours_get_colour")
    note_colour = "#0bcdef"
    NoteColours(session, substring="substring", colour=note_colour)

    records = NoteColours.get_all(session)
    assert len(records) == 1
    record = records[0]
    assert record.colour == note_colour


def test_notecolours_get_all(session):
    """Create two colour records and retrieve them all"""

    print(">>>text_notcolours_get_all")
    note1_colour = "#1bcdef"
    note2_colour = "#20ff00"
    NoteColours(session, substring="note1", colour=note1_colour)
    NoteColours(session, substring="note2", colour=note2_colour)

    records = NoteColours.get_all(session)
    assert len(records) == 2
    assert note1_colour in [n.colour for n in records]
    assert note2_colour in [n.colour for n in records]


def test_notecolours_get_colour_none(session):
    note_colour = "#3bcdef"
    NoteColours(session, substring="substring", colour=note_colour)

    result = NoteColours.get_colour(session, "xyz")
    assert result is None


def test_notecolours_get_colour_match(session):
    note_colour = "#4bcdef"
    nc = NoteColours(session, substring="sub", colour=note_colour)
    assert nc

    result = NoteColours.get_colour(session, "The substring")
    assert result == note_colour


def test_playdates_add_playdate(session, track1):
    """Test playdate and last_played retrieval"""

    playdate = Playdates(session, track1.id)
    assert playdate

    last_played = Playdates.last_played(session, track1.id)
    assert abs((playdate.lastplayed - last_played).total_seconds()) < 2


def test_playlist_create(session):
    playlist = Playlists(session, "my playlist")
    assert playlist


# def test_playlist_add_track(session, track):
#     # We need a playlist
#     playlist = Playlists(session, "my playlist")

#     row = 17

#     playlist.add_track(session, track.id, row)

#     assert len(playlist.tracks) == 1
#     playlist_track = playlist.tracks[row]
#     assert playlist_track.path == track_path


# def test_playlist_tracks(session):
#     # We need a playlist
#     playlist = Playlists(session, "my playlist")

#     # We need two tracks
#     track1_path = "/a/b/c"
#     track1_row = 17
#     track1 = Tracks(session, track1_path)

#     track2_path = "/x/y/z"
#     track2_row = 29
#     track2 = Tracks(session, track2_path)

#     playlist.add_track(session, track1.id, track1_row)
#     playlist.add_track(session, track2.id, track2_row)

#     tracks = playlist.tracks
#     assert tracks[track1_row] == track1
#     assert tracks[track2_row] == track2


# def test_playlist_notes(session):
#     # We need a playlist
#     playlist = Playlists(session, "my playlist")

#     # We need two notes
#     note1_text = "note1 text"
#     note1_row = 11
#     _ = Notes(session, playlist.id, note1_row, note1_text)

#     note2_text = "note2 text"
#     note2_row = 19
#     _ = Notes(session, playlist.id, note2_row, note2_text)

#     notes = playlist.notes
#     assert note1_text in [n.note for n in notes]
#     assert note1_row in [n.row for n in notes]
#     assert note2_text in [n.note for n in notes]
#     assert note2_row in [n.row for n in notes]


def test_playlist_open_and_close(session):
    # We need a playlist
    playlist = Playlists(session, "my playlist")

    assert len(Playlists.get_open(session)) == 0
    assert len(Playlists.get_closed(session)) == 1

    playlist.mark_open()

    assert len(Playlists.get_open(session)) == 1
    assert len(Playlists.get_closed(session)) == 0

    playlist.close()

    assert len(Playlists.get_open(session)) == 0
    assert len(Playlists.get_closed(session)) == 1


def test_playlist_get_all_and_by_id(session):
    # We need two playlists
    p1_name = "playlist one"
    p2_name = "playlist two"
    playlist1 = Playlists(session, p1_name)
    _ = Playlists(session, p2_name)

    all_playlists = Playlists.get_all(session)
    assert len(all_playlists) == 2
    assert p1_name in [p.name for p in all_playlists]
    assert p2_name in [p.name for p in all_playlists]
    assert session.get(Playlists, playlist1.id).name == p1_name


def test_tracks_get_all_tracks(session, track1, track2):
    # Need two tracks

    result = [a.path for a in Tracks.get_all(session)]
    assert track1.path in result
    assert track2.path in result


def test_tracks_by_path(session, track1):

    assert Tracks.get_by_path(session, track1.path) is track1


def test_tracks_by_id(session, track1):

    assert session.get(Tracks, track1.id) is track1


def test_tracks_search_artists(session, track1):
    track1_artist = "Fleetwood Mac"

    assert len(Tracks.search_artists(session, track1_artist)) == 1


def test_tracks_search_titles(session, track1):
    track1_title = "I'm So Afraid"

    assert len(Tracks.search_titles(session, track1_title)) == 1
