#!/usr/bin/perl

# Recommend albums whose tracks should be given ratings.  
# This means albums with a high total playcount which haven't
#  already been rated.  Typically, a "rated" album either has
#  at least one 4- or 5-star track, or lots of tracks with
#  at least some rating.  

use strict;
require 'parse_playlist.pm';
use Getopt::Std;
our ($opt_g, $opt_n, $opt_G);
getopts('g' . 'G:n:');
my $g_split_genres = $opt_g;
my $g_ignore_genre = $opt_G;
my $g_n_albums = $opt_n || 1;


my @g_tracks = &parse_playlist;
my @g_albums = &unrated_albums(@g_tracks);
my %g_genre_printed;
for (@g_albums)
{
	my ($album, $playcount, $genre) = @{$_};
	next	if	$genre eq $g_ignore_genre;
	my $this_genre = $g_split_genres? $genre:'ALL';
	#print join("\t", 
	printf '%15s %4s %s%s',
		$genre, $playcount, $album, "\n"
		unless	$g_genre_printed{$this_genre}++ >= $g_n_albums;
}

exit;

###  Main routines.  ###

sub unrated_albums
{
	my @tracks = @_;

	# Get the list of rated albums, and the play count for each album.
	my (%playcount, %rated, %genre);
	foreach my $t (@tracks)
	{
		my %t = %{$t};
		my $rating = int($t{'My Rating'}/20);
		my $aname = sprintf '%s (%s)', $t{'Album'}, $t{'Artist'};
		$playcount{$aname} += $t{'Play Count'}	unless	$rating;
		$rated{$aname}++	if	$rating >= 4;
		$genre{$aname} = $t{'Genre'};
	}

	return 
		map { [ $_, $playcount{$_}, $genre{$_} ] }
		sort { $playcount{$b}/($rated{$b}+1) <=> $playcount{$a}/($rated{$a}+1) } 
		#grep { not defined ($rated{$_}) }
		keys %playcount;
}
