package femaletwo;
use strict;
use warnings;
use List::Util qw(sample any);
use Math::Random qw(random_normal);

my $Census = 0;

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $type = shift;
    $type ||= 0;
    my $self = {};
    $Census++;
    $self->{duration}      = sample 1, (3, 4, 4, 4, 5, 5, 5, 6);
	$self->{cyclelength}   = int(random_normal(1, 28.7, 2.5)+0.5);
    $self->{start}         = int(rand($self->{cyclelength}));
    my @bleedingdays       = map {$_ % $self->{cyclelength}} $self->{start}..$self->{start} + $self->{duration}-1;
    $self->{bleedingdays}  = $type ? \@bleedingdays : calcbleedingdays($self);
    $self->{internalclock} = 0;
    bless ($self, $class);
    return $self;
}

sub population {
    return $Census;
}

sub addday {
	my $self = shift;
	$self->{internalclock}++;
}

sub start {
    my $self = shift;
    if (@_) { $self->{start} = shift }
    return $self->{start};
}

sub duration {
    my $self = shift;
    if (@_) { $self->{duration} = shift }
    return $self->{duration};
}

sub cyclelength {
    my $self = shift;
    if (@_) { $self->{cyclelength} = shift }
    return $self->{cyclelength};
}

sub internalclock {
    my $self = shift;
    if (@_) { $self->{internalclock} = shift }
    return $self->{internalclock};
}

sub bleedingdays {
    my $self = shift;
    my @bleedingdays = @_;
    if (@_) { $self->{bleedingdays} = \@bleedingdays }
    return $self->{bleedingdays};
}

sub calcbleedingdays {
    my $self = shift;
    my @bleedingdays = map {$_ % $self->{cyclelength}} $self->{start}..$self->{start} + $self->{duration}-1;
	return \@bleedingdays;
}

sub isbleeding {
    my $self = shift;
	my $res = any {$_ == $self->{internalclock} % $self->{cyclelength}} @{$self->{bleedingdays}};
	return $res ? 1 : 0;
}

sub mustbefedwithstick {
    my $self = shift;
    return isbleeding($self);
}

sub maynevertalktomales {
    my $self = shift;
    return isbleeding($self);
}

sub adversetransfer {
    my $self = shift;
    return isbleeding($self);
}

sub viewfemale {
	my $self = shift;
	print join "\n", map {$self->$_} qw(start duration cyclelength internalclock isbleedingA isbleedingB);
	print "\n", join(' ', @{$self->bleedingdays}), "\n";
}

sub DESTROY { 
	--$Census;
}

1;
