# -*- Perl -*-
#
# X-Rust Filter Definitions and Subroutines
#
# $Id: xrust_configure.pl,v 1.2 2013/07/30 20:49:01 zmiller Exp $

#use strict;
use vars qw(%FILTER $globals %COLORS @caller_queues %FONTS, %GEOMETRY %INITIAL %MAIL_CONFIG);

sub create_configure_window {

    my ($exit_on_close) = shift;

    my $CONFIG_WINDOW = MainWindow->new;
    $CONFIG_WINDOW->title("X-Rust Configuration Panel");
    push(@{$COLORS{'Default'}->{'widgets'}},$CONFIG_WINDOW);

    $CONFIG_WINDOW->resizable(0,0);
    
    my $panel = &create_config_panel($CONFIG_WINDOW);
    $panel->pack(-side => 'top', -fill => 'x', -expand => 1);
    
    # ***** Buttons *****
    
    my $button_frame = $CONFIG_WINDOW->Frame;

    $button_frame->pack( -side => 'top', -fill => 'x', -expand => 1);
    push(@{$COLORS{'Default'}->{'widgets'}},$button_frame);
    
    my $xrust_save_button = $button_frame->Button(
	-text => "Save Configs",
	-font => $FONTS{'BUTTON_FONT'},
	-command => \&save_xrustrc
					    );
    $xrust_save_button->pack( -side => 'left', -fill => 'x' , -expand => 1);
    push(@{$COLORS{'Buttons'}->{'widgets'}},$xrust_save_button);

    my $button_close = $button_frame->Button(
					     -text => "Close",
					     -font => $FONTS{'BUTTON_FONT'},
					     -command => sub { 
						 $CONFIG_WINDOW->destroy;
						 if ($exit_on_close) {
						     exit(0);
						 }
					     }
					    );
    $button_close->pack( -side => 'left', -fill => 'x', -expand => 1);
    push(@{$COLORS{'Buttons'}->{'widgets'}},$button_close);
    
    $CONFIG_WINDOW->OnDestroy([\&delete_from_color,
			      $CONFIG_WINDOW,
			      $CONFIG_WINDOW->Descendants]);
    my $style;
    foreach $style (keys %COLORS) {
	&colorize_widgets($style);
    }

}

sub delete_from_color {
    my($l,$a,$b,$c,@wid);
    my @kk = @_;

    foreach $c (keys %COLORS) {
	foreach $l (keys %{$COLORS{$c}}) {
	    if ($l eq 'widgets') {
		print XRUSTRC"\$COLORS{'$c'}->{'$l'} = '$COLORS{$c}->{$l}';\n";
		@wid = @{$COLORS{$c}->{$l}};
		foreach $a (0..$#wid) {
		    foreach $b (0..$#kk) {
			#print "$wid[$a] =? $kk[$b]\n";
			if (@wid[$a] eq @_[$b]) {
			    #print "MATCH: $a -- @_[$b]\n";
			    $COLORS{$c}->{$l}->[$a] = "";
			}
		    }
		}
	    }
	}
    }
}

