#!/usr/bin/env python2

import sys

from soco import SoCo

v_commands = ['play', 'pause', 'stop', 'volume', 'eq', 'led', 'next', 'previous', 'info']
version = '1.0.1'
usage_shell = 'sonoshell [ip]'

if __name__ == '__main__':
    if(len(sys.argv) > 1 and sys.argv[1] == '-v'):
        print version
        sys.exit()

    if (len(sys.argv) < 3):
        print 'Sonoshell - version '+version
        print "Usage: sonoshell [-v|] [speaker's IP] [cmd]"
        print ""
        print "Valid commands: " + ', '.join(v_commands)
        sys.exit()

    speaker_ip = sys.argv[1]
    cmd = sys.argv[2].lower()
    param1 = ''
    param2 = ''

    if (len(sys.argv) > 3):
        param1 = sys.argv[3]
    if (len(sys.argv) > 4):
        param2 = sys.argv[4]

    sonos = SoCo(speaker_ip)

    if (cmd == 'play'):
        if(param1 != ''):
            sonos.play(param1)
        print sonos.play()
    elif (cmd == 'pause'):
        print sonos.pause()
    elif (cmd == 'volume'):
        if(param1 != ''):
            print param1
            sonos.volume(int(param1))
        else:
            print sonos.volume()
    elif (cmd == 'eq'):
        if(param1 == 'bass'):
            if(param2 != ''):
                sonos.bass(int(param2))
            else:
                print sonos.bass()
        elif(param1 == 'treble'):
            if(param2 != ''):
                sonos.treble(int(param2))
            else:
                print sonos.treble()
        else:
            print 'Usage: ' + usage_shell +' eq [bass|treble]'
    elif (cmd == 'stop'):
        print sonos.stop()
    elif (cmd == 'next'):
        print sonos.next()
    elif (cmd == 'previous'):
        print sonos.previous()
    elif (cmd == 'start_playlist'):
        print sonos.start_playlist()
    elif (cmd == 'set_playmode'):
        print sonos.set_playmode()
    elif (cmd == 'led'):
        if(param1 == '1' or param1 == '0'):
            if(param1 == '1'):
                sonos.status_light(True)
            else:
                sonos.status_light(False)
        else:
            print usage_shell + ' led 0|1'
    elif (cmd == 'info'):
        track = sonos.get_current_track_info()
        print track
        #print 'Artist: ' + track['artist'] + ', Current track:  ' + track['title'] + ', From album: ' + track['album'] + ', This is track number: ' + track['playlist_position'] + ' in the playlist. It is ' + track['duration'] + ' minutes long.'
    else:
        print "Unknown command \"" + cmd + "\""
        print "Valid commands: " + ', '.join(v_commands)
