#!/usr/bin/perl

use EPrints;
use strict;

my $session = new EPrints::Session;
return unless( defined $session );

my $current_user = $session->current_user();

binmode( STDOUT, ":utf8" );

if(!defined $current_user)
{ 
	print STDOUT $session->phrase("sneep_not_logged_in");
	$session->terminate;
	return;
}

my $eprintid = $session->param("eprintid" );
my $from_javascript = EPrints::Utils::is_set( $session->param( "callback" ) ) ? 1 : 0;

my $eprint = EPrints::DataObj::EPrint->new($session, $eprintid, $session->get_repository->get_dataset("eprint"));

if(!defined $eprint)
{
	print STDOUT $session->phrase("sneep_not_an_eprint");
	$session->terminate;
	return;
}

my $title = $session->param( "title" );
my $content = $session->param( "content" );
my $type = $session->param( "type" );

unless( EPrints::Utils::is_set( $content ) )
{
	my $message = $session->phrase( 'sneep_content_required', type=>$type );
	&_render_error_response( $session, $message, $from_javascript );
	$session->terminate;
	return;
}

my $data = { 
	type=>$type, 
	title=>$title, 
	content=>$content, 
	datestamp=>EPrints::Time::get_iso_timestamp(), 
	lastmod=>EPrints::Time::get_iso_timestamp(), 
	userid=>$current_user->get_id(), 
	eprintid=>$eprint->get_id(), 
	eprint_owner=>$eprint->get_user()->get_id() 
};

my $sneep_ds = $session->get_repository()->get_dataset("sneep");

my $new_sneep = $sneep_ds->create_object( $session, $data );
#my $new_sneep = EPrints::DataObj::Sneep->create_from_data($session, $data, $sneep_ds);

unless( defined $new_sneep )
{
	&_render_error_response( $session, "Internal error", $from_javascript );
	$session->terminate;
	return;
}

$new_sneep->commit;

# create a notification (to the owner of the eprint) - only if it's comment

if( defined $type && $type eq 'comment' )
{
#my $ctext = length $content > 100 ? substr($content,0,96)."..." : $content;

#my $notif_data = { 
#	eprintid => $eprint->get_id, 
#	type => "comment", 
#	message => "$ctext" 
#};

#my $notif_ds = $session->get_repository->get_dataset( 'notification' );

#my $notif = $notif_ds->create_object( $session, $notif_data );
#if( !defined $notif )
#{
#	$session->get_repository->log( "Failed to create a notification object." );
#	return;
#}

## target user = owner of the eprint
#$notif->set_value( "target_userid", [ $eprint->get_value( "userid" ) ] );
#$notif->commit;

	# Also send a notif email to the user

	EPrints::DataObj::EventQueue->create_from_data( $session, {
			pluginid => "Event::EmailComment",
			action => "email",
			params => [$new_sneep->internal_uri],
	});
}


&render_response( $session, $new_sneep, $type, $from_javascript );

$session->terminate;

return;

sub render_response
{
	my( $session, $sneep, $type, $from_javascript ) = @_;

	if( $from_javascript ) 
	{
		$session->get_request->content_type("application/json");
		$session->get_request->headers_out->{'Cache-control'} = 'No-cache';
		if($sneep->can_edit)
		{
			my $string = EPrints::XML::to_string($sneep->render_citation('editable'),undef,1);
			$string =~ s/\r//g;
			$string =~ s/\n//g;
			$string =~ s/\t//g;
			$string =~ s/"/'/g;
			print STDOUT '{ "status": 1, "content": "'.$string.'" }';
		}
		else
		{
			my $string = EPrints::XML::to_string($sneep->render_citation('editable'),undef,1);
			$string =~ s/\r//g;
			$string =~ s/\n//g;
			$string =~ s/\t//g;
			$string =~ s/"/'/g;
			print STDOUT '{ "status": 1, "content": "'.$string.'" }';
		}
	}
	else
	{
		my $cgi_url = $session->get_repository()->get_conf("perl_url");
		$session->redirect($cgi_url."/sneep/sneep_page?eprintid=".$eprint->get_id."&type=".$type);
	}
}

sub _render_error_response
{
	my( $session, $message, $from_javascript ) = @_;

	if( $from_javascript ) 
	{
		$session->get_request->content_type( 'application/json' );
		$session->get_request->headers_out->{ 'Cache-control' } = 'No-cache';
		print STDOUT '{ "status": 0, "content": "'.$message.'" }';
	}
}
