package MpmasRentalContracts;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use MpmasInputfile;
use MpmasInputBlock;
use IndexedTable;

our @ISA = qw(MpmasInputfile);


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

sub required {
	my $self = shift;
	return  ( $self->config_entry("LANDMARKET_MODEL") >= 2 
		  and  (defined $self->config_entry("RENTAL_TAB") and $self->config_entry("RENTAL_TAB") ne "0" and $self->config_entry("RENTAL_TAB") ne "" )   
		);
}
sub prepare_shared {
	my $self = $_[0];
	my $rental_tab = $self->config_entry("RENTAL_TAB")->transform($self->get_dbh() , 4);
	#catchment, sector, x, y, user, payment, duration
	my $agentinfo = $self->agentinfo();
	my ($x_extend, $y_extend);
	if ( not defined $self->config_entry("MAP_X_EXTENT") or not defined $self->config_entry("MAP_Y_EXTENT") ) {
	      use EnhancedGrid;
	      my $mapdir = "gis/";
	      $mapdir = $self->config_entry("MAPS") if (defined $self->config_entry("MAPS"));
	      $mapdir = $self->outdir ."../". $mapdir;
	      $mapdir = $mapdir ."/" unless ($mapdir =~/\/$/);
	      my $propmap = EnhancedGrid->create_from_ascii($mapdir."CatchMap00Prop.txt");
	      $x_extend = $propmap->ncols();
	      $y_extend = $propmap->nrows();
	}
	else {
	      $x_extend = $self->config_entry("MAP_X_EXTENT");
	      $y_extend = $self->config_entry("MAP_Y_EXTENT");
	}
	my @prepared_tab;
	foreach my $s (  $rental_tab->query([0], 0)) {
	      foreach my $x ( $rental_tab->query([0, $s], 0)) {
		  foreach my $y ( $rental_tab->query([0, $s, $x], 0)) {
			  my $fstid = $rental_tab->query([0, $s, $x, $y], 5);
			  if ( defined $agentinfo->query([$fstid], 2)) { #check if agent is part of current model
				  push @prepared_tab, [(
					"","", #skip two rows for file formatting
					 0,
					 $s,
					 $fstid,
					 #$y_extend - $y - 1,
					 $y,
					 $x,
					$rental_tab->query([0, $s, $x, $y], 6),
					$rental_tab->query([0, $s, $x, $y], 7)
				  )];
			  }; 
		  }
	      }
	}
	$self->{prepared_rental_tab} = \@prepared_tab;
	$self->set_config_entry("LM_numcontracts_start", scalar @prepared_tab);
}

sub prepare_special {
	my $self = $_[0];	
	my $header = MpmasInputBlock->new(2,10);
	$header->add_comment_row("","THE RENTAL CONTRACT FILE");
	$header->add_empty_rows(1);
	$header->add_comment_row("1","List of existing rental contracts at start of simulation");
        $header->add_comment_row("","Note: Contains look-up table with farmstead Ids, no map input needed");
       # $header->add_comment_row("","Note: Map indices are relative to partial sector maps here"); #changed this 20130219
	$header->add_empty_rows(1);								
	$header->add_comment_row("", "Number of contracts", $self->config_entry("LM_numcontracts_start"));
	$header->add_empty_rows(1);

	$header->add_comment_row("","Sector name","CatchID","SectorID","AgentMapID of land user renting-in","Map row index of parcel","Map column index of parcel","Rental payment","Duration of contract");

	my @tab = sort {  $a->[2] <=> $b->[2] || $a->[3] <=> $b->[3] || $a->[4] <=> $b->[4] || $a->[5] <=> $b->[5] || $a->[6] <=> $b->[6]  } @{$self->{prepared_rental_tab}};

        $header->add_table(\@tab);
	$self->add_block($header);
}
1;
