package MpmasInitialAgentCharacteristics;

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

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

sub required {
	my $self = shift;
	return 1 if ($self->get_cfg_list_empty_ok("EXTRA_AGENT_ATTRIBUTES") );
	return 0;
}

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

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



}

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

	my @agents  = sort { $a <=> $b  } $agentinfo->query([],0);
	my $head = MpmasInputBlock->new(1,10);
	$head->add_comment_row("File containing characteristics 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("", "Farmstead ID", "CharacteristicID",  "Value");

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

		 foreach my $i ($charhash->query([$ag],0)) {
						

				my $x = $charhash->query([$ag, $i],3); 
				my $k = $self->get_id_of_agent_attribute($i);
				print STDERR "Error: Agent characteristic $i not part of the current model definition\n" and exit(1) unless (defined $k);
				$block->add_row("", $ag, $k, $x);
				++$counter;
		}
	}
	
	$head->add_row("Number of rows:", $counter);
	$self->add_block($head);
	$self->add_block($block);
}
1;
