# Kufr, Miroslav Novak 2003

set totaltime 91
set totalwordsused 20

package require Iwidgets 4.0

set wordsused 0
set words {}
set vtime 0
set vscore 0
set vword "-- Kufr --"
set timer 0
set fileName ""


proc openfile {} {
	global fileName words vword score time timer vtime
	set fn [tk_getOpenFile]
	if {[string equal "" $fn]} return
	set fileName $fn
	set fil [open $fn r]
	set words {}
	while {![eof $fil]} {
		set w [string trim [gets $fil]]
		if {![string equal "" $w]} {
		        lappend words $w
		}
	}
	updatetitle
	set vword "-- začínáme --"
	set score 0
	set vtime 0
	if {$timer !=0 } {
		after cancel $timer
		set timer 0
	}
}

proc updatetitle {} {
	global words fileName
	set wfree 0
	foreach w $words {
		if {![string equal * [string index $w 0]]} {incr wfree}
	}
	wm title . "Kufr $fileName [llength $words] / $wfree"
}

proc start {} {
	global vscore vtime totaltime timer wordsused
	if {$timer !=0 } {
		after cancel $timer
		set timer 0
	}
	set vscore 0
	set vtime $totaltime
	countdown
	set wordsused 0
	nextword
}

proc countdown {} {
	global vtime timer vword
	incr vtime -1
	if {$vtime <= 0} {
		set vword "-- čas vypršel --"
		set timer 0
		finish
	} else {
		set timer [after 999 [info level 0]]
	}
}


proc nextword {{inc 0}} {
	global vtime words vword fileName wordsused totalwordsused vscore
	if {$vtime <= 0} return
	incr vscore $inc
	if {$wordsused >= $totalwordsused} {
		set vword "-- příděl slov vyčerpán --"        
		finish
		return
	}
	set wfree 0
	foreach w $words {
		if {![string equal * [string index $w 0]]} {incr wfree}
	}
	updatetitle
	if {$wfree == 0} {
		set vword "-- nejsou další slova !!! --"
		finish
		return
	}
	set sel [expr int(rand()*$wfree)]
	for {set i 0} {$i < [llength $words]} {incr i} {
		set w [lindex $words $i]
		if {![string equal * [string index $w 0]]} {
		        if {$sel == 0} {
		                break
		        } else {
		                incr sel -1
		        }
		}
	}
	if {$sel != 0} {error "nesedi slovicka"}
	set vword $w
	lset words $i "*$w"
	
	incr wordsused
	#slovicka savneme kvuli hvezdicce
	set fil [open $fileName w]
	puts $fil [join $words "\n"]
	close $fil
}

proc finish {} {
	global timer vname vscore
	if {$timer !=0 } {
		after cancel $timer
		set timer 0
	}
	# zapis vysledku
	set vname ""
	toplevel .resultdlg
	label .resultdlg.labtitle -text "Konec hry"
	frame .resultdlg.fr1
	label .resultdlg.fr1.labname -text "Jméno hráče:"
	entry .resultdlg.fr1.entname -textvariable vname 
	frame .resultdlg.fr2
	button .resultdlg.fr2.butok -text OK -command {destroy .resultdlg}
	button .resultdlg.fr2.butcancel -text Zrušit -command {destroy .resultdlg; set vname ""}
	button .resultdlg.fr2.plus -text "+" -command {incr vscore}
	button .resultdlg.fr2.minus -text "-" -command {incr vscore -1}
	pack .resultdlg.fr1.labname .resultdlg.fr1.entname -side left -expand 1 -fill x
	pack .resultdlg.fr2.butok .resultdlg.fr2.butcancel .resultdlg.fr2.plus .resultdlg.fr2.minus -side left -expand 1 -fill x
	pack .resultdlg.labtitle .resultdlg.fr1 .resultdlg.fr2 -side top -pady 20
	grab .resultdlg
	focus .resultdlg
	tkwait window .resultdlg
	if {[string equal "" [string trim $vname]]} {return}
	set fres [open "vysledky.txt" a]
	puts $fres [format "%s:%d" [string trim $vname] $vscore]
	close $fres
}

proc showresults {} {
	if {[catch {set fres [open "vysledky.txt" r]}]} return
	
	toplevel .resultswin
	
	set cs .resultswin

	#iwidgets::scrolledframe .resultswin.sf -hscrollmode dynamic -vscrollmode dynamic -width 600 -height 700
	#pack .resultswin.sf -expand 1 -fill both
	#set cs [.resultswin.sf childsite]
	
	grid [label $cs.title -text Výsledky] -  -padx 20 -pady 20
	
	set lres {}
	while {![eof $fres]} {
		set rr [string trim [gets $fres]]
		if {![string equal "" $rr]} {
		        lappend lres [split [string trim $rr] :]
		}
	}
	close $fres
	set lres [lsort -index 1 -decreasing -integer $lres]
	set rowcnt 0
	set rownums {}
	foreach rl $lres {
		grid [label $cs.name$rowcnt -text [lindex $rl 0]] \
		        [label $cs.score$rowcnt -text [lindex $rl 1]]\
		         -padx 40 -pady 5
		lappend rownums $rowcnt
		incr rowcnt
	}
	foreach num $rownums {
		grid configure $cs.name$num -sticky w
	}

	grab .resultswin
	focus .resultswin
}


option add *font {Courier 30 bold}
label .word -textvariable vword -font {Arial 70} -bg white
frame .fr1
label .fr1.score -textvariable vscore -font {Courier 60 bold}
label .fr1.time -textvariable vtime -font {Courier 60 bold}
frame .fr2
button .fr2.nextok -text Správně -command {nextword 1} -foreground green
button .fr2.nextbad -text Špatně -command nextword -foreground yellow
button .fr2.start -text Start -command start
button .fr2.open -text Slova -command openfile
button .fr2.results -text Výsledky -command showresults
pack .word -expand 1 -fill both
pack .fr1 .fr2 -side top -fill x 
pack .fr1.score .fr1.time -side left -expand 1 -fill x
pack .fr2.nextok .fr2.nextbad .fr2.start .fr2.open .fr2.results -side left -expand 1 -fill x 







