#!/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 Attributes defined for Resource. Resource is required field.
	----------------------------------------------------
	Available options:
	--resourceId  | -r  Resource idetifier
	--orderById   | -i  order by numeric ID
	--orderByName | -n  order by name
	--batch       | -b  batch
	--help        | -h  prints this help
	};
}

our $batch;
my ($resourceId, $sortingFunction);
GetOptions ("help|h" => sub {
		print help;
		exit 0;
	},
	"batch|b"        => \$batch,
	"resourceId|r=i" => \$resourceId,
	"orderById|i"    => sub { $sortingFunction = getSortingFunction('getId') },
	"orderByName|n"  => sub {$sortingFunction = getSortingFunction("getName", 1); } ) || die help;


#options check
unless (defined $sortingFunction) { $sortingFunction = getSortingFunction("getName", 1); }
unless (defined $resourceId) { die "ERROR: ResourceId required\n"; }

my $agent = Perun::Agent->new();
my $attributesAgent = $agent->getAttributesAgent;

my @attributes = $attributesAgent->getAttributes( resource => $resourceId );
unless (@attributes) {
	printMessage "No Attribute found", $batch;
	exit 0;
}

#output
printTable($sortingFunction, @attributes);
