#!/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 tableToPrint getSortingFunction);

sub help {
	return qq{
	Print list of Attribute for Resource and Member. Resource and member fields are required.
	------------------------------------------
	Available options:
	--resourceId  | -r  Resource identifier
	--memberId    | -m  Members identifier
	--orderById   | -i  order by numeric ID
	--orderByName | -n  order by name
	};
}


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

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

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


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

#output
			 my $table = Text::ASCIITable->new();
			 $table->setCols('ID','Name', 'Type', 'Value');

			 foreach my $attribute (sort $sortingFunction @attributes) {
				 $table->addRow($attribute->getId, $attribute->getName, $attribute->getType, $attribute->getValueAsScalar);
			 }

			 print tableToPrint($table, $batch);
