#!/usr/bin/env perl

use warnings;
use strict;
use POSIX;

use CGI;

use RENA::LocalConfiguration qw($COLLECTIONS_DIR $BASE_DIR $ANALYSIS_DIR
				@collections);
use RENA::Support qw(&analyse_article &xml_to_xhtml
		     &yaml_to_xhtml);
use RENA::Support::Classes;
use RENA::JSpellFilter;
use RENA::JSpellFilters qw(&default_pipeline);

my $q = new CGI;

my $number = $q->param("number");
my $collection = $q->param("collection");

print STDERR "$collection, $number\n";
print STDERR "$COLLECTIONS_DIR\n";

if (not defined $number or not $number =~ /^\d+$/
    or not defined $collection or not $collection =~ /^[A-Za-z0-9_-]+$/
    or not -d "$COLLECTIONS_DIR/$collection") {
	print $q->redirect('/~epa/pick_article.html');
	exit 0;
}

if (not -d "$ANALYSIS_DIR") {
    mkdir "$ANALYSIS_DIR" or die; # FIXME produce some error message ?
}
if (not -d "$ANALYSIS_DIR/$collection") {
    # FIXME produce some error message ?
    mkdir "$ANALYSIS_DIR/$collection" or die;
}

my $f_xml = "$ANALYSIS_DIR/$collection/$number-annotated.xml";
my $f_yaml = "$ANALYSIS_DIR/$collection/$number-entities.yaml";
my $f_source = "$COLLECTIONS_DIR/$collection/$number.yaml";
my ($xml, $yaml);

my $cl = RENA::Support::Classes::load_from_file
    ("$BASE_DIR/tree.yaml");

if (not -f $f_yaml or not -f $f_xml) {
    if (not -f $f_source) {
	print $q->header,
	$q->start_html("article not found"),
	"Article nr. $number was not found.",
	$q->br,
	"Go ", $q->a({-href => "/~epa/pick_article.html"}, "back"),".",
	$q->end_html;
	exit 0;
    }
    
    my @pipeline = @{&default_pipeline($BASE_DIR)};
    push @pipeline, { name => "unified_yaml", options => {} },
                    { name => "toxml", options => {} };
    
    my $filter = new RENA::JSpellFilter
	(pipeline => \@pipeline, global => {cl => $cl});

    my $article;
    {
	my $t;
	my $f;
	open $f, "<", $f_source or die;
	local $/ = undef;
	$t = <$f>;
	close $f;
	$article = YAML::Load($t);
    }
    
    my $analysis = &analyse_article($filter, $article->{text});
    
    my ($x, $y);
    open $x, ">", $f_xml or die;
    open $y, ">", $f_yaml or die;
    print $x $analysis->{xml};
    print $y $analysis->{yaml};
    close $x;
    close $y;

    $xml = $analysis->{xml};
    $yaml = $analysis->{yaml};
} else {
    my ($h_xml, $h_yaml);

    open $h_xml, "<", $f_xml or die;
    open $h_yaml, "<", $f_yaml or die;
    {
	local $/ = undef;
	$xml = <$h_xml>;
	$yaml = <$h_yaml>;
    }
    close $h_xml;
    close $h_yaml;
}

my ($xml_xhtml, $yaml_xhtml);
$xml_xhtml = &xml_to_xhtml($xml);
$yaml_xhtml = &yaml_to_xhtml($yaml, $cl);

my $article = YAML::LoadFile($f_source);

print $q->header,
    $q->start_html({-style => {-src => "/~epa/article.css",
			       -type => "text/css"},
		    -script => {-src => "/~epa/pick_entity.js"},
                   -title => "$article->{collection} -- article nr. $number"}),
    qq(<div id="pageheader">),
    qq(<a href="../index.html">Home</a>),
    "</div>",
    qq(<div id="picker">),
    $q->start_form(-method => "GET", -accept_charset => "ISO-8859-15"),
      "Collection:<br />",
      $q->popup_menu("collection", \@collections, $collections[0]),
      "<br />Article:<br />",
      $q->textfield("number", $number, 10, 10),
      '<button type="submit">Retrieve</button>',
    $q->end_form,
    "</div>",
    qq(<div id="article_header">),
    $q->table({-border => 0},
	      $q->Tr({-style=>"background: lightgrey"},
		     $q->th("Collection"), $q->td($article->{collection})),
	      $q->Tr($q->th("Source", $q->td($article->{source}))),
	      $q->Tr({-style=>"background: lightgrey"},
		     $q->th("Number", $q->td($number))),
	      $q->Tr($q->th("Section", $q->td($article->{section}))),
	      $q->Tr({-style=>"background: lightgrey"},
		     $q->th("Date", $q->td($article->{date}))),
	      $q->Tr($q->th("Pre-title"), $q->td($article->{pretitle})),
	      $q->Tr({-style=>"background: lightgrey"},
		     $q->th("Title", $q->td($article->{title}))),
	      $q->Tr($q->th("Sub-title", $q->td($article->{subtitle}))),
	      ),
    qq(</div>), #article_header
    qq(<div id="analysis">),
    $yaml_xhtml,
    $xml_xhtml,
    "</div>",
    $q->end_html;
