""" Spotify Tracker - Save every song you play from OS X to a playlist

Usage:
  spotifytracker setupwatcher
  spotifytracker setupfavorites
  spotifytracker watch [ [-V | --verbose] | [-Q | --quiet ]]
  spotifytracker addtofavorites [ [-V | --verbose] | [-Q | --quiet ] | [-S | --scripted ]]
  spotifytracker debug-refresh-token
  spotifytracker remove-old-tracks-watcher <days> [-D | --dryrun ] [ [-V | --verbose] | [-Q | --quiet ]]

Options:
  -V --verbose
  -Q --quiet
  -D --dryrun
"""
import logging
import sys

from docopt import docopt

from spotify_tracker.spotify_client import SpotifyPlaylistClient
from spotify_tracker.watcher_client import SpotifyWatcherClient
from spotify_tracker.favorites_client import SpotifyFavoritesClient


DEFAULT_LOG_LEVEL = logging.INFO


def main():
    arguments = docopt(__doc__)

    logging.basicConfig(
        format='%(asctime)s %(levelname)s: %(message)s')

    spotify_tracker_logger = logging.getLogger(name='spotify_tracker')
    base_logger = logging.getLogger()
    if arguments['--verbose']:
        base_logger.setLevel(level=logging.DEBUG)
    elif arguments['--quiet']:
        base_logger.setLevel(level=logging.WARNING)
    elif arguments['--scripted']:
        spotify_tracker_logger.setLevel(level=DEFAULT_LOG_LEVEL)
        ch = logging.StreamHandler(sys.stdout)
        ch.setLevel(DEFAULT_LOG_LEVEL)
        formatter = logging.Formatter('%(message)s')
        ch.setFormatter(formatter)
        spotify_tracker_logger.addHandler(ch)
    else:
        spotify_tracker_logger.setLevel(level=DEFAULT_LOG_LEVEL)

    if arguments['setupwatcher']:
        _spotify_client = SpotifyWatcherClient()
        _spotify_client.setup()
        return
    if arguments['setupfavorites']:
        _spotify_client = SpotifyFavoritesClient()
        _spotify_client.setup()
        return
    if arguments['watch']:
        _spotify_client = SpotifyWatcherClient()
        _spotify_client.watch()
        return
    if arguments['addtofavorites']:
        _spotify_client = SpotifyFavoritesClient()
        _spotify_client.safe_main()
        return
    if arguments['debug-refresh-token']:
        _spotify_client = SpotifyPlaylistClient()
        _spotify_client.save_token()
        return
    if arguments['remove-old-tracks-watcher']:
        _spotify_client = SpotifyWatcherClient()
        _spotify_client.remove_old_tracks_in_playlist(
            int(arguments['<days>']), arguments['--dryrun']
        )

if __name__ == "__main__":
    main()

@@ -272,20 +272,13 @@
 %7C %5B-
-S %7C --script
+-alfr
 ed %5D
@@ -1192,14 +1192,12 @@
 %5B'--
-script
+alfr
 ed'%5D
@@ -2025,32 +2025,119 @@
 voritesClient()%0A
+        if arguments%5B'--alfred'%5D:%0A            _spotify_client.main()%0A        else:%0A    
         _spotify

change argument --scripted to --alfred, don't run safe_main for alfred