#!/bin/python3

import atexit, time

import mpd

from smarty import cmd_line_parser, library


def main():
	args = cmd_line_parser.get_args()
	library.set_args(args)

	client = mpd.MPDClient()
	client.connect(args.ip, args.port)
	library.set_client(client)

	atexit.register(library.restore_previous_settings, library.save_current_settings())
	library.disable_random()

	while True:
		pos, total = library.get_playlist_pos()
		if total > args.max_num:
			for i in range(total - args.max_num):
					library.rm_song(0)
		if pos != -1 and total - pos < args.songs_to_end:
			playlist = library.parse_playlist()
			next_genre = library.get_smart_genre(playlist)
			new_song = library.get_song(next_genre)
			if new_song == None:
					print("Skipping %s" % next_genre)
			else:
					print("Adding %s" % next_genre)
					library.add_song(library.get_song(next_genre))
			del playlist

			wait = 0.5
		else:
			wait = 10

		time.sleep(wait)

if __name__ == '__main__':
	main()
