#
#	$Id: TopHits.pm,v 1.29 2004/12/30 02:48:22 kevin Exp $
#
#	Author: Kevin Walsh <kevin@cursor.biz>
#
#	Copyright (c) 2003-2004 Cursor Software Limited.
#	All rights reserved.
#
#	----------------------------------------------------------------------
#
#	Most requested tracks list.
#
#	Allows the user to browse the most popular tracks in the jukebox.
#	Note that the SLIMP3 server will clear the request counters every
#	time it (re)starts unless "savehistory" is set true in the preferences.
#
#	Scroll through the tracks using the up/down buttons.  Press 'play'
#	to play the current selection.  Press the 'add' key to add the
#	selection to the end of the current playlist.
#
#	Note that this module will NOT work with SLIMP3 servers older than
#	version 3.1b5.
#
#	----------------------------------------------------------------------
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#	02111-1307 USA
#
package Plugins::TopHits;
use Slim::Utils::Strings qw(string);
use strict;

use vars qw($VERSION);
$VERSION = substr(q$Revision: 1.29 $,10);

my %prefs = (
    'trackcount' => {
	order => 0,
	default => 40,
	widget => {
	    validate => \&Slim::Web::Setup::validateInt,
	    validateArgs => [1,100,1,100],
	},
    },
);

my @tracks;
my %current_track;
my %context;

my %functions = (
    'left' => sub {
	Slim::Buttons::Common::popModeRight(shift);
    },
    'right' => sub {
	my $client = shift;

	if (scalar(@tracks)) {
	    my @oldlines = Slim::Display::Display::curLines($client);

	    #
	    #	show the track information
	    #
	    Slim::Buttons::Common::pushMode(
		$client,
		'trackinfo',
		{ 'track' => $current_track{$client} },
	    );

	    #
	    #	push the old lines off the left side of the display
	    #
	    Slim::Display::Animation::pushLeft(
		$client,
		@oldlines,
		Slim::Display::Display::curLines($client),
	    );
	}
	else {
	    #
	    #	the list is empty so do nothing
	    #
	    Slim::Display::Animation::bumpRight($client);
	}
    },
    'up' => sub {
	my $client = shift;

	$context{$client} = Slim::Buttons::Common::scroll(
	    $client,
	    -1,
	    $#tracks + 1,
	    $context{$client},
	);
	$client->update();
    },
    'down' => sub {
	my $client = shift;

	$context{$client} = Slim::Buttons::Common::scroll(
	    $client,
	    1,
	    $#tracks + 1,
	    $context{$client},
	);
	$client->update();
    },
    'add' => sub {
	#
	#	add the selected track to the end of the current playlist
	#
	#	note that the [add] key is labeled as [rec] on the Sony remotes
	#
	my $client = shift;
	return unless exists($current_track{$client});

	Slim::Display::Animation::showBriefly($client,(
	    string('ADDING_TO_PLAYLIST'),
	    Slim::Music::Info::standardTitle($client,$current_track{$client}),
	));
	Slim::Player::Playlist::executecommand(
	    $client,
	    ['playlist','append',$current_track{$client}],
	);
    },
    'play' => sub {
	#
	#	replace the current playlist with the selected track and
	#	play it
	#
	my $client = shift;
	return unless exists($current_track{$client});

	Slim::Display::Animation::showBriefly($client,(
	    string('NOW_PLAYING_FROM'),
	    Slim::Music::Info::standardTitle($client,$current_track{$client}),
	));
	Slim::Player::Playlist::executecommand(
	    $client,
	    ['playlist','play',$current_track{$client}],
	);
    },
);

