#################################################
# Package that can create and manage Menus      #
# within the WLAN radio application             #
#################################################
#!/usr/bin/perl
use strict;

package WLAN::Radio::Menu;

sub new {
   my $invocant = shift;
   my $level = 0;
   my $name = shift || "";
   
   my $self = {};
   my @entries;

   $self->{'isMenu'} = 1;
   $self->{'entries'} = \@entries;
   $self->{'current'} = 0;
   $self->{'level'} = $level;
   $self->{'parent'} = -1;
   $self->{'name'} = $name;
   my $class=ref($invocant) || $invocant;   
    
   bless $self, $class;
   return $self;
}

# Adds a menu entry 
sub addEntry {
   my $self = shift;
   my $hashRef = shift;   
   push (@{$self->{'entries'}}, $hashRef);   
}

# Returns the current selected menu entry/sub menu
sub getCurrent {
   my $self = shift;
   return ($self->{'entries'}->[$self->{'current'}]);
}

# Switch to the next item within this menu
sub next {
   my $self = shift;
   if ($self->hasNext() == 1) {
	   $self->{'current'}++;
   }
   return($self->getCurrent());
}
# Switch to the previous item within this menu
sub prev {
   my $self = shift;
   if ($self->hasPrev() == 1) {
      $self->{'current'}--;
   }
   return($self->getCurrent());
}
# Returns 1, if the current menu item is not the last (otherwise, 0 will be returned)
sub hasNext {
   my $self = shift;
   my $len = @{$self->{'entries'}};
   if ($self->{'current'} < $len -1) {return(1);}
   return(0);
}
# Returns 1, if the current menu item is not the first (otherwise, 0 will be returned)
sub hasPrev {
   my $self = shift;
      if ($self->{'current'} > 0) {return(1);}
   return(0);
}
# Adds a sub menu to the menu
sub addSubMenu {
   my $self = shift;
   my $menu = shift;
   $menu->{'level'} = $self->{'level'}+1;
   $menu->{'parent'} = $self;
   $self->addEntry($menu);
}
# Goes up one level (from a sub menu to its parent menu)
sub getParent {
   my $self = shift;
   return($self->{'parent'});
}
# Reset the current menu to the first menu item
sub reset {
   my $self = shift;
   $self->{'current'} = 0;
}
# Deletes the menu items of the current menu
sub flush {
  my $self = shift;
  my @entries;
  $self->{'entries'} = \@entries;
  $self->{'current'} = 0;
}

1;





   

