#!/usr/bin/perl

use strict;

use lib qw(/usr/lib/libDrakX);
use common;

# we need ugtk2 to build a gtk2 interface easily
use ugtk2 qw(:all);
# we need this so that libconf can generate GUI
use Libconf::GUI::Gtk2 qw(:generators :builders);
use Libconf::System::Network;

# for debugging purpose
use Data::Dumper;

# we ask systemconf to give us the structure representing the global network config
my $sys_network = new Libconf::System::Network();



# we build the window
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect(delete_event => sub { ugtk2->exit(0) });
$window->set_position('center');
$window->set_title(N("LAN configuration"));
$window->set_size_request(640, 480);
$window->set_border_width(5);

my $button_box = Gtk2::HButtonBox->new;
$button_box->set_spacing_default(5);
gtkadd($window,
       gtkpack_(Gtk2::VBox->new(0,5),
                1, my $main_notebook = new Gtk2::Notebook,
                0, gtkadd(gtkset_layout($button_box, 'end'),
                          gtksignal_connect(new Gtk2::Button(N("Apply")), clicked => sub { save() }), #BUG- does not exists
                          gtksignal_connect(new Gtk2::Button(N("Cancel")), clicked => sub { cancel_exit() }), #BUG- does not exists
                          gtksignal_connect(new Gtk2::Button(N("OK")), clicked => sub { save_exit() }), #BUG- does not exists
                         ),
               )
      );

# global notebook page
$main_notebook->append_page(my $global_notebook = new Gtk2::Notebook, Gtk2::Label->new(N("Global parameters")));
$global_notebook->set_tab_pos('right');
$global_notebook->append_page(gtkpack_(gtkset_border_width(Gtk2::VBox->new(0,5), 5),
                                       0, gtkadd(new Gtk2::Frame(N("General network parameters")),
                                                 gtkset_border_width(my $general_net_table = mycreate_packtable({},
                                                     [ getBestWidget($sys_network->{general}, '{NETWORKING}', 'Activate networking') ],
                                                     [ N("Hostname"), getBestWidget($sys_network->{general}, '{HOSTNAME}') ],
                                                     [ N("Gateway"), getBestWidget($sys_network->{general}, '{GATEWAY}') ],
                                                     [ N("Gateway device"), getBestWidget($sys_network->{general}, '{GATEWAYDEV}') ],
                                                     [ N("NIS domain"), getBestWidget($sys_network->{general}, '{NISDOMAIN}') ],
                                                     [ getBestWidget($sys_network->{general}, '{FORWARD_IPV4}', N("Forward IPV4")) ],
                                                     [ getBestWidget($sys_network->{general}, '{DEFRAG_IPV4}', N("Defrag IPV4")) ],
                                                     [ getBestWidget($sys_network->{general}, '{VLAN}', N("Virtual LAN")) ],
                                                                                        ), 5),
                                                ),
                                      ),
                              Gtk2::Label->new(N("General")),
                             );
$global_notebook->append_page(gtkpack_(gtkset_border_width(Gtk2::VBox->new(0,5), 5),
                                   0, gtkadd(new Gtk2::Frame(N("IPX")),
                                             gtkset_border_width(mycreate_packtable({},
                                                  [ getBestWidget($sys_network->{general}, '{IPX}', N("Activate IPX")) ],
                                                  [ N("IPX Auto primary"), getBestWidget($sys_network->{general}, '{IPXAUTOPRIMARY}') ],
                                                  [ N("IPX Auto frame"), getBestWidget($sys_network->{general}, '{IPXAUTOFRAME}') ],
                                                  [ N("IPX internal net num"), getBestWidget($sys_network->{general}, '{IPXINTERNALNODENUM}') ],
                                                  [ N("IPX internal node num"), getBestWidget($sys_network->{general}, '{IPXINTERNALNODENUM}') ],
                                                  [ N("IFPLUGD args"), getBestWidget($sys_network->{general}, '{IFPLUGD_ARGS}') ],
                                                 ), 5),
                                            ),
                                      ),
                              Gtk2::Label->new(N("IPX")),
                             );
