#is this the holy jesus christ of the same?
use strict;
use warnings;
#use lib '.';
#use List::Util qw(sample);
#use Math::Random qw(random_normal);

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;
			 $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));
		push @pop, makehash(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);
	my @cycles = (25, 27, 27, 28, 28, 28, 28, 29, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 33, 35);
	my $cyclelength = $cycles[rand(@cycles)];
    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;
	addday($_) for @$pop;
}

sub makehash {
    my ($type, $duration, $cyclelength, $start, $internalclock, $bleedingdaysref) = @_;
    my $self = {};
    $self->{type} = $type;
    $self->{duration} = $duration;
    $self->{cyclelength} = $cyclelength;
    $self->{start} = $start;
    $self->{internalclock} = $internalclock;
    $self->{bleedingdays} = $bleedingdaysref;
    return $self;
}

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

sub isbleeding {
    my $self = shift;
	my $res = 0;

	for (@{$self->{bleedingdays}}) {
		if ($_ == $self->{internalclock} % $self->{cyclelength}) {
			$res = 1;
			last;
		}
	}
	
	return $res;
}

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 wherearetheyanyway {
    my $self = shift;
    return isbleeding($self);
}

sub problemswiththisone {
    my $self = shift;
    my $advice;
    if (isbleeding($self)) {
    	$advice = "create a different area to exist in for them\nseek local assistance from females\n";
    } elsif (takingdrugs($self)) {
        $advice = "protect eyes\nbecome over-sensitised to women\nooohhh become over-sensitised to women\n";
    } elsif (needsdoctor($self)) {
        $advice = "seek medical remedy\n";    
    } else {
        $advice = refreshthegood($self);
    }
    return $advice;
}

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

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

sub refreshthegood() {
	my $self = shift;
	return "seek assistance from females\nfind out from men what they know\n";
}

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

