package MpmasLivSimCouplingInfo;

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

sub filename {
	return "MpmasLivSimCouplingInfo";
}
sub required {
	my ($self) = @_;
	return ($self->config_entry("MULTIPERIOD")  && 
	($self->config_entry("CROP_MODEL") == 3 )
	&& ($self->config_entry("BIOVERSION") == 7 || $self->config_entry("BIOVERSION") == 8)); 
}


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

	print STDERR "Error: Missing LIVSIM_ASSET_LINK [MPMAS TABLES]\nThis is required because you set BIOVERSION to ".$self->config_entry("BIOVERSION")  and exit(1) unless (defined $self->config_entry("LIVSIM_ASSET_LINK"));
	my $linktab = $self->config_entry("LIVSIM_ASSET_LINK")->transform($dbh, 6);
	
	print STDERR "Error: Missing LUCIA_LIVSIM_PASTURE_ID [MPMAS TABLES]\nThis is required because you set BIOVERSION to ".$self->config_entry("BIOVERSION")  and exit(1) unless (defined $self->config_entry("LUCIA_LIVSIM_PASTURE_ID"));
	my $pastureIDtab = $self->config_entry("LUCIA_LIVSIM_PASTURE_ID")->transform($dbh, 2);
	
	unless (defined $self->config_entry("LIVSIM_AGE_TO_MPMAS_AGE")) {
		$self->set_config_entry("LIVSIM_AGE_TO_MPMAS_AGE", $self->config_entry("PERIODS_PER_YEAR"));
	}
	print STDERR "Error: LIVSIM_MPMAS_AGE_PRECISION_FACTOR must be greater than LIVSIM_AGE_TO_MPMAS_AGE (which is by default equal to PERIODS_PER_YEAR)\n" 
		and exit(1) unless ( $self->config_entry("LIVSIM_MPMAS_AGE_PRECISION_FACTOR") > $self->config_entry("LIVSIM_AGE_TO_MPMAS_AGE"));

	
	my $block = MpmasInputBlock->new(3,10);
	$block->add_row("THE 'MPMAS LIVSIM COUPLING INFO' FILE");
	$block->add_row("1","NUMERIC TRANSFORMATIONS","","");
	$block->add_row("","1.1", "LivSim age to Mpmas age", $self->config_entry("LIVSIM_AGE_TO_MPMAS_AGE") );
	$block->add_row("","1.2", "LivSim Class Age Precision Factor", $self->config_entry("LIVSIM_MPMAS_AGE_PRECISION_FACTOR") );
	$block->add_row("","1.3", "LivSim Class Bodyweight Precision Factor", $self->config_entry("LIVSIM_MPMAS_BW_PRECISION_FACTOR") || 10);
	$block->add_empty_rows(1);
	$block->add_row("2","MATCH LIVSIM TO MPMAS","","");
	
	$block->add_comment_row("","","AssetID","ObjectID","Breed","Sex","Lower age", "Upper age", "Lower body weight", "Upper body weight", "gestating", "lactating");
	
	
	
	my $block2 = MpmasInputBlock->new(3,20);
	my $count = 0;
	for my $breed ($linktab->query([],0))
	{
		for my $sex ($linktab->query([$breed],0))
		{		
			for my $gestating ($linktab->query([$breed,$sex],0))
			{		
				for my $lactating ($linktab->query([$breed,$sex,$gestating],0))
				{
					my $prev_upage = 0 ;
					for my $upper_age (sort {$a <=> $b} $linktab->query([$breed,$sex,$gestating,$lactating],0))
					{
						my $prev_upbw = 0 ;
						for my $upper_bw (sort {$a <=> $b} $linktab->query([$breed,$sex,$gestating,$lactating,$upper_age],0))
						{
							my $elementID = $linktab->query([$breed,$sex,$gestating,$lactating,$upper_age, $upper_bw],7);
							my $c = $self->networkobject($elementID);
							print STDERR "Error: Unknown asset  '$elementID' in LIVSIM_ASSET_LINK\n" and exit(1) unless (defined $c);
							$block2->add_row("", "", $elementID, $c->oid(),$breed,$sex, $prev_upage, $upper_age ,
																		, $prev_upbw, $upper_bw,$gestating, $lactating  );
							$prev_upbw = $upper_bw + 1 /$self->config_entry("LIVSIM_MPMAS_BW_PRECISION_FACTOR") ;
							++$count;
						}
						$prev_upage = $upper_age + $self->config_entry("LIVSIM_AGE_TO_MPMAS_AGE") /$self->config_entry("LIVSIM_MPMAS_AGE_PRECISION_FACTOR")   ;
					}
				}
			}
		}
	}
	$block->add_row("","", "Number of livsim match classes",$count);

	$self->add_block($block);
	$self->add_block($block2);
	
	my $block3 = MpmasInputBlock->new(3,30);
	$block3->add_row("3", "CropManagement IDs allowing pasturing");
	
	my $block4 = MpmasInputBlock->new(3,40);

	
	my $xnlinktab = $self->config_entry("CONVERTED_EXTERNAL_CG_INFO_MULTIPERIOD");
	
	$count = 0;
	for my $season ($pastureIDtab->query([],0) )
	{
		for my $cmID ($pastureIDtab->query([$season],0) )
		{
			for my $sID ($xnlinktab->query([], 0) ) {
			
				my $caID = $xnlinktab->query([$sID, $cmID], 3);
				if (defined $caID) {
					$block4->add_row("","","",$season, $caID);
					++$count;
				}	
			}
		}
	}
	$block3->add_row("","", "Number of entries:",$count);

	$self->add_block($block3);
	$self->add_block($block4);	
	
		
	my $block5 = MpmasInputBlock->new(3,50);
	
	$block5->add_row("4","LIVSIM_HERD_MANAGEMENT","","");
	$block5->add_row("","", "LIVSIM_target_herd", $self->config_entry("LIVSIM_target_herd"));
	$block5->add_row("","", "LIVSIM_replacement_age_bulls", $self->config_entry("LIVSIM_replacement_age_bulls"));
	$block5->add_row("","", "LIVSIM_max_open", $self->config_entry("LIVSIM_max_open"));
	$block5->add_row("","", "LIVSIM_max_bulls", $self->config_entry("LIVSIM_max_bulls"));
	$block5->add_row("","", "LIVSIM_max_age_cows", $self->config_entry("LIVSIM_max_age_cows"));
	$block5->add_row("","", "LIVSIM_max_lact", $self->config_entry("LIVSIM_max_lact"));
	$self->add_block($block5);	
}


1;
