use strict;
use warnings;
use lib '.';
#use List::Util qw(sample);
use Math::Random qw(random_normal);
use femalesbook;

my ($poptotal, $days, $type) = @ARGV;
$poptotal ||= 100;
$days ||= 365;
$type ||= 0;

my $popref = createpop($poptotal, $type);
printfullness($days, $popref);

sub printfullness {
	my ($days, $popref) = @_;
	my $count = 0;
	
	for (1..$days) {
		my $bleedingtotal = 0;
		
		for (@$popref) {
			 $bleedingtotal += $_->isbleeding;
#			 $_->viewfemale();
		}

		print $count++, " $bleedingtotal\n";
		turnearth($popref);
	}
	
}

sub createpop {
	my ($total, $type) = @_;
	my @pop;

	for (1..$total) {
		push @pop, new femaletwo(createdata($type));
	}

	return \@pop;
}

sub createdata {
	my $type = shift;
	my @data;
	push @data, $type;
#	my $duration = sample 1, (3, 4, 4, 4, 5, 5, 5, 6);
	my @durations = (3, 4, 4, 4, 5, 5, 5, 6);
#	print scalar @durations, "\n";
	my $duration = $durations[rand(@durations)];
	push @data, $duration;
	my $cyclelength = int(random_normal(1, 28.7, 2.5)+0.5);
    push @data, $cyclelength;
    my $start = int(rand($cyclelength));
    push @data, $start;
	push @data, 0;
    my @bleedingdays = map {$_ % $cyclelength} $start..$start + $duration - 1;
	push @data, $type ? \@bleedingdays : calcbleedingdays($cyclelength, $start, $duration);
	return @data;
}

sub calcbleedingdays {
    my ($cyclelength, $start, $duration) = @_;
    my @bleedingdays = map {$_ % $cyclelength} $start..$start + $duration - 1;
	return \@bleedingdays;
}

sub turnearth {
	my $pop = shift;
	$_->addday for @$pop;
}
