#!/usr/bin/env python3
import os
import mpd

LEFT_BUTTON = '1'
MID_BUTTON = '2'
RIGHT_BUTTON = '3'
MW_UP = '4'
MW_DOWN = '5'

button = os.environ.get('BLOCK_BUTTON')

client = mpd.MPDClient(use_unicode=True)
client.connect('localhost', 6600)
status = client.status()['state']

if button == RIGHT_BUTTON:
    if status == 'play':
        client.pause()
    else:
        client.play()
elif button == MW_UP:
    client.next()
elif button == MW_DOWN:
    client.previous()

status = client.status()['state']
if status == 'stop':
    output = ' '
    short_output = output
else:
    if status == 'pause':
        output = ' '
    else:
        output = ' '
    short_output = output
    song = client.playlistid(client.status()['songid'])
    try:
        left = song[0]['albumartist']
    except KeyError:
        left = song[0]['artist']
    left = left.replace('&', 'and')
    if button == LEFT_BUTTON:
        right = song[0]['album'].replace('&', 'and')
    else:
        right = song[0]['title'].replace('&', 'and')
    output = ' - '.join([output, left, right])
    output += ' '
    short_output = ' - '.join([short_output, right])
    short_output += ' '

print(output)
print(short_output)
