def playlistOptionsWindow(playlist):
    playlist = db.getPlaylist(playlist)
    title = playlist.name + ' playlist options window'

    def optionsContent():
        def press(button):
            if button == 'play now':
                media.forcePlayPlaylist(playlist)
            elif button == 'add to queue':
                media.queuePlaylist(playlist)
            elif button == 'view songs':
                showPlaylistSongs(playlist)
            elif button == 'edit':
                playlistForm(playlist)
                # remember to edit bangericity and includeLogic too
            elif button == 'remove':
                confirmation = app.yesNoBox(
                    'remove playlist?',
                    'Are you sure you want to remove this playlist?')
                if confirmation is True:
                    db.removePlaylist(playlist)
                    updatePlaylistTable()
                    app.destroySubWindow(title)

        app.addButtons(
            ['play now', 'add to queue', 'view songs', 'edit', 'remove'],
            press)
        # maybe don't do make into a tag

    temporaryWindow(title, optionsContent)
def forcePlayPlaylist(playlist):
    playlist = db.getPlaylist(playlist)
    if len(songQueue) == 0:
        queuePlaylist(playlist)
    else:
        songs = pl.buildPlaylist(playlist)
        for song in songs:
            songQueue.insert(songIndex + 1, song)
            # order doesn't matter
        nextSong()
def showPlaylistSongs(playlist):
    playlist = db.getPlaylist(playlist)
    songs = list(pl.buildPlaylist(playlist))
    songNames = [song.name for song in songs]
    app.infoBox(playlist.name + ' songs', '\n'.join(songNames))
def playlistForm(playlist=None):
    '''can be used for creation and editing.
    if playlist is None, it goes to creation mode
    '''
    if playlist == 'newRow':
        playlist = None
    title = ''
    if playlist is not None:
        playlist = db.getPlaylist(playlist)
        title = 'edit playlist ' + playlist.name
    else:
        title = 'create playlist'

    def formContents():
        allTags = db.getAllTags()
        includedTags = []
        excludedTags = []
        if playlist is not None:
            includedTags = playlist.includedTags
            excludedTags = playlist.excludedTags
        includedTagDict = {}
        excludedTagDict = {}
        # both going to be {tagname => boolean, ...}
        for tag in allTags:
            includedTagDict[tag.name] = tag in includedTags
            excludedTagDict[tag.name] = tag in excludedTags
        if playlist is None:
            app.addLabel('playlist name:')
            app.entry('name')

        app.properties('included tags', includedTagDict)
        app.properties('excluded tags', excludedTagDict)
        app.addLabel('minimum and maximum bangericity:')
        app.addNumericEntry('minimum bangericity')
        app.addNumericEntry('maximum bangericity')
        min = 0
        max = 100
        if playlist is not None:
            min = playlist.minBangericity
            max = playlist.maxBangericity
        app.setEntry('minimum bangericity', min)
        app.setEntry('maximum bangericity', max)

        app.addCheckBox('use AND logic on includes')
        ticked = True
        if playlist is not None:
            ticked = playlist.andLogic
        app.setCheckBox('use AND logic on includes', ticked=ticked)

        def submit(button):
            if button == 'submit':
                # bangericity
                minBangericity = float(app.getEntry('minimum bangericity'))
                maxBangericity = float(app.getEntry('maximum bangericity'))
                # validate bangericities
                if not minBangericity < maxBangericity:
                    app.errorBox(
                        'invalid bangericities', 'minimum bangericity\
                     must be less than maximum bangericity')
                    return None
                # include logic
                andLogic = app.getCheckBox('use AND logic on includes')
                # tags
                newIncludedTags = []
                newExcludedTags = []
                includedTagDict = app.getProperties('included tags')
                excludedTagDict = app.getProperties('excluded tags')
                ##### left off here investigating bug where you can't edit then add
                # also label entries
                # figure out what the new tags will be
                for tag in includedTagDict:
                    if includedTagDict[tag] is True:
                        newIncludedTags.append(db.getTag(tag))
                    if excludedTagDict[tag] is True:
                        newExcludedTags.append(db.getTag(tag))
                # name
                if playlist is None:
                    name = app.getEntry('name')
                    # validate name
                    if name == '':
                        app.errorBox('invalid name',
                                     'playlist must have a name')
                        return None
                    existingPlaylistNames = [
                        playlist.name for playlist in db.getAllPlaylists()
                    ]
                    if name in existingPlaylistNames:
                        app.errorBox(
                            'duplicate name', 'a playlist with that\
                         name already exists')
                        return None
                    # create playlist
                    db.addPlaylist(name, newIncludedTags, newExcludedTags,
                                   minBangericity, maxBangericity, andLogic)
                    showPlaylistSongs(name)
                    updatePlaylistTable()
                    app.destroySubWindow(title)
                else:
                    # edit playlist
                    playlist.includedTags = newIncludedTags
                    playlist.excludedTags = newExcludedTags
                    playlist.minBangericity = minBangericity
                    playlist.maxBangericity = maxBangericity
                    playlist.andLogic = andLogic
                    showPlaylistSongs(playlist)
                    updatePlaylistTable()
                    app.destroySubWindow(title)

        app.addButton('submit', submit)

    temporaryWindow(title, formContents)
def queuePlaylist(playlist):
    playlist = db.getPlaylist(playlist)
    songs = pl.buildPlaylist(playlist)
    for song in songs:
        queue(song)
def queuePlaylist(playlist):
    playlist = db.getPlaylist(playlist)
    playlist = list(playlist)
    for song in pl.buildPlaylist(playlist):
        queue(song)
def queuePlaylist(playlist):
    playlist = db.getPlaylist(playlist)
    songs = pl.buildPlaylist(playlist)
    for song in songs:
        queue(song)
        # source = load(songPath(song))


def initialize():
    t = threading.Thread(target=loop)
    t.start()


if __name__ == '__main__':
    queuePlaylist(db.getPlaylist('test'))
    # queue(pl.buildPlaylist(db.getPlaylist('test')).pop())
    play()
    player.volume = .3
    # songIndex = len(songQueue) - 2
    # nextSong()
    seekRatio(.97)
    initialize()
# play = player.play
# pause = player.pause
# seek = player.seek
# next = player.next_source()

# ### testing eos ###
# queuePlaylist(db.getPlaylist('test'))
# player.play()