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

my $Census = 0;
#my $maxcyclelength = 32;
#my $Caged = 0;

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self  = {};
    $Census++;
#    $self->{start}         = int(rand($maxcyclelength));
    $self->{duration}      = sample 1, (3, 4, 4, 4, 5, 5, 5, 6);
#    $self->{cyclelength}   = sample 1, (28, 29, 30, 31, 32);
	$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}  = \@bleedingdays;
    $self->{bleedingdays}  = calcbleedingdays($self);
    $self->{internalclock} = 0;
#    $self->{bleeding}      = isbleeding($self);
#    $self->{out}           = onelessbleeding($self);
#    my $keepitpositive     = $self->{bleeding} - $self->{out} < 0 ? 0 : $self->{bleeding} - $self->{out};
#    $Caged += $keepitpositive;
    bless ($self, $class);
    return $self;
}

sub population {
    return $Census;
}

#sub caged {
#    return $Caged;
#}

sub addday {
	my $self = shift;
#	$self->{internalclock} = ($self->{internalclock} + 1) % $maxcyclelength;
	$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;
#    if ($self->{internalclock} >= $self->{start}
#    	&& $self->{internalclock} <= $self->{start} + $self->{duration}) {
#    	return 1;
#    } else {
#    	return 0;
#    }
#	my @bleedingdays = map {$_ % $self->{cyclelength}} $self->{start}..$self->{start} + $self->{duration}-1;
#	print join ' ', @bleedingdays;
#	print "\n";
	my $res = any {$_ == $self->{internalclock} % $self->{cyclelength}} @{$self->{bleedingdays}};
	return $res ? 1 : 0;
}

#sub onelessbleeding {
#    my $self = shift;
#	my $onelessday = ($self->{start} + $self->{duration}) % $self->{cyclelength};
#	return $onelessday == $self->{internalclock} % $self->{cyclelength}; 
#}

#sub transitionday {
#    my $self = shift;
#	my $transitionday = ($self->{start} + $self->{duration}-1) % $self->{cyclelength};
#	return $transitionday == $self->{internalclock} % $self->{cyclelength};
#}

sub mustbefedwithstick {
    my $self = shift;
    return $self->isbleeding;
}

sub maynevertalktomales {
    my $self = shift;
    return $self->isbleeding;
}

sub adversetransfer {
    my $self = shift;
    return $self->isbleeding;
}

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

sub DESTROY { 
	--$Census;
#	--$maxcyclelength;
#	--$Caged;
}

1;
