package Modsde::Post;
use DateTime;
use base "Modsde";

__PACKAGE__->mk_accessors(qw/pid username uid/);

sub fetch {
	my $self = shift;
	$self->debug("Fetching post with PID " . $self->pid);
	$self->mech->get('http://forum.mods.de/bb/newreply.php?PID='.$self->pid);
	my $content = $self->mech->content;
	if($content =~ m~<textarea.+?'>(.*?)</textarea>~s) { #'
		my $post = $1;
		$post =~ s~\[quote.*?\[b\]\s(.*)\[/b\]\[/quote\]~$1~s;
		return $post;
	}
}

sub new {
	my $self = shift;
	my $options = shift;

	my $self = $self->SUPER::new($options);

	if(exists $self->{posted} && defined $self->{posted}) {
			$self->{posted} = $self->parseTime($self->{posted});
	}
	return $self;
}

sub post {
	my $self = shift;
	unless(defined $self->{post}) {
		$self->{post} = $self->fetch;
	}
	return $self->{post};
}

sub posted {
	my $self = shift;
	if(@_) {
		$self->{posted} = $self->parseTime($_[0]);
	}
	return $self->{posted};
}

sub parseTime {
	my $self = shift;
	my $string = shift;
	die "Wrong Date/Time format: [$string]" unless($string =~ /(\d{1,2}.\d{1,2}.\d\d\d\d)\s+(\d{1,2}:\d{1,2}:\d{1,2})/);

	my ($day,$month,$year) = split/\./, $1;
	my($hour, $min, $sec) = split/:/, $2;
	return DateTime->new(
		year		=> $year,
		month		=> $month,
		day		=> $day,
		hour		=> $hour,
		minute	=> $min,
		second	=> $sec
	);
}

1;
