package MpmasSegment;

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 {
	my $self = shift;
	return "Segment".$self->file_suffix;
}

sub required {
	my $self = shift;
	return 1;
}
sub prepare_special {
	my $self = shift;
	my $thresholds = shift;
	my $seginfo = shift;
	my $segbounds = shift;
	
	$self->argument =~ /(\d+)_(\d+)/;
	my $i = $1; #Current Network
	my $curSeg = $2; #Current Segment
#	my $csegstring = $self->argument;
	my %financial;
	for my $f ( qw(INTEREST_LGCRD INTEREST_SHCRD INTEREST_SHDEP EQSHARE INCTRANS DISCOUNT_RATE) ) {	 #RESELL OPPWAGE  MIGRATIONPULL
		$financial{$f} = $self->config_entry($f); 
	}
	if (defined $seginfo  ) {
		for my $f ($seginfo->query([$i, $curSeg], 0) ){
					$financial{$f} = $seginfo->query([$i, $curSeg, $f], 4);
		}
	}
	my $seghead =MpmasInputBlock->new(2, 10);
	$seghead->add_comment_row("THE INNOVATION SEGMENT FILE");
	$seghead->add_comment_row("NETWORK $i", "SEGMENT $curSeg");
	$seghead->add_empty_rows(1);
	$seghead->add_comment_row("1", "Basic Information per Innovation Segment");
	$seghead->add_comment_row("1.1","Segment ID");
	$seghead->add_row("","Innovation network threshold",$thresholds->[$curSeg]);
	$seghead->add_empty_rows(1);
	#$seghead->add_row("1.2","Interest rates");
	$seghead->add_row("1.2",	"Investment parameters");
	$seghead->add_row("","Interest rate on long-term borrowing",$financial{INTEREST_LGCRD});
	$seghead->add_row("","Interest rate on short-term borrowing",$financial{INTEREST_SHCRD});
	if ($self->config_entry("MULTIPERIOD") > 0) {
		$seghead->add_row("","Discount rate for multi-period planning",$financial{DISCOUNT_RATE});
	}
	else {
		 $seghead->add_row("","Interest rate on short-term deposits",$financial{INTEREST_SHDEP});
	 }
	$seghead->add_row("","Minimum proportion of the investment cost that has to be self-financed [0-1]", $financial{EQSHARE});
	#$seghead->add_row("","Income transfers",$financial{INCTRANS});
	$seghead->add_empty_rows(1);
	
#	$seghead->add_empty_rows(1);
#	$seghead->add_row("1.3","Migration");
#	$seghead->add_row("","Migration pull factor",$financial{MIGRATIONPULL});
#	$seghead->add_row("","Opportunity wage of the household head", $financial{OPPWAGE});
#	$seghead->add_row("","Proportion of the capital (of investment goods) that can be recovered when exiting[0-1]", $financial{RESELL});
#	$seghead->add_empty_rows(1);
	my $ni = keys %{$self->get_global("InnovationsInCurrentNetwork")};
	$seghead->add_row("1.4","Number of innovations", "$ni");
	$seghead->add_empty_rows(2);
	$self->add_block($seghead);
	
	my $segtab= MpmasInputBlock->new(2, 20) ;
	$segtab->add_comment_row("2","Diffusion Information for Innovations");
	$segtab->add_comment_row("","Code of Innovation", "Innovation ID", "Availability", "Accessibility");
	
	my $innos = $self->get_global("Innovations");
	my $innohash = $self->get_global("InnovationInfo");
	for my $curInno (@{$self->get_global("SortedInnovationsInCurrentNetwork")} ) {
		$segtab->add_row("",$curInno,$innos->{$curInno}, $innohash->query([$i,$curSeg, $curInno], 4), $innohash->query([$i,$curSeg, $curInno], 5));
		
	}
	$segtab->add_empty_rows(2);
	$self->add_block($segtab);
	if ($self->config_entry("PERMCROP_MODEL") == 1) {
		my $sboundtab = MpmasInputBlock->new(2,50);
		$sboundtab->add_comment_row("3","INVESTMENT CONSTRAINTS ON SOIL TYPES","Physical Bounds",	"Estimated Opportunity Cost");
	
		for my $cb (0..($self->config_entry("NSOIL")-1)) {
			my $bound = 1;
			my $oppcost = 0;
			
			if (defined $segbounds and defined $segbounds->query([$i,$curSeg,$cb],4)) {
				$bound = $segbounds->query([$i,$curSeg,$cb],4);
				$oppcost = $segbounds->query([$i,$curSeg,$cb],5);
			}
		    $sboundtab->add_row ("","Investments on soil type $cb", $bound , $oppcost);

		}
		$self->add_block($sboundtab);
	}
#	else {
#		my $sboundtab = MpmasInputBlock->new(2,50);
#		$sboundtab->add_comment_row("3","STANDARD LAND RENTS ON SOIL TYPES", ,"Standard land rent");
#	
#		for my $cb (0..($self->config_entry("NSOIL")-1)) {
#			
#			my $rent = 0;
#			
#			if (defined $segbounds and defined $segbounds->query([$i,$curSeg,$cb],4)) {
#				$rent = $segbounds->query([$i,$curSeg,$cb],4);
#
#			}
#		    $sboundtab->add_row ("","Soil type $cb", ,$rent);
#
#		}
#		$self->add_block($sboundtab);
#
#	}
}
