#!/usr/bin/perl -w

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

sub help {
	return qq{
	Lists all required resource-group attributes.  Group id or group name and VO and resource Id are required fields.
	-----------------------------------------------------------------------
	Available options:
	Available options:
	--groupId     | -g group id
	--groupName   | -G group name
	--voId        | -v vo id
	--voShortName | -V vo short name
	--resourceId  | -r resource id
	--orderById   | -i order by attribute id
	--orderByName | -n order by attribute friendly name
	--batch       | -b batch
	--help        | -h help

	};
}

our $batch;
my ($groupId, $groupName, $voId, $voShortName,$resourceId, $sortingFunction);
GetOptions ("help|h" => sub { print help(); exit 0;}, "batch|b" => \$batch,
"groupId|g=i" => \$groupId,
		"groupName|G=s" => \$groupName,
	"voId|v=i" => \$voId,
"voShortName|V=s" => \$voShortName,
		"resourceId|r=i" => \$resourceId,
	"orderById|i" => sub { $sortingFunction = getSortingFunction('getId') },
	"orderByName|n" => sub {$sortingFunction = getSortingFunction("getFriendlyName", 1); } ) || die help();


# Check options
unless (defined($groupId) or ((defined($voShortName) or defined($voId)) and defined($groupName))) {die "ERROR: groupId or groupName and voId or voShortName is required\n";}
unless (defined($resourceId)) { die "ERROR: resourceId is required \n";}
unless(defined $sortingFunction) { $sortingFunction = getSortingFunction("getFriendlyName", 1); }


my $agent = Perun::Agent->new();
my $vosAgent = $agent->getVosAgent;
my $groupsAgent = $agent->getGroupsAgent;

unless(defined($groupId)) {
	unless (defined($voId)) {
		my $vo = $vosAgent->getVoByShortName(shortName => $voShortName);
		$voId = $vo->getId;
	}

	my $group = $groupsAgent->getGroupByName(vo => $voId, name => $groupName);
	$groupId = $group->getId;
}

my $attributesAgent = $agent->getAttributesAgent;
my @attributes = $attributesAgent->getResourceRequiredAttributes(group => $groupId, resource => $resourceId, resourceToGetServicesFrom => $resourceId);

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

my $table = Text::ASCIITable->new();
$table->setCols('attribute Id','attribute friendly name','namespace', 'value');

foreach my $attribute (sort $sortingFunction @attributes) {
	$table->addRow($attribute->getId, $attribute->getFriendlyName, $attribute->getNamespace, $attribute->getValueAsScalar);
}
print tableToPrint($table, $batch);