sub create_config_panel {
    
    my $MW = shift;

    my ($each,$count,$radial);

    my $topframe = $MW->Frame;
    push(@{$COLORS{'Default'}->{'widgets'}},$topframe);

    my $column1 = $topframe->Frame;
    my $column2 = $topframe->Frame;
 
    my @column_packing = (-side => 'left', -fill => 'both', -pady => 5, -padx =>  5);

    $column1->pack(@column_packing);
    $column2->pack(@column_packing);

    push(@{$COLORS{'Default'}->{'widgets'}},$column1);
    push(@{$COLORS{'Default'}->{'widgets'}},$column2);

    my @frame_packing = (
			 -side => 'top',
			 -fill => 'x',
			 -pady => 1
			 );

    # **** Outgoing mail signature file ****

    my $signature_frame = $column1->Frame;
    $signature_frame->pack(@frame_packing);

    &create_box_label($signature_frame,"Mail signature file");
    my $signature_entry = $signature_frame->Entry(
	-relief => 'sunken',
	-width  => 30,
	-textvariable => \$MAIL_CONFIG{'SIGNATURE'},
	);

    $signature_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$signature_entry);

    # **** Xrust password ****

    my $xrust_pass_frame = $column1->Frame;
    $xrust_pass_frame->pack(@frame_packing);

    &create_box_label($xrust_pass_frame,"X-Rust Header Password");
    my $xrust_pass_entry = $xrust_pass_frame->Entry(
	-relief => 'sunken',
	-width  => 30,
	-textvariable => \$MAIL_CONFIG{'XRUST_PASSWORD'},
	);

    $xrust_pass_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$xrust_pass_entry);


    # **** Text Font ****

    my $text_font_frame = $column1->Frame;
    $text_font_frame->pack(@frame_packing);

    &create_box_label($text_font_frame,"Text Font");
    my $text_font_entry = $text_font_frame->Entry(
					-relief => 'sunken',
					-width  => 30,
					-textvariable => \$FONTS{'TEXT_FONT'},
					);
    $text_font_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$text_font_entry);

    # **** Button Font ****

    my $button_font_frame = $column1->Frame;
    $button_font_frame->pack(@frame_packing);

    &create_box_label($button_font_frame,"Button Font");
    my $button_font_entry = $button_font_frame->Entry(
	-relief => 'sunken',
	-width  => 30,
	-textvariable => \$FONTS{'BUTTON_FONT'},
					  );

    $button_font_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$button_font_entry);

    # **** Menu Font ****

    my $menu_font_frame = $column1->Frame;
    $menu_font_frame->pack(@frame_packing);

    &create_box_label($menu_font_frame,"Menu Font");
    my $menu_font_entry = $menu_font_frame->Entry(
	-relief => 'sunken',
	-width  => 30,
	-textvariable => \$FONTS{'MENU_FONT'},
	);

    $menu_font_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$menu_font_entry);

    # **** Main Geometry ****

    my $main_geometry_frame = $column2->Frame;
    $main_geometry_frame->pack(@frame_packing);

    &create_box_label($main_geometry_frame,"Main Geometry");
    my $main_geometry_entry = $main_geometry_frame->Entry(
	-relief => 'sunken',
	-width  => 30,
	-textvariable => \$GEOMETRY{'MAIN'},
	);

    $main_geometry_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$main_geometry_entry);

    # **** Comment Geometry ****

    my $comment_geometry_frame = $column2->Frame;
    $comment_geometry_frame->pack(@frame_packing);

    &create_box_label($comment_geometry_frame,"Comment Geometry");
    my $comment_geometry_entry = $comment_geometry_frame->Entry(
	-relief => 'sunken',
	-width  => 30,
	-textvariable => \$GEOMETRY{'COMMENT'},
	);

    $comment_geometry_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$comment_geometry_entry);


    # **** Mail Geometry ****

    my $mail_geometry_frame = $column2->Frame;
    $mail_geometry_frame->pack(@frame_packing);

    &create_box_label($mail_geometry_frame,"Mail Geometry");
    my $mail_geometry_entry = $mail_geometry_frame->Entry(
	-relief => 'sunken',
	-width  => 30,
	-textvariable => \$GEOMETRY{'MAIL'},
	);

    $mail_geometry_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$mail_geometry_entry);


    # **** Initial Queue ****

    my $initial_q_frame = $column2->Frame;
    $initial_q_frame->pack(@frame_packing);

    &create_box_label($initial_q_frame,"Initial Queue");
    my $initial_q_entry = $initial_q_frame->Entry(
	-relief => 'sunken',
	-width  => 30,
	-textvariable => \$INITIAL{'QUEUE'},
	);

    $initial_q_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$initial_q_entry);


    # **** Initial Owner ****

    my $initial_owner_frame = $column2->Frame;
    $initial_owner_frame->pack(@frame_packing);

    &create_box_label($initial_owner_frame,"Initial Owner");
    my $initial_owner_entry = $initial_owner_frame->Entry(
	-relief => 'sunken',
	-width  => 30,
	-textvariable => \$INITIAL{'OWNER'},
	);

    $initial_owner_entry->pack(-side => 'top', -fill => 'x');
    push(@{$COLORS{'Entry'}->{'widgets'}},$initial_owner_entry);


    # **** Match Unowned ****

    my $match_unowned_frame = $column2->Frame;
    $match_unowned_frame->pack(@frame_packing);

    &create_box_label($match_unowned_frame,"Match Unowned");
    my $match_unowned_yes = $match_unowned_frame->Radiobutton(
							      -text     => "Yes",
							      -font     => $FONTS{'BUTTON_FONT'},
							      -variable => \$INITIAL{'UNOWNED'},
							      -value    => 1,
							      );
    $match_unowned_yes->pack(-side => 'top', -anchor => 'w');
    push(@{$COLORS{'Radial'}->{'widgets'}},$match_unowned_yes);



    my $match_unowned_no = $match_unowned_frame->Radiobutton(
							     -text     => "No",
							     -font     => $FONTS{'BUTTON_FONT'},
							     -variable => \$INITIAL{'UNOWNED'},
							     -value    => 0,
							     );
    $match_unowned_no->pack(-side => 'top', -anchor => 'w');
    push(@{$COLORS{'Radial'}->{'widgets'}},$match_unowned_no);

    return $topframe;

}

sub create_box_label {
    my $frame = shift;
    my $text = shift;
    
    my $label = $frame->Label(
			      -text => "$text",
			      -font => $FONTS{'TEXT_FONT'},
			      -relief => 'ridge'
			      );

    $label->pack(
		 -side => 'top',
		 -fill => 'x'
		 );
    push(@{$COLORS{'Labels'}->{'widgets'}},$label);
    return $label;
}



1;
