#!/usr/bin/wish -f

#######
# GUI #
#######

wm title . "Latin Kikérdező"

font create latin_font -size 14 -family "Helvetica"

label .definition -textvariable definition -width 50 -anchor w
entry .userword -textvariable userword -width 50 -font latin_font
text .solution -width 50 -height 1 -state disabled -font latin_font
button .checknext -text Check -command checkNext

pack .definition .userword .solution .checknext -fill x

bind . <Return> checkNext
bind . <Escape> exit

##############
# Procedures #
##############

proc loadDictionary { filename } {
    global number_of_entries dictionary next
    set number_of_entries 0

    set f [open $filename]
    while {![eof $f]} {
        set match ""
        set sol ""
        set def ""
        gets $f line
        regexp -- "^(.*) = (.*)$" $line match sol def
        if {[string length $match] != 0} {
            set dictionary($number_of_entries) [list $def $sol]
            incr number_of_entries
        }
    }
    close $f
    tk_messageBox -message "$number_of_entries dictionary entries loaded." \
        -title "Words" -parent . -type ok

    set next $number_of_entries;        # force a new random order
}

proc randomizeDictionary {} {
    global number_of_entries dictionary
    for {set i 0} {$i < [expr $number_of_entries - 1]} {incr i} {
        set j [expr $i + int(($number_of_entries - $i - 1) * rand()) + 1]
        set tmp $dictionary($j)
        set dictionary($j) $dictionary($i)
        set dictionary($i) $tmp
    }
}

proc checkNext {} {
    global check_or_next number_of_entries dictionary next
    global userword definition solution
    if {$check_or_next == "check"} {    # Check pressed
        set sol [lindex $dictionary($next) 1]
        set solution [regsub -all "\"" $sol ""]
        .solution configure -state normal
        .solution delete 1.0 end
        .solution insert end $solution
        .solution tag delete emphasis
        .solution tag configure emphasis -foreground red
        .solution configure -state disabled
        set emph -1
        set num 1
        while {[set emph [string first "\"" $sol [expr $emph + 1]]]
               >= 0} {
            .solution tag add emphasis "1.[expr $emph - $num]"
            incr num
        }
        .userword configure -state readonly
        .checknext configure -text Next
        set check_or_next next
    } else {                            # Next pressed
        incr next
        if {$next >= $number_of_entries} {
            randomizeDictionary
            set next 0
        }

        set definition [lindex $dictionary($next) 0]
        set solution ""
        .solution configure -state normal
        .solution delete 1.0 end
        .solution configure -state disabled
        set userword ""

        .userword configure -state normal
        .checknext configure -text Check
        set check_or_next check
    }
}

##################
# Initialization #
##################

set check_or_next next
loadDictionary "szotar"
checkNext
focus .userword