#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case);
use Perun::Agent;
use Perun::Common qw(printMessage);
use Switch;

sub help {
	return qq{
	Sets the UserExtSource attribute. UserExtSource id or (external source Name or Id and userExtSourceLogin) required. Attribute value and attribute id or name are required too.
	---------------------------------------------------------------
	Available options:
	--userExtSourceId    | -u userExtSource Id
	--userExtSourceLogin | -l users login at ExtSource
	--extSourceName      | -E extSource name
	--extSourceId        | -e extSource Id
	--attributeId        | -a attribute id
	--attributeName      | -A attribute name including namespace
	--attributeValue     | -w attribute value
	--batch              | -b batch
	--help               | -h prints this help

	};
}

my ($userExtSourceId, $userExtSourceLogin, $extSourceId, $extSourceName, $attributeId, $attributeName, @attributeValue);
our $batch;
GetOptions ("help|h"    => sub {
		print help();
		exit 0;
	}, "batch|b"        => \$batch,
	"userExtSourceId|u=i"   => \$userExtSourceId,
	"userExtSourceLogin|l=s" => \$userExtSourceLogin,
	"extSourceName|E=s" => \$extSourceName,
	"extSourceId|e=i" => \$extSourceId,
	"attributeId|a=i"   => \$attributeId,
	"attributeName|A=s" => \$attributeName, 'attributeValue|w=s@{1,}' => \@attributeValue) || die help();

# Check options
unless (defined $userExtSourceId ) {
	unless (defined $extSourceName or defined $extSourceId) { die "ERROR: extSourceId  or extSourceName is required \n";}
	unless (defined $userExtSourceLogin) { die "ERROR: userExtSourceLogin is required \n";}
}
unless (defined($attributeId) or defined($attributeName)) { die "ERROR: attributeId or attributeName is required \n";}
unless (@attributeValue) { die "ERROR: attributeValue is required \n";}

my $agent = Perun::Agent->new();
my $usersAgent = $agent->getUsersAgent;
my $extSourcesAgent = $agent->getExtSourcesAgent;

unless ($userExtSourceId) {
	my $extSource = $extSourcesAgent->getExtSourceByName( name => $extSourceName) if $extSourceName;
	$extSource = $extSourcesAgent->getExtSourceById( id => $extSourceId) if $extSourceId;

	my $userExtSource = $usersAgent->getUserExtSourceByExtLogin( extSourceLogin => $userExtSourceLogin, extSource => $extSource );
	$userExtSourceId = $userExtSource->getId;
}

my $attributesAgent = $agent->getAttributesAgent;
my $attributeDefinition;

if (!defined($attributeId)) {
	$attributeDefinition = $attributesAgent->getAttributeDefinition( attributeName => $attributeName );
} else {
	$attributeDefinition = $attributesAgent->getAttributeDefinitionById( id => $attributeId );
}

# Get the attribute definition and create the attribute
my $attribute = Perun::beans::Attribute->fromAttributeDefinition( $attributeDefinition );

$attribute->setValueFromArray( @attributeValue );

$attributesAgent->setAttribute( userExtSource => $userExtSourceId, attribute => $attribute );

printMessage("Attribute Id:".$attribute->getId." set for the userExtSource Id:$userExtSourceId", $batch);
