#!/usr/bin/perl
##################################################
# tkpauk -- Mike Schilli, 2001 (m@perlmeister.com)
##################################################
use warnings;
use strict;

use Tk;
use Tk::FileSelect;

my $width   = 400;   # Fensterbreite
my $lheight = 10;    # Texthhe
my $lwidth  = 50;    # Textbreite

my @SHUFFLED;
my %TRANS;

my $TOP     = MainWindow->new(-width => $width);
my $menubar = $TOP->Menu(-type => 'menubar');
my $f       = $menubar->cascade(-label => 'File');

$f->command(-label   => 'Open', 
            -command => \&file_select);
$f->command(-label => 'Exit', 
            -command => sub { exit(0) });
$TOP->configure(-menu => $menubar);

$f->configure(-background => "orangered");

my $QUESTION = $TOP->Label(
    -width      => $lwidth, 
    -height     => $lheight, 
    -wraplength => 7/10*$width,
)->pack();

my $FOOTLINE = $TOP->Frame();
my $COUNTER  = $FOOTLINE->Label();
my $MESSAGE  = $FOOTLINE->Label();

my $ANSWER = $TOP->Label(
    -width      => $lwidth, 
    -height     => $lheight,
    -wraplength => 7/10*$width,
)->pack();

$MESSAGE->pack(-expand => 'yes', -side => 'left');
$COUNTER->pack(-expand => 'no', -anchor => 'e');
$FOOTLINE->pack(-expand => 'yes', -fill => 'x');

state_init();
MainLoop();

##################################################
sub footline {
##################################################
    my($text) = @_;

    my $nof_items = @SHUFFLED;
    $COUNTER->configure(-text => $nof_items);
    $COUNTER->configure(-foreground => "blue");
    $COUNTER->configure(-background => "orange");
#    $MESSAGE->configure(-foreground => 
    $MESSAGE->configure(-foreground => 
                        "orangered");
    $MESSAGE->configure(-background => 
                        "green");
    $MESSAGE->configure(-text => "$text");
}

##################################################
sub file_select {
##################################################
    my $f = $TOP->FileSelect(-directory => ".",
                             -filter => '*.txt');
    my $file = $f->Show();
    state_ask(), return unless defined $file;

    %TRANS = ();

    open DAT, "<$file" or die "Cannot open $file";
    while(my $line = <DAT>) {
        my($q, $a) = split /\s*--\s*/, $line, 2;
        $TRANS{$q} = $a;
    }
    close DAT;
    @SHUFFLED = shuffle(keys %TRANS);
    state_ask();
}

##################################################
sub shuffle {
##################################################
    my @array = @_;
    for(my $i=@array; --$i; ) {
        my $j = int rand ($i+1);
        next if $i == $j;
        @array[$i,$j] = @array[$j,$i];
    }
    return @array;
}

##################################################
sub state_init {
##################################################
    footline("Please select a data file");
    $QUESTION->configure(-text => "");
    $QUESTION->configure(-foreground => "red");
    $QUESTION->configure(-background => "yellow");
    $ANSWER->configure(-text => "");
    $ANSWER->configure(-foreground => "yellow");
    $ANSWER->configure(-background => "orange");

    map { $TOP->bind("<$_>" => "") } 
               qw(Return space Button-1 Button-3);

    file_select();
}

##################################################
sub state_ask {
##################################################
    return unless @SHUFFLED;
    my $S = $SHUFFLED[0];
    $S =~ s/, /
/mg;
    $S =~ s+/+ABCDEF+;
    $S =~ s+/ere+ABCDEFere+;
    $S =~ s+/++g;
    $S =~ s+ABCDEF+/+g;
    $S =~ s+\|++g;

    footline("Enter: See translation");
    $QUESTION->configure(-text => $S);
    $ANSWER->configure(-text => "");

    $TOP->bind("<Key-q>"  => \&state_init);

    map { $TOP->bind("<$_>" => \&state_show) } 
               qw(Return space Button-1 Button-3);
}

##################################################
sub state_show {
##################################################
    footline(
      "Enter: Item processed   Space: Keep item");
    $QUESTION->configure(-text => $SHUFFLED[0]);
    print("$SHUFFLED[0]: ");
    $ANSWER->configure(-text => 
                       $TRANS{$SHUFFLED[0]});

    $TOP->bind("<Button-1>" => \&success);
    $TOP->bind("<Return>"   => \&success);
    $TOP->bind("<Button-3>" => \&failure);
    $TOP->bind("<space>"    => \&failure);
}

##################################################
sub state_congrats {
##################################################
    footline("");
    $QUESTION->configure(-text => 
                         "Congratulations!!!");
    $ANSWER->configure(-text => "");
    map { $TOP->bind("<$_>" => \&state_init) } 
               qw(Return space Button-1 Button-3);
}

##################################################
sub success {
##################################################
    shift @SHUFFLED;
    print("Gewusst!\n");
    @SHUFFLED ? state_ask() : state_congrats();
}

##################################################
sub failure {
##################################################
    my $current = shift @SHUFFLED;
    print("Verloren!\n");
    push @SHUFFLED, $current;
    state_ask();
}
