###############################################################################
#
# thumbnailプラグインの設定を行うアクションハンドラ
#
# ::default_size
# :::デフォルトサイズ名
# ::name
# :::サイズ名
# ::priority
# :::プライオリティ(max or min)
# ::ratio:
# :::縮小率(%)
# ::max
# :::最大長(px)
# ::x
# :::横辺長(px)
# ::y
# :::縦辺長(px)
#
###############################################################################
package plugin::thumbnail::Admin;
use strict;
use plugin::thumbnail::AdminTemplate;
use plugin::thumbnail::Delete;
use vars qw(@ISA);
@ISA = qw(Wiki::AdminTemplate);
#==============================================================================
# コンストラクタ
#==============================================================================
sub new {
	my $class = shift;
	my $self = new Wiki::AdminTemplate('thumbnail');
	$self->{action_name} = $plugin::thumbnail::Install::AdminHandler;
	$self->{default_value} = {
		default_size	=> "default",
		size		=> [{
			name		=> "default",
			priority	=> "max",
			ratio		=> "25",
			max		=> "120",
			x		=> "",
			y		=> "",
		}, {
			name		=> "large",
			priority	=> "max",
			ratio		=> "25",
			max		=> "300",
			x		=> "",
			y		=> "",
		}, {
			name		=> "small",
			priority	=> "min",
			ratio		=> "25",
			max		=> "80",
			x		=> "",
			y		=> "",
		}],
	};
	$self->{config_template} = {
		default_size	=> '^\w+$',
		size		=> {
			name		=> '^\w+$',
			priority	=> ["max", "min"],
			ratio		=> '^\d*$',
			max		=> '^\d*$',
			x		=> '^\d*$',
			y		=> '^\d*$',
		},
	};
	$self->{size_change_conf} = {};
	return bless $self,$class;
}

sub load_config {
	my $self = shift;
	$self->SUPER::load_config(@_);
	my %param = map {$_->{name} => $_} @{$self->{conf}->{size}};
	$self->{param} = \%param;
}

sub change_value {
	my $self = shift;
	my $key = shift;
	my $new = shift;
	my $old = shift;
	if($key =~ /^(size\d\d)(.*)/) {
		$self->{size_change_conf}->{$1} = $2 eq "_name" ? $old : "";
	}
}

sub after_save_hook {
	my $self = shift;
	my $wiki = shift;
	foreach my $key (keys %{$self->{old}}) {
		if($key =~ /^(size\d\d)_name$/) {
			$self->{size_change_conf}->{$1} = $self->{old}->{$key};
		}
	}
	foreach my $key (keys %{$self->{size_change_conf}}) {
		my $name = $self->{size_change_conf}->{$key} || $self->{flat}->{$key."_name"};
		plugin::thumbnail::Delete::delete_of_size($wiki, $name);
	}
	$self->{size_change_conf} = {};
}

1;
