#
# This is the 'MainWindow' class of Mchange's glade
#

# babb
# 5/2/2011

package MainWindow;
use strict;

use CSL::Gnome2;
use Gnome2;
use Gtk2::GladeXML;

use Mchange;
use GUI::ChangeHW;
use GUI::Attribute;

use Carp;

### Change this to wherever glade will live.
our $GLADEPATH = '/s/mchange-2.2/lib';

sub new
{
	# Open the glade file to build controls for main window.
	my $this = shift;
	my $class = ref($this) || $this;

	my $self = { };
	bless $self, $class;

	$self->{FORM} = Gtk2::GladeXML->new("$GLADEPATH/mchange-gtk2.glade", 
					"MainWindow");

	croak "Error opening Glade file! Check your GLADEPATH." if(!$self->{FORM});

    $self->{FORM}->signal_autoconnect_from_package($self);

    return $self;

}


sub run
{
	my ($class, %params) = @_;

	Gnome2::Program->init("Mchange", "2.2");
	my $window = $class->new;

    $window->{FORM}->{mc} = new Mchange;
    my $me = `whoami`;
    chomp $me;

    $window->{FORM}{rts} = $window->{FORM}{mc}{rtdb}->get_rts( $me );
    $window->{FORM}->get_widget('run_button')->set_state(1) if $params{run};

    $window->{FORM}{rtBox} = new Gtk2::ListStore('Glib::String');
    my $renderer = new Gtk2::CellRendererText;
    $window->{FORM}->get_widget('rt_combo')->pack_start($renderer, 0);
    $window->{FORM}->get_widget('rt_combo')->set_attributes($renderer, text => 0);
    $window->{FORM}->get_widget('rt_combo')->set_model($window->{FORM}{rtBox});

    # Pick a rt number.
    # Populate the combo box with rts.
    if ( $params{rt} eq 'None' ) {
        $window->{FORM}->get_widget('no_rt')->hide;
        $window->{FORM}->get_widget('yes_rt')->show;
        $window->{FORM}->get_widget('rt_combo')->hide;
    }
    else {
        $window->{FORM}->get_widget('rt_combo')->append_text(" ");

        my @rt_list = keys %{$window->{FORM}{rts}};
        unshift @rt_list, $params{rt} if $params{rt};
        foreach(@rt_list)
        {
            $window->{FORM}->get_widget('rt_combo')->append_text($_);
        }

   }

    
    # Fill in the host name box.
    my ( $name1 );

    $name1 = $params{mnames}[0];

    $name1 = (gethostbyname($name1))[0] || $name1;
    $window->{FORM}->get_widget('machine_name1')->set_text( $name1 );

	# Go thundercats, go!
	Gtk2->main;

    return $window;

}


#==============================================================================
#=== MainWindow signal handlers
#==============================================================================
sub on_MainWindow_destroy {
    # I don't know what this was used for.
    # the code from mchange 2.1 does nothing.
    return;
}

sub on_MainWindow_delete_event {
    Gtk2->main_quit;
    return 0;
}

sub on_next_clicked {
    my ($form) = @_;

    my $machine1 = $form->wgt('machine_name1')->get_text;

    return $form->{FORM}{mc}{eh}->( $form->wgt('MainWindow'), "You didn't enter a machine name", 'error', 'ok' )
      unless $machine1;
    
    my $rt = "None";

    # Check to see if the rt box is visible or not. If it is, then get the number.

    if($form->wgt('rt_combo')->visible == 1)
    {
        $rt = $form->wgt('rt_combo')->get_active_text();
        return $form->{FORM}{mc}{eh}->($form->wgt('MainWindow'), "You didn't enter a RT number", 'error', 'ok' )
            unless $rt;
    }

    $form->{FORM}{run} = $form->wgt('run_button')->get_active;

    $form->{FORM}{mc}->set_rt ( $rt );

    my $machine = $form->{FORM}{mc}->register_machine( $machine1 );
    return if $machine eq 'error';

    $form->wgt('MainWindow')->hide;
     

    # Order of running things is:
    # HW (because once name changes, it "doesnt" have any of the hardware.
    # Attributes
    # Network (this may need to go earlier...not sure how splat works yet.)

    # Check to see if we actually did something
    my $bool = 0;


    if ( $form->wgt('change_hw')->get_active ) {
      ChangeHW->run( machine => $machine,
                            mc => $form->{FORM}{mc} );
      $bool = 1;
      
    }
    if ( $form->wgt('change_attr')->get_active ) {
        Attribute->run( machine => $machine,
                            mc => $form->{FORM}{mc} );
      $bool = 1;
    }

    if($bool == 0)
    {
        # User did not check any boxes!
        $form->wgt('MainWindow')->show;
        return $form->{FORM}{mc}{eh}->($form->wgt('MainWindow'), "You didn't actually check any boxes!", 'error', 'ok');
    }


    #write the script and possibly run it 
    $form->{FORM}{mc}->do_script( $form->wgt('run_button')->get_active, [ $machine1 ] ) ;

#th..th..th..that's all folks
    Gtk2->main_quit;

}

 
sub on_no_rt_clicked {
    my ($form) = @_;

    $form->wgt('no_rt')->hide;
    $form->wgt('rt_combo')->hide;
    $form->wgt('yes_rt')->show;
    $form->wgt('yes_rt')->grab_focus;
    
} # End of sub on_no_rt_clicked

sub on_yes_rt_clicked {
    my ($form) = @_;

    $form->wgt('yes_rt')->hide;
    $form->wgt('rt_combo')->show;
    $form->wgt('no_rt')->show;

} # End of sub on_yes_rt_clicked

sub on_machine_name1_focus_out_event {
  my ($form) = @_;

  return unless $form->wgt('autocomplete')->get_active;
  
  my $name = $form->wgt('machine_name1')->get_text;
  
  #check to see if we want to do a db query on the possible names
  if ( $name =~ m/^(.*)\*$/ ) {
    my @list = $form->{mc}->search_machine( $1 );

    if ( @list ) {
      $form->wgt('combo1')->set_popdown_strings( @list );
      $form->wgt('machine_name1')->activate;
    }
    else {
      chop( $name );
      $form->wgt('machine_name1')->set_text( $name );
      $form->wgt('machine_name1')->grab_focus;
    }
    
  }
  else {
    $name = (gethostbyname($name))[0] || $name;
    $form->wgt('machine_name1')->set_text( $name );
  }
  

  
} # End of sub on_hostname_focus_out_event


sub on_quit_clicked {
    # Goodbye!
    Gtk2->main_quit;
    
} # End of sub on_quit_clicked






#
# Helper function to get widget.
#
sub wgt
{
    my ($self, $widget) = @_;
    return $self->{FORM}->get_widget($widget);
}

