# Updated by James Sutula (j2m2s (A_t) hotmail.com) September 17 2006
# Version 1.4
#
# This code is derived from code with the following copyright message:
#
# SlimServer Copyright (C) 2005 Sean Adams, Slim Devices Inc.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2.
# 
package Plugins::Transitions;

use strict;
use vars qw($VERSION);
use Slim::Player::ReplayGain;
$VERSION = substr( q$Revision: 1.4 $, 10 );

# USER CONFIG START

# Change these variables to get the desired behavior when playing an album
# or random tracks
# Key:
# transition_type: '0' = none, '1' = crossfade, '2' = fade in, '3' = fade out, '4' fade in & fade out
# transition_duration: (seconds)
my $album_transition_type = 0;
my $album_transition_duration = 0;
my $random_transition_type = 1;
my $random_transition_duration = 5;

# Specify the default transition behavior for cases when a track is playing with an excluded genre
# (see next section)
my $default_transition_type = 0;
my $default_transition_duration = 0;

# Optionally specifiy genres you would like to exclude from being processed
# For example, if I wanted to exclude any tracks with genre 'Jazz', 'Bluegrass', or 'Blues'  from having
# any transition behavior but the default, the following line would look like:
# my %exclude_genre = ('Jazz'=>'', 'Bluegrass'=>'', 'Blues'=>'');
my %exclude_genre = ();

# USER CONFIG END


sub getDisplayName {return 'PLUGIN_TRANSITIONS'}

sub strings() { return '
PLUGIN_TRANSITIONS
	EN	Dynamic Transition Updater

PLUGIN_TRANSITIONS_DESC
	EN	Changes the SB2+ between-track transition to your preference based on whether an album is playing.
'};

sub initPlugin {
	$::d_plugins && Slim::Utils::Misc::msg("Dynamic Transition Updater initialized.\n");
	Slim::Control::Request::subscribe(\&commandCallback, 
                                    [['playlist'], ['newsong']]);
}

sub shutdownPlugin {
	Slim::Control::Request::unsubscribe(\&commandCallback);
}

sub commandCallback {
	my $request = shift;
	my $client = $request->client();

	if (validGenre($client))
	{
		#if next or previous song is album match, use playing album options
		if (Slim::Player::ReplayGain->trackAlbumMatch($client, -1) || Slim::Player::ReplayGain->trackAlbumMatch($client, 1))
		{
			Slim::Utils::Prefs::clientSet($client,"transitionDuration",$album_transition_duration);
			Slim::Utils::Prefs::clientSet($client,"transitionType",$album_transition_type);
			$::d_plugins && Slim::Utils::Misc::msg("DTU: Using transition preferences for playing an album.\n");
		}
		#else use playing random tracks option
		else
		{
			Slim::Utils::Prefs::clientSet($client,"transitionDuration",$random_transition_duration);
			Slim::Utils::Prefs::clientSet($client,"transitionType",$random_transition_type);
			$::d_plugins && Slim::Utils::Misc::msg("DTU: Using transition preferences for random tracks.\n");
		}	
	}
	else
	{
		Slim::Utils::Prefs::clientSet($client,"transitionDuration",$default_transition_duration);
		Slim::Utils::Prefs::clientSet($client,"transitionType",$default_transition_type);
		$::d_plugins && Slim::Utils::Misc::msg("DTU: Genre for current or next track is excluded from transition processing. Using default transition preferences.\n");
	}
}

sub validGenre {
	my $client = shift;

	my $current_index = Slim::Player::Source::streamingSongIndex($client);
	my $current_fullpath = Slim::Player::Playlist::song($client, $current_index);
	my $current_track;
	my $current_genre;
	
	my $next_index = Slim::Player::Source::streamingSongIndex($client) + 1;
	my $next_fullpath = Slim::Player::Playlist::song($client, $next_index);
	my $next_track;
	my $next_genre;
	
	if (defined $current_fullpath)
	{
		$current_track    = Slim::Schema->resultset('Track')->objectForUrl($current_fullpath);
		$current_genre =  $current_track->genre();
		if (defined $current_genre && exists($exclude_genre{$current_genre}))
		{
			return 0;
		}
	}

	if (defined $next_fullpath)
	{
		$next_track    = Slim::Schema->resultset('Track')->objectForUrl($next_fullpath);
		$next_genre    = $next_track->genre();
		if (defined $next_genre && exists($exclude_genre{$next_genre}))
		{
			return 0;
		}
	}	
	
	# Genre not found in genre exclude list
	return 1;
}

1;