#
#	lines()
#	-------
#	Create and return the two-line display.
#
sub lines
{
    my $client = shift;

    load_preferences();

    @tracks = ();
    foreach (grep { $_->[0] !~ m|/__| } Slim::Web::History::recount()) {
	last if (push(@tracks,$_) >= $prefs{trackcount}->{current} && $prefs{trackcount}->{current});
    }

    $context{$client} = $#tracks if $context{$client} > $#tracks;

    my @lines;
    if (scalar(@tracks)) {
	my $track = $tracks[$context{$client}];
	$current_track{$client} = $track->[0];

	my $played;
	if ($track->[1] == 1) {
	    $played = string('PLUGIN_TOPHITS_PLAY_COUNT_1');
	}
	else {
	    $played = sprintf(
		string('PLUGIN_TOPHITS_PLAY_COUNT'),
		$track->[1],
	    );
	}

	@lines = (
	    (
		string('PLUGIN_TOPHITS_MODULE_NAME') .
		' (' .
		($context{$client} + 1) .
		' ' .
		string('OF') .
		' ' .
		($#tracks + 1) .
		") $played"
	    ),
	    Slim::Music::Info::standardTitle($client,$track->[0]),
	    undef,
	    Slim::Hardware::VFD::symbol('notesymbol'),
	);
    }
    else {
	@lines = (
	    string('PLUGIN_TOPHITS_MODULE_NAME'),
	    string('PLUGIN_TOPHITS_NO_TRACKS'),
	);
    }

    @lines;
}	

#
#	load_preferences()
#	------------------
#	Load the current preferences or set defaults
#
sub load_preferences
{
    while (my ($key,$val) = each %prefs) {
	my $name = __PACKAGE__;
	$name =~ s/^.*:://;
	$key = 'plugin_' . lc($name) . "_$key";

	if (Slim::Utils::Prefs::isDefined($key)) {
	    $val->{current} = Slim::Utils::Prefs::get($key);
	}
	else {
	    $val->{current} = $val->{default};
	    Slim::Utils::Prefs::set($key,$val->{default});
	}
    }
}

sub setupGroup
{
    my $name = __PACKAGE__;
    $name =~ s/^.*:://;
    $name = 'plugin_' . lc($name);

    load_preferences();

    my @order;
    push(@order,"${name}_$_") for (sort {$prefs{$a}->{order} <=> $prefs{$b}->{order}} keys %prefs);

    my $version = ' (' . string("${name}_VERSION") . ')';
    $version =~ s/%s/$VERSION/;
    $version =~ s/\s+\)/)/;

    my %group = (
	PrefOrder => \@order,
	GroupHead => string("${name}_MODULE_NAME") . $version,
	GroupDesc => string("${name}_MODULE_DESCRIPTION"),
	GroupLine => 1,
	GroupSub => 1,
	Suppress_PrefSub => 1,
	Suppress_PrefLine => 1,
    );

    my %widgets;
    while (my ($key,$val) = each %prefs) {
	$widgets{"${name}_$key"} = $val->{widget};

	if ($val->{translate} && exists($val->{widget}->{options})) {
	    $_ = string("${name}_$_") for (values %{$val->{widget}->{options}});
	    delete $val->{translate};
	}
    }

    return (\%group,\%widgets);
}

sub setMode
{
    my $client = shift;

    $context{$client} = 0;
    delete $current_track{$client};
    $client->lines(\&lines);
    $client->update();
}

sub getFunctions
{
    \%functions;
}

sub getDisplayName
{
    my $name = 'PLUGIN_TOPHITS_MODULE_NAME';

    $::VERSION =~ /^(\d+)/;
    return ($1 >= 6) ? $name : string($name);
}

sub strings
{
    local $/ = undef;
    <DATA>;
}

1;

__DATA__

PLUGIN_TOPHITS_MODULE_NAME
	EN	Top Hits

PLUGIN_TOPHITS_MODULE_DESCRIPTION
	EN	Lists the most popular tracks

PLUGIN_TOPHITS_VERSION
	EN	Version %s

PLUGIN_TOPHITS_NO_TRACKS
	EN	No tracks have been played

PLUGIN_TOPHITS_PLAY_COUNT_1
	EN	Played 1 time

PLUGIN_TOPHITS_PLAY_COUNT
	EN	Played %d times

SETUP_PLUGIN_TOPHITS_TRACKCOUNT
	EN	Maximum number of tracks to show in the list


