#!/usr/bin/perl -w

use strict;
use warnings;

use Mojo::JSON qw/decode_json/;
use SReview::Config::Common;
use SReview::Talk;
use SReview::Template::SVG;
use SReview::Files::Factory;
use DBI;

my $config = SReview::Config::Common::setup();

my $talkid = shift;

my $db = DBI->connect($config->get("dbistring"));
$db->prepare("UPDATE talks SET state='preview', progress='running' WHERE id = ?")->execute($talkid);

my $talk = SReview::Talk->new(talkid => $talkid);

if($talk->get_flag("manual_review")) {
	exit 0;
}

my $detector = $config->get("autoreview_detect");
my $coll = SReview::Files::Factory->create("intermediate", $config->get("pubdir"));
$coll->delete_files(relnames => [dirname($talk->relative_name)]);
my $prefile = $coll->get_file(relname => $talk->relative_name . "/pre.mkv");
my $mainfile = $coll->get_file(relname => $talk->relative_name . "/main.mkv");
my $postfile = $coll->get_file(relname => $talk->relative_name . "/post.mkv");

open JSON, "-|:encoding(UTF-8)", $detector, $talkid, $prefile->filename, $mainfile->filename, $postfile->filename;
my $json;
{
	local $/ = undef;
	$json = <JSON>;
}
close JSON;
$json = decode_json($json);

if(exists($json->{done})) {
	$talk->state_done("preview");
} elsif(exists($json->{broken})) {
	$talk->comment($json->{broken});
	$talk->done_correcting;
	$talk->set_state("broken");
} else {
	foreach my $key(keys %$json) {
		$talk->add_correction($key => $json->{$key});
	}
        $talk->add_correction(serial => 1);
	$talk->done_correcting;
	$talk->set_state("cutting");
}

__END__

=head1 NAME

sreview-autoreview - SReview state script to automate review

=head1 SYNOPSIS

sreview-autoreview I<TALKID>

=head1 DESCRIPTION

This script should be used as a state script for cases where review can
be automated. It should be called with a TALKID argument.

It expects the configuration value C<autoreview_detect> to be set to the
name of a script that will perform the actual review. This script will
receive the TALKID and the filenames of the pre, main and post videos,
in that order, on the command line. It should output a JSON object which
can contain the following keys:

=over

=item done

If this key exists in the JSON object, review will be assumed to have
completed and the talk will be moved to the next state.

=item broken

If this key exists in the JSON object, the will be assumed to be a
failure. The value of the C<broken> key will be used as the comment to
be set in the database for future reference; after that, the talk will
be moved to the "broken" state.

=item audio_channel

=item length_adj

=item offset_audio

=item offset_start

=item offset_end

If any of these keys exist in the JSON object, the value of that key
(which I<must> be a numeric value) will be used as a correction value
for the corresponding talk property. Once all these properties are
processed, the talk's C<serial> property will be incremented and the
talk will be moved back to the C<cutting> state.
