"""
CSCI 145 Day 18 - Songs and playlists (solution)
"""


# Activity C

class Song:
    """A song in a music app."""

    def __init__(self, title, artist, seconds):
        """
        Make a new song.

        Parameters:
            title (str): the title
            artist (str): who wrote or performs it
            seconds (int): its length in seconds
        """
        self.title = title
        self.artist = artist
        self.seconds = seconds

    def __str__(self):
        """
        Returns:
            str: the song as a string, such as "Clair de Lune by Debussy"
        """
        return self.title + " by " + self.artist

    def length(self):
        """
        Returns:
            str: the length as minutes and two-digit seconds, such as
            "5:03" for 303 seconds
        """
        minutes = self.seconds // 60
        secs = self.seconds % 60
        if secs < 10:
            return str(minutes) + ":0" + str(secs)
        return str(minutes) + ":" + str(secs)

    def longer_than(self, other):
        """
        Parameters:
            other (Song): another song

        Returns:
            bool: True if this song is longer than other. Otherwise, False.
        """
        return self.seconds > other.seconds


# On your own

class Playlist:
    """A named list of songs."""

    def __init__(self, name):
        """
        Make a new, empty playlist.

        Parameters:
            name (str): the playlist's name
        """
        self.name = name
        self.songs = []

    def add(self, song):
        """
        Add song to the end of the playlist. Changes the playlist.

        Parameters:
            song (Song): the song to add

        Returns: None
        """
        self.songs.append(song)

    def total_seconds(self):
        """
        Returns:
            int: the total length of all the songs, in seconds
        """
        total = 0
        for song in self.songs:
            total += song.seconds
        return total

    def longest(self):
        """
        Returns:
            Song: the longest song in the playlist. If the playlist is
            empty, returns None. If two songs tie, returns the first.
        """
        if len(self.songs) == 0:
            return None
        best = self.songs[0]
        for song in self.songs:
            if song.longer_than(best):
                best = song
        return best

    def __str__(self):
        """
        Challenge.

        Returns:
            str: the name and a colon, then each song on its own line,
            indented two spaces (see the tests at the bottom)
        """
        text = self.name + ":"
        for song in self.songs:
            text = text + "\n  " + str(song)
        return text


if __name__ == "__main__":
    # Activity C
    moon = Song("Clair de Lune", "Debussy", 303)
    elise = Song("Fur Elise", "Beethoven", 175)
    print(moon)                    # Clair de Lune by Debussy
    print(moon.length())           # 5:03
    print(elise.length())          # 2:55
    print(moon.longer_than(elise))  # True
    print(elise.longer_than(moon))  # False

    # On your own
    practice = Playlist("Practice")
    print(practice.longest())      # None
    practice.add(elise)
    practice.add(moon)
    practice.add(Song("Maple Leaf Rag", "Joplin", 186))
    print(practice.total_seconds())  # 664
    print(practice.longest())      # Clair de Lune by Debussy
    print(practice)
    # Practice:
    #   Fur Elise by Beethoven
    #   Clair de Lune by Debussy
    #   Maple Leaf Rag by Joplin
