package MpmasAgentExpectationParameters;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use CtUtilities qw(queryhash extqueryhash);
use MpmasInputfile;
use MpmasInputBlock;
our @ISA = qw(MpmasInputfile);

sub filename {
		
	return "AgentExpectationParameters";
}

sub required {
	my $self = shift;
	return 1 if ($self->config_entry("ADVANCED_EXPECTATIONS") );
	return 0;
}



sub prepare_shared {
	my ($self) = @_;	

	my $charhash;
	print STDERR "\nError: Missing EXPECTATION_PARAMETERS [MPMAS TABLES].\n
	This is required because you activated ADVANCED_EXPECTATIONS\n"  and exit(2) unless (defined $self->config_entry("EXPECTATION_PARAMETERS"));
		  
	 $charhash = $self->config_entry("EXPECTATION_PARAMETERS")->transform($self->get_dbh(),3);
	
	$self->{expectation_parameters} = $charhash;

}

sub prepare_special {
	my ($self) = @_;
	
	my $agentinfo = $self->agentinfo();

	my $head = MpmasInputBlock->new(1,10);
	$head->add_comment_row("File containing agent-specfic parameters for advanced expectation mechanisms");
	$head->add_empty_rows(2);
	
	$head->add_row("Number of years to skip expectations updating in the beginning", $self->config_entry("SKIP_PERIODS_EXPECTATION_UPDATE"));
	$head->add_empty_rows(2);
	
	my $block = MpmasInputBlock->new(1,20);
	$block->add_empty_rows(2);
	$block->add_comment_row("", "Farmstead ID", "WhichExpectations",  "ParamID", "Value");

	
	my $counter = 0;
	
	for my $ag ($self->{expectation_parameters}->query([],0))
	{	
		next if ($ag != -1 && (not defined  $agentinfo->query([$ag], 2) ) );
		
		my $mpr = $self->{expectation_parameters}->query([$ag, "prices", "method"],4);
		print STDERR "Error: expectation parameters for agent $ag incomplete, missing method for prices\n" and exit(1) unless (defined $mpr);
		print STDERR "Error: invalid expectation parameter for agent $ag (prices), method $mpr not defined\n" and exit(1) unless ($mpr >= 0 && $mpr <= 5);
		
		my $myd = $self->{expectation_parameters}->query([$ag, "yields", "method"],4);
		print STDERR "Error: expectation parameters for agent $ag incomplete, missing method for yields\n" and exit(1) unless (defined $myd);
		
		print STDERR "Error: invalid expectation parameter for agent $ag (yields), method $myd not defined\n" and exit(1) unless ($myd >= 0 && $myd <= 5);
		 
		my @ydParam; 
		if	( $myd == 5) {				 
				$ydParam[0] = $self->{expectation_parameters}->query([$ag, "yields", "state_weight"],4);
				print STDERR "Error: expectation parameters for agent $ag incomplete, missing state_weight for yields (method 5)\n" and exit(1) unless (defined $myd);
				$ydParam[1] = $self->{expectation_parameters}->query([$ag, "yields", "trend_weight"],4);
				print STDERR "Error: expectation parameters for agent $ag incomplete, missing trend_weight for yields (method 5)\n" and exit(1) unless (defined $myd);
		}
				
		$block->add_row("", $ag, $myd, $mpr, @ydParam);
		++$counter;
	}
	
	$head->add_row("Number of rows:", $counter);
	$self->add_block($head);
	$self->add_block($block);
}
1;
