#!/usr/bin/wish -f

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

wm title . "Héber Kikérdező"

font create hebrew_font -size 12

label .definition -textvariable definition -width 50
entry .userword -textvariable userword -width 50 -justify center \
    -font hebrew_font
label .solution -textvariable solution -font hebrew_font
label .pronounciation -textvariable pronounciation -font hebrew_font
button .checknext -text Check -command checkNext

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

bind . <Control-r> reverse_entry
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 pro ""
        set def ""
        gets $f line
        regexp -- "^(.*) = (.*) = (.*)$" $line match sol pro def
        if {[string length $match] != 0} {
            set dictionary($number_of_entries) [list $def $sol $pro]
            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 reverse {s} {
    set result ""
    for {set i [string length $s]} {$i >= 0} {incr i -1} {
        append result [string index $s $i]
    }
    return $result
}

proc reverse_entry {} {
    global userword
    set userword [reverse $userword]
}

proc checkNext {} {
    global check_or_next number_of_entries dictionary next
    global userword definition solution pronounciation
    if {$check_or_next == "check"} {
        # Check pressed
        set solution [reverse [lindex $dictionary($next) 1]]
        .userword configure -state readonly
        .checknext configure -text "Show pronounciation"
        set check_or_next show_pro
    } elseif {$check_or_next == "show_pro"} {
        # Pron pressed
        set pronounciation [lindex $dictionary($next) 2]
        .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 ""
        set pronounciation ""
        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