#
# ThemeManger is interface-class that handles themes(-plugins)
# 		written by ...: geronimo at vdrportal dot de
# 
# COPYRIGHT:	(c) 2006 Creative Commons by-nc-sa
#					some rights reserved. For details on copyright see:
#					http://creativecommons.org/licenses/by-nc-sa
#
# DISCLAIMER:	NO WARANTY AT ALL. Use at your own risk.
# 
# vi: ts=3
#
package V2I::ThemeManager;

use Data::Dumper;
use Carp qw(carp croak);
use strict;
use base 'V2I::PluginManager';

my $debug=0;

sub populate {
	my $self = shift;

	while(my $vn = shift){
		SWITCH: for($vn){			  
			/^-plugindir$/		and do { $self->{$vn} = shift; last SWITCH; }; 
			/^-appconfig$/		and do { $self->{$vn} = shift; last SWITCH; };
			/^-themeconfig$/	and do { $self->{$vn} = shift; last SWITCH; };
			croak('unknown parameter: '.$vn); 
			}
		}
}

sub initialize {
	my $self = shift;
	my $dp;
	my $base = $self->{'-plugindir'};

	$base .= '/' if not $base =~ /\/$/;
	$base .= 'V2I/plugins/';
   print('plugindir is ['.$base."]\n") if $debug;
	if(opendir($dp, $base)){
		$self->{'activethemes'} = {};	  
		while(my $de = readdir($dp)){
			if($de =~ /^Theme(.+?)\.pm$/ and -s $base.$de){
				my $pluginClass = $1;
				my $moduleName = 'V2I::plugins::Theme'.$pluginClass;

				print('check for Theme <'.$pluginClass.'>, ['.$moduleName."]\n") if $debug;
				eval "require $moduleName";
				if($moduleName->isa('V2I::Theme')){
					if(ref($self->{'-appconfig'}) eq 'HASH' and defined($self->{'-appconfig'}{$pluginClass})){
						$self->{'activethemes'}{$pluginClass} = $moduleName->new($self->{'-appconfig'}{$pluginClass});	  
						}
					elsif(ref($self->{'-themeconfig'}) eq 'HASH' and scalar(keys(%{$self->{'-themeconfig'}}))){
						$self->{'activethemes'}{$pluginClass} = $moduleName->new($self->{'-themeconfig'});	  
						if(ref($self->{'-appconfig'}) eq 'HASH'){
							$self->{'-appconfig'}{$pluginClass} = {};	  
							$self->{'activethemes'}{$pluginClass}->copyDefaults($self->{'-appconfig'}{$pluginClass});
							}
						}
					else{ $self->{'activethemes'}{$pluginClass} = $moduleName->new; }
					}
				else{ print("\t".'class <'.$moduleName.'> seems NOT to be a valid Theme!?!'."\n"); }	
				}
         elsif($debug){ print("\t".'no theme entry <'.$de.">\n"); }
			}
		closedir($dp);
		}
}

sub getPlugin {
	croak('please use the PluginManager to handle plugins!');	  
}

sub getTheme {
	my $self = shift;
	my $theme = shift;

	croak($theme.' is not an active theme!') if not defined($self->{'activethemes'}{$theme});

	return $self->{'activethemes'}{$theme};
}

sub getActiveThemes {
	my $self = shift;

	return keys(%{$self->{'activethemes'}});
}

sub isActiveTheme {
	my $self = shift;
	my $ut = shift;

	return exists(${$self->{'activethemes'}}{$ut});
}		  

sub saveConfig {
	my $self = shift;

	if(ref($self->{'-appconfig'}) eq 'HASH') {
		foreach my $themeName (keys(%{$self->{'activethemes'}})){
			my $theme = $self->{'activethemes'}{$themeName};

			$theme->saveConfig(\$self->{'-appconfig'}{$themeName});
			}
		}
	elsif(ref($self->{'-appconfig'}) eq 'REF'
	  and ref(${$self->{'-appconfig'}}) eq 'HASH'){
		foreach my $themeName (keys(%{$self->{'activethemes'}})){
			my $theme = $self->{'activethemes'}{$themeName};

			$theme->saveConfig(\${$self->{'-appconfig'}}->{$themeName});
			}
		}
}

1;
