#!/usr/bin/perl -w
#
# $Id: Event.pm,v 1.2 2002/03/14 10:14:11 fpmip Exp $
#
# Event object
#
# Author : Tycho Fruru <tycho@fruru.com>
#
# Copyright (C) 2001 Tycho Fruru <tycho@fruru.com>
# Copyright (C) 2001 Conostix S.A. Luxembourg - Tycho Fruru <tycho@conostix.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
package IPFC::Event;

use strict;
use warnings;

use Storable;

sub new() {
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	bless ($self,$class);
	return $self;
}

sub clone() {
	my $self = shift;
	my $class = ref($self);
	my $new;
	
	$new = Storable::dclone($self);
	bless ($new,$class);
	return $new;
}

sub add_attribute($$) {
	my $self = shift;
	my $attribute = shift;
	my $value = shift;
	push @{$self->{$attribute}} , $value;
	return 1;
}

sub add_attributes($) {
	my $self = shift;
	my $attributes = shift; # hashref containing attribute/value or attribute/index/value data
# $event->add_attributes( { 'attr_name' => 'attr_value', 
#  		'attr_name2' => [ 'attr_value1','attr_value2','attr_value3' ] } );
	
	foreach my $key (keys %$attributes) {
		if (ref($attributes->{$key}) eq "ARRAY") {
			push @{$self->{$key}}, @{$attributes->{$key}};
		} elsif (ref($attributes->{$key}) eq "HASH") {
			die "HASH not yet implemented in IPFC::Event";
		} elsif (!ref($attributes->{$key})) {
			push @{$self->{$key}}, $attributes->{$key};
		}
	}
	return 1;
}

sub replace_attributes($) {
# takes same type of parameters as add_attributes, but removes all occurrences of the attr_names first
	my $self = shift;
	my $attributes = shift;
	
	my $delete_attributes;
	foreach my $key (keys %$attributes) {
		$delete_attributes->{$key} = undef;
	}
	$self->delete_attributes($delete_attributes);
	$self->add_attributes($attributes);
	return 1;
}

sub delete_attributes($) {
	my $self = shift;
	my $attributes = shift;
# $event->delete_attributes( { 'attr_name' => 'attr_value',
#				'attr_name' => undef } )
	foreach my $key (keys %$attributes) {
		if (!defined($attributes->{$key})) {
			delete $self->{$key};
		} else {
			my $index = 0;
			while ($index <= $#{$self->{$key}}) {
				if ($self->{$key}->[$index] ne $attributes->{$key}) {
					$index++;
				} else {
					splice(@{$self->{$key}},$index,1); # get rid of the element	
				}
			}
		}
	}
	return 1;
}

return 1;
