from __future__ import unicode_literals

from mopidy import models

import spotify


def to_track(sp_track):
    if not sp_track.is_loaded:
        return

    if sp_track.error != spotify.ErrorType.OK:
        return

    if sp_track.availability != spotify.TrackAvailability.AVAILABLE:
        return

    # TODO artists
    # TODO album
    # TODO date from album
    # TODO bitrate

    return models.Track(
        uri=sp_track.link.uri,
        name=sp_track.name,
        length=sp_track.duration,
        track_no=sp_track.index)


def to_playlist(sp_playlist, folders=None, username=None):
    if not isinstance(sp_playlist, spotify.Playlist):
        return

    if not sp_playlist.is_loaded:
        return

    name = sp_playlist.name
    if name is None:
        name = 'Starred'
        # TODO Reverse order of tracks in starred playlists?
    if folders is not None:
        name = '/'.join(folders + [name])
    if username is not None and sp_playlist.owner.canonical_name != username:
        name = '%s by %s' % (name, sp_playlist.owner.canonical_name)

    tracks = [to_track(sp_track) for sp_track in sp_playlist.tracks]
    tracks = filter(None, tracks)

    return models.Playlist(
        uri=sp_playlist.link.uri,
        name=name,
        tracks=tracks)

@@ -139,33 +139,79 @@
 :%0A        return
+  # TODO Return placeholder %22%5Bloading%5D%22 track?
 %0A
-
 %0A    if sp_track
@@ -248,32 +248,76 @@
 :%0A        return
+  # TODO Return placeholder %22%5Berror%5D%22 track?
 %0A%0A    if sp_trac
@@ -371,16 +371,16 @@
 ILABLE:%0A
-
         
@@ -381,24 +381,74 @@
       return
+  # TODO Return placeholder %22%5Bunavailable%5D%22 track?
 %0A%0A    # TODO
@@ -846,24 +846,73 @@
       return
+  # TODO Return placeholder %22%5Bloading%5D%22 playlist?
 %0A%0A    name =

Add TODOs on how to expose non-playable tracks