package MpmasInfrastructureProjects;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Carp ;
use Data::Dumper;
use List::Util qw(min max);
use CtUtilities qw(queryhash extqueryhash get_unique);
use MpmasInputfile;
use MpmasInputBlock;
use IndexedTable;
our @ISA = qw(MpmasInputfile);

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

sub required {
	my $self = shift;
	return(defined $self->config_entry("INFPROJ") and not $self->config_entry("INFPROJ") eq "0");
}
sub prepare_shared {
	my $self = shift;
	my $dbh = $self->get_dbh;
	$self->set_config_entry("POLICY_MODEL", $self->config_entry("POLICY_MODEL") + 2);
	my $infprojtab = $self->config_entry("INFPROJ")->transform($dbh,1);
	my @projects = $infprojtab->query([],0);
	$self->set_config_entry("N_INFPROJ", scalar @projects);
	$self->set_config_entry("INFPROJTAB", $infprojtab);
	my $infprojparticipants = $self->config_entry("INFPROJPARTICIPANTS")->transform($dbh,2);
 	$self->set_config_entry("INFPROJPARTICIPANTSTAB", $infprojparticipants);

}

sub prepare_special {
	my $self = shift;	
	my $regionhash = $self->config_entry("regionhash");
	my @sortedsectors = @{$self->config_entry("SORTEDSECTORS")};
	my $infprojtab = $self->config_entry("INFPROJTAB");
	my $infprojparticipants= $self->config_entry("INFPROJPARTICIPANTSTAB");

	my %projhash;

	for my $p ($infprojtab->query([],0)) {
	      my @participants = $infprojparticipants->query([$p],0);
	      my @sectors;
	      my @catchments;
	      for my $q (@participants) {
		    push @sectors, $infprojparticipants->query([$p,$q],4);
		    push @catchments, $infprojparticipants->query([$p,$q],3);
	      }
	      die "Error: Infrastructure project $p can only be awarded to severak farms if these lie in the same sector/district\n" unless (min(@sectors) == max(@sectors) and min(@catchments) == max(@catchments));
	      my $catch = min(@catchments);
	      my $sector = min(@sectors);
	      my $year =$infprojtab->query([$p],2);
	      $projhash{$catch}->{$sector}->{$year}->{$p} = $infprojtab->query([$p],3);
	      
	}
	
	my $head = MpmasInputBlock->new(2, 30);

	$head->add_row("THE INFRASTRUCTURE PROJECT FILE	");
	$head->add_row("1","Number of infrastructure projects", $self->get_config_entry("N_INFPROJ"));
	$head->add_row("2","List of infrastructure projects");
	$head->add_empty_rows(2);
	$head->add_comment_row("","","CatchID","SectorID","Year of implementation","Object ID","Size [ha]","Cost per participant","Share of subsidies","Number of participants","Farmstead ID of all participants");

	for my $c ( sort {$a <=> $b} keys %projhash ){
		for my $s (0..$#sortedsectors) {
		      for my $y (sort {$a <=> $b} keys %{$projhash{$c}->{$sortedsectors[$s]}}) {
			      for my $i (sort {$a <=> $b} keys %{$projhash{$c}->{$sortedsectors[$s]}->{$y}}) {

				    my @participants = $infprojparticipants->query([$i],0);
				    my $object = $self->networkobject( $infprojtab->query([$i],3) );

				    my  @row = (
				      "","",
				      $c,
				      $s,
				      $y,
				      $object->oid,
				      $infprojtab->query([$i],4),
				      $infprojtab->query([$i],5) / (scalar @participants),
				      $infprojtab->query([$i],6),
				      scalar @participants,
				      @participants
				    );
				    $head->add_row(@row);
			      }
			}
		}
	}
	$self->add_block($head);
}
1;
