package MpmasAssets;

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 "Assets$temp";
}

sub required {
	my $self = shift;
	return 1 if ($self->config_entry("ASSETDISTTYPE") == 1);
	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 @sexagecats = @ { $clufile->sexagecats};
	my @assets = @{ $clufile->assets };
	my @oids = @{ $clufile->oids };
	my @agents  = sort {$agentinfo->query([$a], 6 ) <=> $agentinfo->query([$a], 6 ) || $agentinfo->query([$a], 5 ) <=> $agentinfo->query([$a], 5 ) || $a <=> $b  } @{$self->agents};
	my $block = MpmasInputBlock->new(1,10);
	$block->add_comment_row("File containing asset assignments in farm agent cluster[0][0]");
	$block->add_empty_rows(2);
	$block->add_row("Number of rows:", scalar(@agents) + 1);
	$block->add_empty_rows(2);
	$block->add_comment_row("Sector seed","Population ID","Cluster ID","Catchment ID","Agent ID","Farmstead ID","Sector ID","Number plots",@sexagecats, @assets);

	my %specobjname = % {$self->get_specobj_names()};
	my @tmp;
	$#tmp = 7;
	@tmp = (-2502) x @tmp;
	$block->add_row("",@tmp, ( map { -1 * $_} (0..$#sexagecats) ),  @oids);  
	for my $ag ( @agents) {
		 my @row =	("");
		 push @row, -1000; # arbitrary seed
		 push @row, $curPop;
		 push @row, $curClu;
		 push @row, $agentinfo->query([$ag], 4);
		 push @row, $agentinfo->query([$ag], 5);
		 push @row, $ag;
		 push @row, $agentinfo->query([$ag], 6);
		 push @row, $agentinfo->query([$ag], 7);
		 foreach my $i (@sexagecats) {
		    my $x = $householdhash->query([$ag,$i],3);
		    $x = 0 unless (defined $x);
 			print STDERR "Error in household member assignment: Invalid quantity provided for member type $i (agent $ag)\n" and exit(1) unless (looks_like_number($x));
		    push @row, $x ;
		 }
		 foreach my $i (@assets) {

		    my $x = $assethash->query([$ag,$i],3); 
		    if (defined $x) {
				print STDERR "Error in asset assignment: Invalid quantity provided for asset $i (agent $ag)\n" and exit(1) unless (looks_like_number($x));
				push @row, $x ;
		    }
		    elsif (exists $specobjname{$i}) {
				push @row, $self->config_entry(uc($i));
		    }
		    else {
				push @row, 0 ;
		    }
		 }
		 $block->add_row(@row);
	}
	$self->add_block($block);
}
1;
