#!/usr/bin/perl
use autodie;
use warnings;
use strict;

use utf8;
use XML::Simple;
use Data::Dump qw(dump);

my $odt_file   = 'contributions.odt';

my $xs = XML::Simple->new();

use	OpenOffice::OODoc	2.101;
my $doc = odfDocument(file => $odt_file, create => 'text');

my $stat;
my $abstracts;

$doc->createStyle(
	"Abstract",
	family		=> 'paragraph',
	parent		=> 'Standard',
#	category	=> 'auto',
	properties	=> {
		-area			=> 'paragraph',
		'fo:text-align'	=> 'justify'
	}
);


my $nr = 0;
#foreach my $file ( glob 'contributions/*.xml' ) {

my $xml;
open(my $fh, '<:encoding(utf-8)', 'contributions/all.xml');
{
	local $/ = undef;
	$xml = <$fh>;
	close($fh);
}

#warn "---xml---", dump($xml), "---/xml---\n";

foreach my $xml ( split(/\Q<?xml\E/, $xml) ) {

	next unless $xml;

	$xml = '<?xml' . $xml;

	my $abstract;

	$abstract = $xs->XMLin( $xml, ForceArray => qw(PrimaryAuthor) );

	warn "# abstract = ", dump($abstract);

	my $id = $abstract->{'Id'}->[0] || die "no Id in ",dump($abstract);
	warn "# $id abstract = ", dump($abstract);

	foreach my $k (qw( ContributionType )) {
		push @{ $stat->{$k}->{ $abstract->{$k}->[0] } }, $id;
	}

	if ( $abstract->{'ContributionType'}->[0] eq 'Symposium proposal' ) {
		my $t = $abstract->{Title}->[0];
		push @{ $stat->{_symposium}->{ $t } }, -$id;
	}

	if ( my $s = $abstract->{Symposium_title} ) {
		$s = $s->[0];
		push @{ $stat->{_symposium}->{$s} }, $id;
	}

	if ( exists $abstract->{PrimaryAuthor}->[0]->{FamilyName} ) {
		$stat->{_order_by}->{$id} = $abstract->{PrimaryAuthor}->[0]->{FamilyName}->[0];
	}

	$abstracts->{ $id } = $abstract;

}

#warn "# stat = ",dump($stat);

use Encode;
sub enc {
	my $b = shift;
	warn "# enc in  ",Encode::is_utf8($b), dump($b);

#	$b = Encode::decode('iso-8859-1',$b); # XXX doesn't work!

#	Encode::from_to($b, 'iso-8859-1','utf-8');
#	Encode::_utf8_on($b);

	Encode::_utf8_off($b);

	warn "# enc out ",Encode::is_utf8($b), dump($b);
	return $b;
}
sub l1 {
	my $l = shift;
	#return 'FIXME ref ' . dump($l) if ref $l;
	return undef if ref $l;
	return enc($l) if $l =~ m/([\x{80}-\x{ff}])/;
	return $l;
}

my @authors;

sub abstract2doc {
	my $id = shift;

	my $abstract = $abstracts->{$id} || die "no $id in abstracts ",dump($abstracts);

	my $t = $doc->appendParagraph(text => $abstract->{Title}->[0], style => 'Heading 1');
	$doc->setPageBreak( $t, position => 'before', style => 'Heading 1');

	foreach my $a ( @{ $abstract->{PrimaryAuthor} } ) {
		my $v;
		$v->{$_} = l1($a->{$_}->[0]) foreach qw( FirstName FamilyName Affiliation Email );
		$doc->appendParagraph(text => $v->{FirstName} . ' ' . $v->{FamilyName}, style => 'Heading 2');
		$doc->appendParagraph(text => $v->{Affiliation}, style => 'Heading 3');
		push @{ $stat->{_Affiliation}->{ $v->{Affiliation} } }, $id;

		push @authors, $v;

	}

#	$doc->appendParagraph(text => $abstract->{Content}->[0], style => 'Abstract');
	foreach my $p ( split(/[\n\r]+/, $abstract->{Content}->[0] ) ) {
		$doc->appendParagraph(text => $p, style => 'Abstract');
	}

#	$doc->appendParagraph(text => "\nTheme: " . $abstract->{Theme}->[0], style => 'Theme');

#	$doc->appendParagraph(text => "\nContributionType: " . $abstract->{ContributionType}->[0], style => 'Quotations');

	$doc->appendParagraph(text => "\nID: " . $abstract->{Id}->[0], style => 'Abstract');

	warn "# added $id to document\n";
}

#foreach my $id ( keys %$abstracts ) {
#	abstract2doc($id);
#}

foreach my $symposium ( keys %{ $stat->{'_symposium'} } ) {

	my $t = $doc->appendParagraph(text => $symposium, style => 'Title');
	$doc->setPageBreak( $t, position => 'before', style => 'Title');
	$doc->appendParagraph(text => 'Symposium', style => 'Subtitle');

	my @symposium_ids = sort @{ $stat->{'_symposium'}->{$symposium} };
	warn "## [$symposium] symposium_ids ", dump( @symposium_ids );

	my $proposal_id = shift @symposium_ids;
	if ( $proposal_id < 0 ) {
		$proposal_id = abs( $proposal_id );
	} else {
		warn "ERROR: symposium [$symposium] doesn't have proposal!";
		
	}
#	@symposium_ids = sort { $stat->{_order_by}->{$a} cmp $stat->{_order_by}->{$b} } @symposium_ids;
	@symposium_ids = sort @symposium_ids;
	unshift @symposium_ids, $proposal_id;

	$stat->{_order_symposium}->{$symposium} = [ @symposium_ids ];

	foreach my $id ( @symposium_ids ) {
		abstract2doc($id);
	}

}

foreach my $type ( 'Oral presentation', 'Poster presentation' ) {

	my $t = $doc->appendParagraph(text => $type, style => 'Title');
	$doc->setPageBreak( $t, position => 'before', style => 'Title');

	my @order = sort { lc $stat->{_order_by}->{$a} cmp lc $stat->{_order_by}->{$b} }
		@{ $stat->{'ContributionType'}->{$type} };

	$stat->{ '_order_' . $type } = [ @order ];

	foreach my $id ( @order ) {
		abstract2doc($id);
	}
}


warn "# stat = ",dump($stat);

$doc->save;


open(my $fh, '>', 'affiliation.csv');
foreach my $a ( sort keys %{ $stat->{_Affiliation} } ) {
	print $fh '"',$a,'","',dump( $stat->{_Affiliation}->{$a} ), '"', "\n";
}


my $last_html = '';
open(my $authors_fh, '>', 'authors.html');
foreach my $a ( sort { $a->{FamilyName} cmp $b->{FamilyName} } @authors ) {
	my $html = qq{
<li>
 <b>$a->{FamilyName}, $a->{FirstName}</b><br>
 $a->{Affiliation}<br>
 <em>$a->{Email}</em>
</li>};
	print $authors_fh $html eq $last_html ? '<!-- dup -->' : $html;
	$last_html = $html;
}

close $authors_fh;

__DATA__
    "Symposium proposal"  => 12,
    	"Symposium abstract"  => 52,
    "Oral presentation"   => 141,
    "Poster presentation" => 71,

