package MpmasLandMarket;

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

sub filename {
	return "LandMarkets";
}
sub required {
	my ($self) = @_;
	return ($self->config_entry("LANDMARKET_MODEL") > 1);
}


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

	####Prepare Input Blocks
	##General Parameters Block		
	my $block1 = MpmasInputBlock->new(3, 10);  # number of commentary columns, priority
	
	$block1->add_row("THE LAND MARKET FILE");
	$block1->add_row("1","GENERAL RENTAL MARKET PARAMETERS","","");
	$block1->add_row("","1.1","Agents sell land if lacking liquidity?",$self->config_entry("LMSELLFORCASH"));
	$block1->add_row("","1.2", "Number of existing rental contracts", $self->config_entry("LM_numcontracts_start") || 0);
	$block1->add_row("","1.3", "Rental market activity", $self->config_entry("LMTRADING_ON"));

	if ($self->config_entry("LANDMARKET_MODEL") > 1) {
		$block1->add_row("","1.4", "No renting out by farm households", $self->config_entry("LMNORENTOUT" || 0));
		$block1->add_row("","1.5","Number of renting-out attempts",$self->config_entry("LMLOOPS_RENTOUT"));
		$block1->add_row("","1.6","Duration of rental contract (years)",$self->config_entry("LMDURATION"));
		$block1->add_row("","1.7","Maximum distance for placing bid (in grid cells from farmstead)",$self->config_entry("LMBID_MAXDIST"));
		$block1->add_row("","1.8","Additional land distribution model",$self->config_entry("LAND_REDISTRIBUTION_MODEL"));
		
		$block1->add_empty_rows(3);
	}
	
	if ($self->config_entry("LANDMARKET_MODEL") == 2 or $self->config_entry("LANDMARKET_MODEL") == 3) {
		$block1->add_row("2","PLOT-WISE MARKET","","");
		$block1->add_row("","2.1","Minimum bid (relative to average shadow prices)",$self->config_entry("LMMINBIDREL"));
		#$block1->add_row("","5.02","Minimum bid (absolute value)",$self->config_entry("LMMINBIDABS"));
		#$block1->add_row("","5.03","Maximum bid (relative to average shadow prices)",$self->config_entry("LMMAXBIDREL"));
		#$block1->add_row("","5.04","Maximum bid (absolute value)",$self->config_entry("LMMAXBIDABS"));
		$block1->add_row("","2.2","Markup for renting-out decisions",$self->config_entry("LMMKUPOUT"));
		$block1->add_row("","2.3","Markup factor for renting-in decisions",$self->config_entry("LMMKUPIN"));
		$block1->add_row("","5.08","Number of renting-in attempts",$self->config_entry("LMTRYIN"));
		$block1->add_row("","","","");
	}
	if ($self->config_entry("LANDMARKET_MODEL") == 4) {
		$block1->add_row("3","PARCEL-WISE MARKETS","","");
		$block1->add_row("","3.1","Maximum parcel size",$self->config_entry("LMMAXPGSIZE"));
		$block1->add_row("","3.2","Join parcels diagonally",$self->config_entry("LMPGDIAGONAL"));
		$block1->add_row("","3.3","Minimum parcel size to split",$self->config_entry("LMMINPGSIZE"));
		$block1->add_row("","3.4","Reserve price for renting-out decisions (relative to observed rental prices)",$self->config_entry("LMRESERVE_PRICE_COEFF"));
		$block1->add_row("","3.5","Calculate maximum distance to nearest plot instead of farmstead",$self->config_entry("LMBID_MAXDIST_TO_PLOT"));
		$block1->add_row("","3.6","Bid relative to shadow price",$self->config_entry("LMBID_PRICE_COEFF"));
		$block1->add_row("","3.7","Assumed prob. of renting at average rental price",$self->config_entry("LMBID_PROB_OTHERS"));
		$block1->add_row("","3.8","Rental price = second highest bid + x * (highest bid - second highest bid)",$self->config_entry("LMRENT_AUCTION_TOPUP"));
		my $as = 0;
		$as = 1 if (scalar(@{$self->config_entry("LM_assets_with_land")}) > 0);
		$block1->add_row("","3.9","Assets traded with land?", $as);
		$block1->add_row("","","","");
	}
	$self->add_block($block1);

	my $segbounds;
	if (defined $self->config_entry("INITIAL_LAND_RENTS")) {
		$segbounds = $self->config_entry("INITIAL_LAND_RENTS")->transform($self->get_dbh(),1);	
	}
	my $sboundtab = MpmasInputBlock->new(2,20);
	$sboundtab->add_comment_row("4","INITIAL 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([$cb],2)) {
			$rent = $segbounds->query([$cb],2);

		}
		$sboundtab->add_row ("","Soil type $cb", $rent);

	}
	$sboundtab->add_empty_rows(3);
	$self->add_block($sboundtab);

	if ($self->config_entry("LAND_REDISTRIBUTION_MODEL")  ) {
		
			my $ldm = MpmasInputBlock->new(2,30);
			$ldm->add_comment_row("5","PARAMETERS FOR LAND (RE)DISTRIBUTION");
			$ldm->add_row ("","Allow partially fulfilling applications", $self->config_entry("LAND_REDISTRIBUTION_PARTIAL_ALLOWED"));
			$ldm->add_row ("","Include free plots at simulation start", $self->config_entry("LAND_REDISTRIBUTION_FREE_AT_START"));
			$ldm->add_row ("","Include plots relinquished in LP", $self->config_entry("LAND_REDISTRIBUTION_PLOTS_FROM_LP"));
			$ldm->add_row ("","Include plots not sold when bankrupt/leaving model", $self->config_entry("LAND_REDISTRIBUTION_PLOTS_ABANDONED"));
			$ldm->add_row ("","Include plots sold for cash when bankrupt/leaving model", $self->config_entry("LAND_REDISTRIBUTION_PLOTS_EMERGENCY_SOLD"));
			
			$ldm->add_empty_rows(3);
			
			$self->add_block($ldm);
	}
	
}

1;
