package MpmasEUCAP2003;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use MpmasInputfile;
use MpmasInputBlock;
use IndexedTable;
our @ISA = qw(MpmasInputfile);

sub filename {
	my $self = shift;
	return "EUCAP2003";
}

sub required {
	my $self = shift;
	return ($self->config_entry("POLICY_EUCAP2003"));
}
sub prepare_shared {
	my ($self) = @_;
	$self->set_config_entry("POLICY_MODEL", $self->config_entry("POLICY_MODEL") + 4);
}
sub prepare_special {
	my ($self) = @_;
	
	my $general = MpmasInputBlock->new(3,10);
	$general->add_row("","DISTRIBUTION OF DIRECT PAYMENT ENTITLEMENTS FOLLOWING the EU CAP MidTermReview 2003");
	$general->add_row("1", "General parameters");
	$general->add_row("1.1", "Model year after which  entitlements are distributed","", $self->config_entry("POLICY_EUCAP2003_YEAR"));
	$general->add_row("1.2", "Base value arable entitlement","", $self->config_entry("POLICY_EUCAP2003_BASEVALUE_ARABLE"));
	$general->add_row("1.3", "Base value grassland entitlement","", $self->config_entry("POLICY_EUCAP2003_BASEVALUE_GRASSLAND"));
	$general->add_row("1.4", "Share of set-aside entitlements","", $self->config_entry("POLICY_EUCAP2003_SHARE_SETASIDE"));
	$general->add_row("1.5", "Maximum area for set-aside exemption","", $self->config_entry("POLICY_EUCAP2003_MAX_NOSETASIDE"));
	$general->add_empty_rows(2);
	
	my $arableAct = $self->activity($self->config_entry("POLICY_EUCAP2003_SUM_ARABLE"));
	print STDERR "Error: Activity '".$self->config_entry("POLICY_EUCAP2003_SUM_ARABLE"). "' specified in POLICY_EUCAP2003_SUM_ARABLE not defined\n" and exit(1) if (not defined $arableAct);			
	my $grasslandAct = $self->activity($self->config_entry("POLICY_EUCAP2003_SUM_GRASSLAND"));
	print STDERR "Error: Activity '".$self->config_entry("POLICY_EUCAP2003_SUM_GRASSLAND"). "' specified in POLICY_EUCAP2003_SUM_GRASSLAND not defined\n" and exit(1) if (not defined $grasslandAct);			
	my $topupAct = $self->activity($self->config_entry("POLICY_EUCAP2003_SUM_FOR_TOPUP"));
	print STDERR "Error: Activity '".$self->config_entry("POLICY_EUCAP2003_SUM_FOR_TOPUP"). "' specified in POLICY_EUCAP2003_SUM_FOR_TOPUP not defined\n" and exit(1) if (not defined $topupAct);			
				
	$general->add_row("2",	"Activities providing agent-specific information"			);
	$general->add_row("2.1", "Total arable area used",	"LP Column", $arableAct->lp_column());
	$general->add_row("2.2", "Total grassland area",	"LP Column", $grasslandAct->lp_column());		
	$general->add_row("2.3", "Sum of all premiums for top-up",	"LP Column", $topupAct->lp_column());
	$general->add_empty_rows(2);
	$self->add_block($general);

				
	my $oidSetAsideEnt;			
	my @ArableEnt;				
	my @GrasslandEnt;								
	
	for my $i ($self->networkobjects() ) {
		if($i->has_subelement("EUCAP2003")) {
			my $booked = 0;
			if($i->has_subelement_field("EUCAP2003", "setaside") && $i->subelement_field("EUCAP2003", "setaside") != 0) {
				print STDERR "Error for EUCAP2003: You defined more than one asset as set-aside entitlement\n" and exit(1) if (defined $oidSetAsideEnt);
				$oidSetAsideEnt = $i->oid();
				$booked = 1;
			}
			if($i->has_subelement_field("EUCAP2003", "arable") && $i->subelement_field("EUCAP2003", "arable") != 0) {
				print STDERR "Error for EUCAP2003: You defined asset ".$i->id ." as set-aside entitlement and as arable entitlement\n" and exit(1) if ($booked);
				push @ArableEnt, {value => $i->subelement_field("EUCAP2003", "arable"), oid => $i->oid() };
				$booked = 1;
			}
			if($i->has_subelement_field("EUCAP2003", "grassland") && $i->subelement_field("EUCAP2003", "grassland") != 0) {
				print STDERR "Error for EUCAP2003: You defined asset ".$i->id ." as grassland entitlement and as set-aside or arable entitlement\n" and exit(1) if ($booked);
				push @GrasslandEnt, {value => $i->subelement_field("EUCAP2003", "grassland"), oid => $i->oid() };
			}
		}
	}
	
	print STDERR "Error for EUCAP2003: You forgot to mark an asset for the set-aside entitlement\n" and exit(1) unless (defined $oidSetAsideEnt);
	@ArableEnt 	 = sort{ $a->{value} <=> $b->{value}} @ArableEnt;				
	@GrasslandEnt = sort{ $a->{value} <=> $b->{value}} @GrasslandEnt;
	
	my $entitlements = MpmasInputBlock->new(3,20);
	$entitlements->add_row("3",	"ObjectIDs of entitlements");			
				
	$entitlements->add_row("3.1", "Set-aside entitlement", "ObjID",  $oidSetAsideEnt);	
	$entitlements->add_empty_rows(1);
	$entitlements->add_row("3.2", "Number of arable entitlements", "",  scalar(@ArableEnt));					
	$entitlements->add_comment_row("3.3", "Arable entitlements", "", "Value", "ObjID");
	for my $i ( @ArableEnt ) {
		$entitlements->add_row("","","",$i->{value}, $i->{oid});	
	}
	$entitlements->add_empty_rows(1);
	$entitlements->add_row("3.4", "Number of grassland entitlements", "",  scalar(@GrasslandEnt));													
	$entitlements->add_comment_row("3.5", "Grassland entitlements", "", "Value", "ObjID");
	for my $i ( @GrasslandEnt ) {
		$entitlements->add_row("","","",$i->{value}, $i->{oid});	
	}
	$self->add_block($entitlements);
}
1;
