package MpmasExternalScriptCalls;

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

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

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

	return ($self->config_entry("PRE_INVESTMENT_EXTERNAL_SCRIPT")
			|| (
				$self->config_entry("MULTIPERIOD")
				 && ( 	$self->config_entry("INYEAR_EXTERNAL_SCRIPT") 
						|| ( $self->config_entry("CROP_MODEL")  == 3
							&&  ( $self->config_entry("BIOVERSION")  == 5    || $self->config_entry("BIOVERSION")  == 7  )       
						   )
				    )  
				)
	);
}
sub prepare_shared {
	my ($self) = @_;
	
	if ( $self->config_entry("PRE_INVESTMENT_EXTERNAL_SCRIPT")) {
		$self->set_config_entry("POLICY_MODEL", $self->config_entry("POLICY_MODEL") + 8);
	}
	if (  $self->config_entry("MULTIPERIOD") &&  $self->config_entry("INYEAR_EXTERNAL_SCRIPT")) {
		$self->set_config_entry("POLICY_MODEL", $self->config_entry("POLICY_MODEL") + 16);
	}
	
}
sub prepare_special {
	my ($self) = @_;
	
	my $general = MpmasInputBlock->new(3,10);
	$general->add_row("","SCRIPT CALLS BY MPMAS");
	if ($self->config_entry("PRE_INVESTMENT_EXTERNAL_SCRIPT")) {
		$general->add_row("1", "INVESTMENT TRIALS");
		$general->add_row("", "","Agents do investment decisions (first round)");
		$general->add_row("", "","No investments are realized yet, but solution values ");
		$general->add_row("", "","for activities having the #solution_to_script_pre_investment> attribute");
		$general->add_row("", "","are written to the file  <scenario>_PreInvestmentSelectedSolution_<YY>.out. (in MPMAS3, different name in mpmasql4)");
		$general->add_row("", "","Also current asset endowments are written to the file <scenario>_Asset_preInv_<YY>.out.");
		$general->add_row("", "","Then the following script is called.", $self->config_entry("PRE_INVESTMENT_EXTERNAL_SCRIPT"));
		$general->add_row("", "","This script is supposed to read the above files and decide on some changes on any required changes on assets");
		$general->add_row("", "","which it is expected to write to the file <scenario>_Asset_cgd_<YY>.dat.");
		$general->add_row("", "","MPMAS will import this file and apply the changes and then run the normal investment decisions and implement it, too.");
		$general->add_row("", "", "","#END_PRE_INVESTMENT_EXTERNAL_SCRIPT");
	}
	if($self->config_entry("MULTIPERIOD") > 0 ) {
		
		if ($self->config_entry("PRE_INVESTMENT_EXTERNAL_SCRIPT")) {
			$general->add_row("", "", "Number of additional decision stages:",$self->config_entry("ADDITIONAL_DECISION_STAGES"));
			$general->add_row("", "", "","#END_ADDITIONAL_DECISION_STAGES");
			$general->add_empty_rows(1);
			
			
			for my $item ($self->get_cfg_list_empty_ok("PRE_INVESTMENT_EXTERNAL_SCRIPT_WRITE_OUT") ) {
				my @definedList = ("Assets", "Attributes", "Solutions");
				unless (is_in_string_list($item , \@definedList )) {
					print STDERR "Error in PRE_INVESTMENT_EXTERNAL_SCRIPT_WRITE_OUT: Undefined information write out requested: $item\nOptions are: ",
					join(", ", @definedList),"\n";
					exit(1);
				}
				
				$general->add_row("", "", "",$item);
			}
			$general->add_row("", "", "","#END_PRE_INVESTMENT_EXTERNAL_SCRIPT_WRITE_OUT");


			for my $item ($self->get_cfg_list_empty_ok("PRE_INVESTMENT_EXTERNAL_SCRIPT_READ_IN") ) {
				my @definedList = ("Assets", "Attributes");
				unless (is_in_string_list($item , \@definedList )) {
					print STDERR "Error in PRE_INVESTMENT_EXTERNAL_SCRIPT_READ_IN: Undefined information intake requested: $item\nOptions are: ",
					join(", ", @definedList),"\n";
					exit(1);
				}
				
				$general->add_row("", "", "",$item);
			}
			$general->add_row("", "", "","#END_PRE_INVESTMENT_EXTERNAL_SCRIPT_READ_IN");

			
			
			
			
		}
	
		if ( $self->config_entry("CROP_MODEL")  == 3 
								&&  ( $self->config_entry("BIOVERSION")  == 5    || $self->config_entry("BIOVERSION")  == 7  )       
			 ) {
			$general->add_row("", "","Call external crop model as shell script", $self->config_entry("CROP_MODEL_EXTERNAL_SCRIPT"));
			$general->add_row("", "", "","#END_CROP_MODEL_EXTERNAL_SCRIPT");
			$general->add_empty_rows(1);
		}
		if ($self->config_entry("INYEAR_EXTERNAL_SCRIPT")) {
			$general->add_row("", "","Call external shell script at end of cropping season", $self->config_entry("INYEAR_EXTERNAL_SCRIPT"));
			$general->add_row("", "", "","#END_ASSETS_INYEAR_EXTERNAL_SCRIPT");

			for my $item ($self->get_cfg_list_empty_ok("INYEAR_EXTERNAL_SCRIPT_WRITE_OUT") ) {
				my @definedList = ("Assets", "Attributes", "Solutions");
				unless (is_in_string_list($item , \@definedList )) {
					print STDERR "Error in INYEAR_EXTERNAL_SCRIPT_WRITE_OUT: Undefined information write out requested: $item\nOptions are: ",
					join(", ", @definedList),"\n";
					exit(1);
				}
				
				$general->add_row("", "", "",$item);
			}
			$general->add_row("", "", "","#END_ASSETS_INYEAR_EXTERNAL_SCRIPT_WRITE_OUT");


			for my $item ($self->get_cfg_list_empty_ok("INYEAR_EXTERNAL_SCRIPT_READ_IN") ) {
				my @definedList = ("Assets", "Attributes");
				unless (is_in_string_list($item , \@definedList )) {
					print STDERR "Error in INYEAR_EXTERNAL_SCRIPT_READ_IN: Undefined information intake requested: $item\nOptions are: ",
					join(", ", @definedList),"\n";
					exit(1);
				}
				
				$general->add_row("", "", "",$item);
			}
			$general->add_row("", "", "","#END_ASSETS_INYEAR_EXTERNAL_SCRIPT_READ_IN");

			$general->add_row("", "", "", $self->get_cfg_list_empty_ok("INYEAR_EXTERNAL_SCRIPT_CHANGED_UDEF_MAPS"));
			$general->add_row("", "", "","#END_INYEAR_EXTERNAL_SCRIPT_SPATIAL_CHANGES");
			$general->add_empty_rows(1);
		}
	}
	$self->add_block($general);
}

1;
