package MpmasMembersAndAges;

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 "HouseholdMembers$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 @sexagecats = @ { $clufile->sexagecats};
	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[0][0]");
	$head->add_empty_rows(2);
	
	
	my $block = MpmasInputBlock->new(1,10);
	$block->add_empty_rows(2);
	
	if ($self->config_entry("DEMOGRAPHY_MODEL") < 2) {
		$block->add_comment_row("", "Farmstead ID", "Career", "Age", "Married?", "Quantity");
	}
	else {
		$block->add_comment_row("", "Farmstead ID", "Career", "Age", "CoupleId", "Quantity");
	}

	my $counter = 0;
	for my $ag ( @agents) {
		 foreach my $i ($householdhash->query([$ag],0, MpmasInputfile->strict_sorting() )) {
			 foreach my $age ($householdhash->query([$ag,$i],0, MpmasInputfile->strict_sorting()  *2)) {
				 foreach my $married ($householdhash->query([$ag,$i, $age],0, MpmasInputfile->strict_sorting()*2 )) {
					my $x = $householdhash->query([$ag,$i, $age, $married],5); 
					my $career = $self->config_entry("CAREERS")->{$i};
					$block->add_row("", $ag, $career, $age, $married, $x);	
					++$counter;
				 }
			}
		 }
	}
	
	$head->add_row("Number of rows:", $counter);
	$self->add_block($head);
	$self->add_block($block);
}
1;
