#!/usr/bin/perl

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

sub help {
	return qq{
	Updates the resource or facility ban. (BanId and type of Ban) OR (resourceId and memberId) OR (facilityId and userId) required . Date (DD.MM.YYYY) OR time (in UNIX epoch) of ban validityTo required.
	---------------------------------------------------------------
	Available options:
        --banId           | -i Ban Id
        --valTo           | -v date of expiration of Ban (DD.MM.YYYY)
        --valToEpoch      | -t time (in UNIX epoch in ms)
        --resourceType    | -R ban for resource-member
        --facilityType    | -F ban for facility-user
        --resourceId      | -r resource Id
        --memberId        | -m member Id
        --facilityId      | -f facility Id
        --userId          | -u user Id 
        --description     | -d description of Ban
	--batch           | -b batch
	--help            | -h prints this help

	};
}

my ($banId, $valTo, $valToEpoch, $resourceType, $facilityType, $dsc, $resourceId, $facilityId, $memberId, $userId);
our $batch;
GetOptions ("help|h"  => sub {
		print help();
		exit 0;
	}, "batch|b"      => \$batch,
	"banId|i=i"       => \$banId,
	"valTo|v=s"       => \$valTo,
	"valToEpoch|t=i"  => \$valToEpoch,
	"resourceType|R"  => \$resourceType,
	"facilityType|F"  => \$facilityType,
	"resourceId|r=i"  => \$resourceId,
	"memberId|m=i"    => \$memberId,
	"facilityId|f=i"  => \$facilityId,
	"userId|u=i"      => \$userId,
	"description|d=s" => \$dsc) || die help();

# Check options
if (defined($banId)) {
	unless (defined($resourceType) or defined($facilityType) or defined($resourceId) or defined($facilityId)) { die "ERROR: resource or facility Ban type is required \n";}
}
if (defined($resourceId) and !defined($memberId) and !defined($banId)) { die "ERROR: member Id is required \n";}
if (defined($facilityId) and !defined($userId) and !defined($banId)) { die "ERROR: user Id is required \n";}
unless (defined($banId) or defined($resourceId) or defined($facilityId)) { die "ERROR: Ban Id or resource Id or facility Id is required \n";}

my $agent = Perun::Agent->new();
if (defined $valTo and !defined $valToEpoch) {
	my ($dd, $mm, $rr) = split(/\./, $valTo);
	$valToEpoch = timelocal(0, 0, 0, $dd * 1, $mm * 1 - 1, $rr * 1 - 1900) * 1000;
}
if (defined($resourceType) or defined($resourceId)) {
	my $BanOnResourceAgent = $agent->getBanOnResourceAgent;
	my $ban;
	unless (defined $banId) {
		$ban = $BanOnResourceAgent->getBan( memberId => $memberId, resourceId => $resourceId );
	} else {
		$ban = $BanOnResourceAgent->getBanById( banId => $banId );
	}
	$ban->setValidityTo( $valToEpoch ) if defined ($valToEpoch);
	$ban->setDescription( $dsc ) if defined($dsc);
	$ban = $BanOnResourceAgent->updateBan( banOnResource => $ban );
	printMessage("Ban Id: ".$ban->getId." successfully updated. ", $batch);
}

if (defined($facilityType) or defined($facilityId)) {
	my $BanOnFacilityAgent = $agent->getBanOnFacilityAgent;
	my $ban;
	unless (defined $banId) {
		$ban = $BanOnFacilityAgent->getBan( userId => $userId, facilityId => $facilityId );
	} else {
		$ban = $BanOnFacilityAgent->getBanById( banId => $banId );
	}
	$ban->setValidityTo( $valToEpoch ) if defined ($valToEpoch);
	$ban->setDescription( $dsc ) if defined($dsc);
	$ban = $BanOnFacilityAgent->updateBan( banOnFacility => $ban );
	printMessage("Ban Id: ".$ban->getId." successfully updated. ", $batch);
}  

