package MpmasInputfileSet;

use strict;
use warnings;
use Exporter;
use Storable qw(dclone);
use Data::Dumper;
use SimpleObjectRegistry;
use MpmasInputBlock;
use Carp;
use SimpleObject;
our @ISA = qw(MpmasInputfile);

sub new {
	my $class = shift;
	my $self = {};
	my $priority = shift;
	
	
	$self->{Files} = SimpleObjectRegistry->create();
	$self->{priority} = $priority;
	$self->{id} = $class;
	bless $self, $class;
	$self->determine_files;
	return $self;
}


sub write {
	my $self = shift;
	my $skip_mode = shift;
	for my $i ($self->files ) {
		$i->write($skip_mode);
	}
}

## Implement for specific file

sub filename {
	return "FileSet";
}
sub required {
	return 1;
}

sub prepare_shared {
	my $self = shift;
	$self->prepare_shared_files;
	return 1;
}

sub prepare_special {
	my $self = shift;
	$self->prepare_special_files (@_);
	return 1;
}

sub determine_files {
	my $self = shift;
	#Implement for derived class
}

sub prepare_shared_files {
	my $self = shift;
	#Implement for derived class
}

sub prepare_special_files {
	my $self = shift;
	#Implement for derived class
}

sub add_file {
	my $self = shift;
	my $argument = shift;
	my $file_suffix = shift;
	my $class_override = shift ;
	my $priority = 1000;
	
	my $class = $self->class_of_files;
	$class = $class_override if (defined $class_override);
	my $f;
	eval("\$f = ".$class."->new($priority, \$argument, \"$file_suffix\" )") or croak "Error $@: Trying to create ". $self->class_of_files."$file_suffix";
	
	$self->{Files}->add_objects($f);
}
sub files {
	return $_[0]->{Files}->object_list;
}
sub file {
	my ($self, $id) = @_;
	return $self->{Files}->get_object_by_id($id);
}
sub delete_file {
	my ($self, $object)	= @_;
	$self->{Files}->delete_objects($object);
 }
1;
