package MpmasAssetsAndAges;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use Scalar::Util qw(looks_like_number);
use CtUtilities qw(queryhash extqueryhash);
use MpmasInputfile;
use MpmasInputBlock;
our @ISA = qw(MpmasInputfile);

sub filename {
	my $self = shift;
	my $temp = $self->file_suffix;
	return "Endowments$temp";
}

sub required {
	my $self = shift;
	return 1 if ($self->config_entry("ASSETDISTTYPE") == 2);
	return 0;
}

sub agents {
    $_[0]->{agentlist};
}

sub prepare_shared {
	my $self = shift;	
	my $sexagedef = shift;	
	my $householdhash = shift;	
	my $assethash = shift;	
	my $agentsinclu = shift;
	
	$self->argument =~ /(\d+)_(\d+)/;
	my $curPop = $1;
	my $curClu = $2;
	$self->{agentlist} = [( $agentsinclu->query([$curPop, $curClu],0) )];


}

sub prepare_special {
	my $self = shift;	
	my $sexagedef = shift;	
	my $householdhash = shift;	
	my $fixcosthash = shift;	
	my $assethash = shift;	
	my $agentinfo = shift;
	my $parent = shift;
	#my $seedhash = $self->config_entry("seedhash");
	
	$self->argument =~ /(\d+)_(\d+)/;
	my $curPop = $1;
	my $curClu = $2;
	my $clufile = $parent->file("MpmasCluster_".$self->argument);

	my @agents  = sort {$agentinfo->query([$a], 6 ) <=> $agentinfo->query([$a], 6 ) || $agentinfo->query([$a], 5 ) <=> $agentinfo->query([$a], 5 ) || $a <=> $b  } @{$self->agents};
	my $head = MpmasInputBlock->new(1,10);
	$head->add_comment_row("File containing asset assignments in farm agent cluster[$curPop][$curClu]");
	$head->add_empty_rows(2);
	
	
	my $block = MpmasInputBlock->new(1,10);
	$block->add_empty_rows(2);
	$block->add_comment_row("", "FarmsteadID", "ObjectID", "Age", "Quantity");

	my %specobjname = % {$self->get_specobj_names()};
	my %specobjtype = % {$self->get_specobj_types()};
	my @sortedSpecObjs = sort {$a cmp $b } keys %specobjname;

	my $counter = 0;
	
	for my $ag ( @agents) {

		 foreach my $i ( $assethash->query([$ag],0, MpmasInputfile->strict_sorting() )) {
			if (exists $specobjname{$i}) {
					next;
			}			
			
			foreach my $age ($assethash->query([$ag, $i],0, MpmasInputfile->strict_sorting()  * 2)) {

				my $x = $assethash->query([$ag, $i, $age],4); 
				my $k = $self->networkobject($i);
				print STDERR "Error: Asset $i not part of the current model definition\n" and exit(1) unless (defined $k);
				print STDERR "Error in asset assignment: Invalid quantity provided for asset $i (agent $ag)\n" and exit(1) unless (looks_like_number($x));
				
				$block->add_row("", $ag, $k->oid, $age, $x);
				++$counter;
			}
		}
		foreach my $i (@sortedSpecObjs) {
			my @ages = $assethash->query([$ag, $i],0);
			my $x;
			if ( (scalar @ages) > 1 ) {
				croak ("Error: several entries for $i for agent $ag\n");
			}
			elsif ( (scalar @ages) == 1 && defined $ages[0] ) {
				$x = $assethash->query([$ag, $i, $ages[0]],4)
			}
		    else {
				$x =  $self->config_entry_or_croak(uc($i));
		    }
			print STDERR "Error in asset assignment: Invalid quantity provided for $i (agent $ag)\n" and exit(1) unless (looks_like_number($x));

		    
		    
		    if ( $i eq "expectations"          
				&&  $x >= 4
				&& ! $self->config_entry("ADVANCED_EXPECTATIONS") )
			{
				print STDERR "Error: You assigned expectations >= 4 to an agent, but did not activate ADVANCED_EXPECTATIONS\n";
				exit(1);
		    }
		    
		    $block->add_row("", $ag, $specobjtype{$i}, -1, $x);
		    ++$counter;
		 }
	}
	
	$head->add_row("Number of rows:", $counter);
	$self->add_block($head);
	$self->add_block($block);
}
1;
