#!/usr/bin/env python

import sys
import signal
from src import lyrics
from src import songs
from src import threads


def usage():
    print 'cli-aoke'
    print 'Command Line Karaoke'
    print 'Version 1.1.0'
    print 'Usage ===>'
    print '           songs: lists the song files you can choose from'
    print '           sing:  starts a song!'


if __name__ == "__main__":
    if len(sys.argv) <= 1:
        usage()
    elif sys.argv[1] == "songs":
        songs.get()
    elif sys.argv[1] == "sing":
        if len(sys.argv) <= 2:
            print 'You did not pass a song file name.'
            print 'Run `cli-aoke songs` to get the list.'
            print 'Then run `cli-aoke sing <song_file_name>`.'
            sys.exit(0)

        song_file = sys.argv[2]
        if songs.exists(song_file) is False:
            print 'Sowwwwyyyyy'
            print '%s is not in the ~/.cli-aoke directory' % song_file
            sys.exit(0)

        print 'DJ cli-aoke on the request line.'
        print 'Bringing up the track...'

        print 'Fetching lyrics...'
        song_lyrics = lyrics.get(song_file)
        if song_lyrics is None:
            print 'Sowwwwyyyyy'
            print 'Search for %s returned zero results.' % song_file
            sys.exit(0)

        # setup graceful exit
        def Exit_gracefully(signal, frame):
            kar.song_thread.stop()
            kar.lyrics_thread.stop()
            # sys.exit(0)

        signal.signal(signal.SIGINT, Exit_gracefully)

        # run the song and lyrics thread
        kar = threads.Karaoke(song_file, song_lyrics)
    else:
        usage()
