#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case);
use Text::ASCIITable;
use Perun::Agent;
use Perun::Common qw(printMessage printTable getSortingFunction);

sub help {
	return qq{
	Prints list of publications.
	------------------------------
	Available options:
	--since        | -s  year since (>=)
	--till         | -t  year till (<=)
	--year         | -y  exact year
	--userId       | -u  ID of user
	--orderById    | -i  order by identifier of publication
	--orderByName  | -n  order by title of publication
	--batch        | -b  batch
	--help         | -h  prints this help
	};
}

our $batch;
my ($sortingFunction);

my $since = 0;
my $till = 0;
my $year = 0;
my $userId = 0;

	GetOptions("help|h" => sub {
		print help;
		exit 0;
	},
	"since|s=i"    => \$since,
	"till|t=i"    => \$till,
	"year|y=i"    => \$year,
	"userId|u=i"    => \$userId,
	"orderById|i"   => sub { $sortingFunction = getSortingFunction("getId") },
	"orderByName|n" => sub { $sortingFunction = getSortingFunction("getTitle", 1) },
	"batch|b"       => \$batch) || die help;

my $agent = Perun::Agent->new();
my $cabinetAgent = $agent->getCabinetAgent;

#options check
unless (defined $sortingFunction) { $sortingFunction = getSortingFunction("getTitle", 1); }

my @publications;
if ($since != 0 and $till == 0) {
	@publications = $cabinetAgent->findPublicationsByGUIFilter( yearSince => $since , userId => $userId );
} elsif ($since == 0 and $till != 0) {
	@publications = $cabinetAgent->findPublicationsByGUIFilter( yearTill => $till , userId => $userId );
} elsif ($since != 0 and $till != 0) {
	@publications = $cabinetAgent->findPublicationsByGUIFilter( yearSince => $since , yearTill => $till , userId => $userId );
} elsif ($year) {
	@publications = $cabinetAgent->findPublicationsByGUIFilter( year => $year , userId => $userId);
} else {
	@publications = $cabinetAgent->findPublicationsByGUIFilter( userId => $userId);
}

unless (@publications) {
	printMessage "No publications found", $batch;
	exit 0;
}

#output
printTable($sortingFunction, @publications);