$global_notebook->append_page(gtkpack_(gtkset_border_width(Gtk2::VBox->new(0,5), 5),
                    0, gtkadd(new Gtk2::Frame(N("IPV6")),
                              gtkset_border_width(mycreate_packtable({},
                                    [ getBestWidget($sys_network->{general}, '{NETWORKING_IPV6}', N("Activate IPV6")) ],
                                    [ getBestWidget($sys_network->{general}, '{IPV6FORWARDING}', N("IPV6 Forwarding")) ],
                                    [ getBestWidget($sys_network->{general}, '{IPV6INIT}', N("IPV6 Init")) ],
                                    [ getBestWidget($sys_network->{general}, '{IPV6_AUTOCONF}', N("IPV6 Autoconf")) ],
                                    [ getBestWidget($sys_network->{general}, '{IPV6_ROUTER}', N("IPV6 Router")) ],
                                    [ getBestWidget($sys_network->{general}, '{IPV6_AUTOTUNNEL}', N("IPV6 Autotunnel")) ],
                                    [ N("IPV6 Default gateway"), getBestWidget($sys_network->{general}, '{IPV6_DEFAULTGW}') ],
                                    [ N("IPV6 Default device"), getBestWidget($sys_network->{general}, '{IPV6_DEFAULTDEV}') ],
                                    [ N("IPV6 Radvd pidfile"), getBestWidget($sys_network->{general}, '{IPV6_RADVD_PIDFILE}') ],
                                    [ N("IPV6 to IPV4 Radvd pidfile"), getBestWidget($sys_network->{general}, '{IPV6TO4_RADVD_PIDFILE}') ],
                                    [ N("IPV6 Radvd trigger action"), getBestWidget($sys_network->{general}, '{IPV6_RADVD_TRIGGER_ACTION}') ],
                                   ), 5),
                             ),
                                      ),
                              Gtk2::Label->new(N("IPV6")),
                             );

# interfaces notebook page
my $int_clist_tree_model = Gtk2::ListStore->new('Glib::String', 'Glib::String', 'Glib::String');
$main_notebook->append_page(gtkpack_(gtkset_border_width(Gtk2::VBox->new(0,5), 5),
                                     0, gtkadd(Gtk2::Frame->new(N("Network Interfaces")),
                                               gtkpack__(gtkset_border_width(Gtk2::HBox->new(0,5), 5),
                                                         my $int_clist = Gtk2::TreeView->new_with_model($int_clist_tree_model),
                                                         gtkadd(gtkset_layout(my $int_button_box = new Gtk2::VButtonBox, 'end'),
                                                                gtksignal_connect(new Gtk2::Button(N("Add")), clicked => \&add_interface),
                                                                gtksignal_connect(new Gtk2::Button(N("Configure")), clicked => sub {}),
                                                                gtksignal_connect(new Gtk2::Button(N("Delete")), clicked => sub {}),
                                                               )
                                                        ),
                                              ),
                                    ), Gtk2::Label->new(N("Interfaces"))
                           );
$int_clist->append_column(Gtk2::TreeViewColumn->new_with_attributes(N("Name"), Gtk2::CellRendererText->new, 'text' => 0));
$int_clist->append_column(Gtk2::TreeViewColumn->new_with_attributes(N("IP address"), Gtk2::CellRendererText->new, 'text' => 1));
$int_clist->append_column(Gtk2::TreeViewColumn->new_with_attributes(N("Mask"), Gtk2::CellRendererText->new, 'text' => 2));

foreach my $key (keys %{$sys_network->{interfaces}}) {
    $int_clist_tree_model->append_set(0 => $key, 1 => $sys_network->{interfaces}{$key}{IPADDR}, 2 => $sys_network->{interfaces}{$key}{NETMASK});
}
# while (my ($key, $value) = %{$sys_network->{interfaces}}) {
#     $int_clist_tree_model->append_set(undef, [ 0 => $key, 1 => $value->{IPADDR}, 2 => $value->{NETMASK} ]);
# }
$int_clist->show_all;


