package Siikir::Plugin::Comment 2011.1003;

use 5.14.0;
use strict;
use warnings;
use Siikir::Util;

use base "Siikir::Plugin";

=head1 NAME

Siikir::Plugin::Comment - Generic page comment support.

=cut

sub init {
	my $self = shift;

	$self->debug("Comment plugin loaded!");
	$self->requires(qw(User Facebook Messaging JsonDB));

	# Options.
	$self->options(
		# Send notification messages how?
		notify => "email", # or "messaging"
		sender => 1,       # sender
	);

	# Interface.
	$self->interface([
		{
			category => "Comment Settings",
			fields   => [
				{ section => "Behavior" },
				{
					name    => "notify",
					label   => "Notification Method",
					text    => "Specify how comment notifications are to be delivered to the owners.",
					type    => "radio",
					options => [
						"email" => "Send an e-mail directly (if the owner has a valid e-mail address)",
						"messaging" => "Use the Messaging plugin (sends them a message on the site, which may also trigger an e-mail)",
						""          => "Do not send notifications about comments.",
					],
				},
				{
					name    => "sender",
					label   => "User ID of Sender",
					text    => "The user ID number of the user that sends in-site messages (if the Notification Method is 'messaging'). Default is 1 for the admin.",
					type    => "number",
				},
			],
		},
	]);
}

=head1 METHODS

=head2 bool addComment (string owner, string thread, hash options)

Publish a comment on a C<thread> that is owned by C<owner>. Options include:

  int uid:        ID of the user posting the comment (0 for guest comments)
  string name:    The name of the commenter (if a guest)
  string image:   URL to their facebook image (if available)
  string message: The message to post.
  string subject: Subject for the notification e-mail.
  string url:     URL where the comment can be seen.
  int time:       Epoch time of the comment.
  string ip:      IP address of the commenter.
  bool noemail:   If true, no notification e-mail is sent.

=cut

sub addComment {
	my ($self,$owner,$thread,%opts) = @_;
	$owner  = Siikir::Util::stripID($owner);
	$thread = Siikir::Util::stripSimple($thread);
	my $uid = Siikir::Util::stripID($opts{uid});

	# Owner must exist.
	if (!$self->Master->User->userExists(id => $owner)) {
		$@ = "Comment owner ID $owner not found.";
		return undef;
	}

	# Get the comments for this thread.
	my $comments = $self->getComments($owner, $thread);

	# Make up a unique ID for the comment.
	my $id = Siikir::Util::randomHash();
	while (exists $comments->{$id}) {
		$id = Siikir::Util::randomHash();
	}

	# Add the comment.
	$comments->{$id} = {
		uid     => $uid,
		name    => $opts{name} || "Anonymous",
		image   => $opts{image} || "",
		message => $opts{message},
		time    => $opts{time} || time(),
		ip      => $opts{ip} || $ENV{REMOTE_ADDR},
	};

	# Save them.
	$self->Master->JsonDB->writeDocument("comments/$owner/$thread", $comments);

	# Get info about the commenter.
	my $name = $opts{name} || "Anonymous";
	if ($self->Master->User->userExists(id => $uid)) {
		my $profile = $self->Master->Profile->getProfile($uid);
		$name = $profile->{displayname};
	}

	# Send the e-mail.
	unless ($opts{noemail}) {
		# How are we notifying them?
		if ($self->{notify} eq "messaging") {
			# Send an in-site message.
			$self->Master->Messaging->sendMessage (
				to      => $owner,
				from    => $self->{sender} || $owner,
				subject => "New Photo Comment!",
				message => "<strong>$name</strong> has left a comment on one of your photos.\n\n"
					. "To view the comment, please go to: <a href=\"$opts{url}\">$opts{url}</a>.\n\n"
					. "<hr>\n"
					. "This message was automatically generated. Do not reply to it.",
				html    => 1,
			);
		}
		elsif ($self->{notify} eq "email") {
			$self->Master->Messaging->email (
				to      => $owner,
				class   => "comment",
				subject => "New Comment: $opts{subject}",
				message => "$name has left a comment on: $opts{subject}\n\n"
					. "$opts{message}\n\n"
					. "To view this comment, please go to $opts{url}\n\n"
					. "================\n"
					. "This e-mail was automatically generated. Do not reply to it.",
			);
		}
	}

	return 1;
}

=head2 href getComments (string owner, string thread)

Get the comments for the thread.

=cut

sub getComments {
	my ($self,$owner,$thread) = @_;
	$owner  = Siikir::Util::stripUsername($owner);
	$thread = Siikir::Util::stripSimple($thread);

	# Owner must exist.
	if (!$self->Master->User->userExists(id => $owner)) {
		$@ = "Comment owner ID $owner not found.";
		return undef;
	}

	# Get the DB.
	if (!$self->Master->JsonDB->documentExists("comments/$owner/$thread")) {
		return {};
	}

	return $self->Master->JsonDB->getDocument("comments/$owner/$thread");
}

=head2 bool deleteComment (string owner, string thread, string commentid)

Delete a comment.

=cut

sub deleteComment {
	my ($self,$owner,$thread,$comment) = @_;
	$owner  = Siikir::Util::stripUsername($owner);
	$thread = Siikir::Util::stripSimple($thread);

	# Owner must exist.
	if (!$self->Master->User->userExists(id => $owner)) {
		$@ = "Comment owner ID $owner not found.";
		return undef;
	}

	# Get the comments for this thread.
	my $comments = $self->getComments($owner, $thread);

	# Delete the comment.
	delete $comments->{$comment};
	$self->Master->JsonDB->writeDocument("comments/$owner/$thread", $comments);

	return 1;
}

1;
