#!/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 Member. Member is required field.
	---------------------------------------------------
	Available options:
	--memberId     | -m  Members identifier
	--orderById    | -i  order by numeric Id
	--orderByName  | -n  order by Name
	--batch        | -b  batch
	--help         | -h  prints this help
	};
}

our $batch;
my ($memberId, $sortingFunction);
GetOptions("help|h" => sub {
		print help;
		exit 0;
	},
	"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 $memberId) { die "ERROR: memberId is required\n";}
unless (defined $sortingFunction) { $sortingFunction = getSortingFunction("getName", 1); }

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

my $attributesAgent = $agent->getAttributesAgent;

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

#output
printTable($sortingFunction, @attributes);