# we make everything visible in the window
$window->show_all;

#and we start the Gtk2 main loop.
Gtk2->main;



sub add_interface() {
    require Libconf::Glueconf::Networking::Ifconfig;
    my $interface = new Libconf::Glueconf::Networking::Ifconfig();
    set_interface($interface, N("Add new network interface"));
}


sub set_interface {
    my ($interface, $title, $lock_device) = @_;
    $lock_device ||= 0;
    my $interface_notebook = gtkset_border_width(new Gtk2::Notebook, 5);
    $interface_notebook->append_page(
                                     gtkset_border_width(mycreate_packtable({},
                                           [ N("Device"), gtkset_sensitive(getBestWidget($interface, '{DEVICE}'), !$lock_device) ],
                                           [ N("Boot protocol"), getBestWidget($interface, '{BOOTPROTO }') ],
                                           [ N("IP address"), getBestWidget($interface, '{IPADDR}') ],
                                           [ N("Netmask"), getBestWidget($interface, '{NETMASK}') ],
                                           [ getBestWidget($interface, '{ONBOOT}', N("Start at boot")) ],
                                           [ getBestWidget($interface, '{MII_NOT_SUPPORTED}', N("Don't support mii tools")) ],
                                           [ N("DHCP client"), getBestWidget($interface, '{DHCP_CLIENT}') ],
                                           [ N("DHCP timeout"), getBestWidget($interface, '{DHCP_TIMEOUT}') ],
                                          ), 5),
                                     Gtk2::Label->new(N("Basic settings"))
                                    );
    $interface_notebook->append_page(
                                     gtkset_border_width(mycreate_packtable({},
                                           [ N("Name"), getBestWidget($interface, '{NAME}') ],
                                           [ N("Gateway"), getBestWidget($interface, '{GATEWAY}') ],
                                           [ getBestWidget($interface, '{USERCTL}', N("User Control")) ],
                                           [ N("MTU"), getBestWidget($interface, '{MTU}') ],
                                           [ getBestWidget($interface, '{PEERDNS}', N("Use peer DNS")) ],
                                           [ N("DNS 1"), getBestWidget($interface, '{DNS1}') ],
                                           [ N("DNS 2"), getBestWidget($interface, '{DNS2}') ],
                                           [ getBestWidget($interface, '{FIREWALL_MODS}', N("Modify Firewall")) ],
                                           [ N("SRC address"), getBestWidget($interface, '{SRCADDR}') ],
                                          ), 5),
                                     Gtk2::Label->new(N("Advanced settings"))
                                    );
    if (create_dialog($title,
                      $interface_notebook,
                      { stock => 'gtk-dialog-info',
                        cancel => 1,
                      })) {
        $sys_network->{interfaces}{$interface->{DEVICE}} = $interface;
        print Dumper($sys_network) . "\n";
    }
}

sub mycreate_packtable {
    my ($options, @l) = @_;
    my $w = Gtk2::Table->new(0, 0, $options->{homogeneous} || 0);
    each_index {
	my ($i, $l) = ($::i, $_);
        if (@$l == 1) {
            $w->attach($l->[0], 0, 2, $i, $i + 1, 'fill', 'fill', 5, 0);
        } else {
            each_index {
                my $j = $::i;
                if ($_) {
                    ref $_ or $_ = gtkpack_(Gtk2::HBox->new(0,0),
                                            1, Gtk2::HBox->new(0,0),
                                            0, Gtk2::Label->new($_)
                                           );
                    $w->attach($_, $j, $j + 1, $i, $i + 1, 'fill', 'fill', 5, 0);
                    $_->show;
                }
            } @$l;
        }
    } @l;
    $w->set_col_spacings($options->{col_spacings} || 0);
    $w->set_row_spacings($options->{row_spacings} || 5);
    $w
}
