package ADBGUI::Loader;

use strict;
use warnings;


sub new {
   my $proto = shift;
   my $class = ref($proto) || $proto;
   my $dsts = shift;
   my $self  = {};
   bless ($self, $class);
   foreach my $dst (@$dsts) {
      push(@{$self->{pmdef}}, [[], $dst]);
   }

   opendir(PACKETS, ".");
   my @dirs = ();
   while(my $dirname = readdir(PACKETS)) {
      push(@dirs, $dirname);
   }
   foreach my $dirname (sort {
      $a cmp $b
   } @dirs) {
      next unless -d $dirname;
      next if $dirname =~ m,^\.+$,;
      next if ($dirname eq "ADBGUI");
      next if ($dirname eq "install");
      next if ($dirname eq "Documentation");
      foreach my $curdef (@{$self->{pmdef}}) {
         if (-R $dirname."/".$curdef->[1].".pm") {
            push(@{$curdef->[0]}, [$dirname, $curdef->[1]]);
            #print "Loading ".$dirname."/".$curdef->[1].".pm\n";
            require $dirname."/".$curdef->[1].".pm";
            print STDERR "Loaded ".$dirname."::".$curdef->[1]."\n";
         }
      }
   }
   return $self;
}

sub newObject {
   my $self = shift;
   my $curid = shift;
   my $curdef = undef;
   foreach my $curpmdef (@{$self->{pmdef}}) {
      next if ($curpmdef->[1] ne $curid);
      $curdef = $curpmdef;
      last;
   }
   die "Unbekanntes Modul '".$curid."' !"
      unless $curdef;
   my $params = shift;
   my $newobj = undef;
   #print "Starting ".$curdef->[1]."\n";
   eval '$newobj = ADBGUI::'.$curdef->[1].'->new(@$params)';
   die $@ if $@;
   #print '  ADBGUI::'.$curdef->[1]."\n";
   foreach my $curpacket (@{$curdef->[0]}) {
      #print "  ".$curpacket->[0]."::".$curpacket->[1]."\n";
      eval '$newobj = '.$curpacket->[0]."::".$curpacket->[1].'->new($newobj)';
      die $@ if $@;
   }
   $newobj->init()
      if ($newobj->can("init"));
   return $newobj;
}

1;
