# Standard library imports
import unittest

# PyQt imports

# Third party imports

# App imports
from app import playlistmodel
from app import repository
from app.models import db


class MyTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        """Runs once before any test in this class"""

        pass

    @classmethod
    def tearDownClass(cls):
        """Runs once after all tests"""

        pass

    def setUp(self):
        """Runs before each test"""

        db.create_all()

        # Create a playlist and model
        playlist_name = "my playlist"
        self.playlist = repository.create_playlist(name=playlist_name, template_id=0)
        assert self.playlist
        self.model = playlistmodel.PlaylistModel(
            self.playlist.playlist_id, is_template=False
        )
        assert self.model

        # Create tracks
        track1_path = "testdata/isa.mp3"
        self.track1 = repository.create_track(track1_path)

        track2_path = "testdata/mom.mp3"
        self.track2 = repository.create_track(track2_path)

        # Add tracks and header to playlist
        self.row0 = repository.insert_row(
            self.playlist.playlist_id,
            row_number=0,
            track_id=self.track1.track_id,
            note="track 1",
        )
        self.row1 = repository.insert_row(
            self.playlist.playlist_id,
            row_number=1,
            track_id=0,
            note="Header row",
        )
        self.row2 = repository.insert_row(
            self.playlist.playlist_id,
            row_number=2,
            track_id=self.track2.track_id,
            note="track 2",
        )

    def tearDown(self):
        """Runs after each test"""

        db.drop_all()

    def test_add_track_to_header(self):
        """Add a track to a header row"""

        repository.add_track_to_header(self.row1.playlistrow_id, self.track2.track_id)
        result = repository.get_playlist_row(self.row1.playlistrow_id)
        assert result.track_id == self.track2.track_id
