#!/usr/local/bin/cz --
use b

# What is a common free font in X11 and preferably windoze etc too????
# Or, just configure it?

def font_family "helvetica"  # "arial"

Main()
	num sleep_word_start = 1
	num sleep_after_letter = 0
	num sleep_after_word = 0
	bit opt_word = 1
	bit opt_word_rand = 1
	bit opt_rand = 0
	bit opt_rand_repeat = 0
	bit opt_alphabet = 0
	bit opt_repeat = 1
	bit opt_upcase = 1
	bit opt_word_spell_1 = 1
	bit opt_word_say_2 = 1
	bit opt_word_spell_2 = 0
	cstr launch_browser = "chrome"
#	cstr launch_search = "http://www.google.com/search?tbm=isch&hl=en&source=hp&biw=1024&bih=536&q=pony&gbv=2&oq=pony&aq=f&aqi=&aql=&gs_sm=e&gs_upl=1980l2478l0l2700l4l4l0l0l0l0l0l0ll0l0"
	cstr launch_search = "http://www.google.com/search?tbm=isch&hl=en&source=hp&biw=1024&bih=536&q=%s&gbv=2&aq=f&aqi=&aql=&gs_sm=e"
#	cstr launch_search = NULL
	int rand_n = 26  #  < 26!
	cstr word = "CAT"
	cstr word_list = "words"

	Chdir(program_dir)

	Setenv("word_list", word_list)

	space(1000, 500, blue)
	key_handlers_ignore()
	key_handler("Escape", KeyPress) = thunk(quit)

	repeat:
		learn_letters()
		if !opt_repeat:
			break

	gr_exit()

def learn_letters()
	if opt_word_rand:
		if opt_upcase:
			word = cmd("< $word_list grep -v '^#' | fortune.b | tr 'a-z' 'A-Z' | tr -dc 'A-Z'")
		 else:
			word = cmd("< $word_list grep -v '^#' | fortune.b | tr -dc 'A-Za-z'")

	char *word_p = word
	int good = 0, tries = 0
	char done[256]
	zero(done, done+sizeof(done))

	if opt_word:
		font_size_to_fit(word)
		gprint_anchor(-1, -0.3)
	 else:
		font_size_to_fit("M")
		gprint_anchor(0, -0.3)

	if opt_word:
		clear()
		move(-w_2, 0)
		white()
		gsay(word)
		paint()
		speak_word(word)
		if opt_word_spell_1:
			speak_letters(word)
			speak_word(word)
		Rsleep(sleep_word_start)

	char s[2] = "A"
	int keycode
	cstr entry
	repeat:
		clear()
		if opt_word:
			move(-w_2, 0)
		 else:
			home()
		if opt_rand:
			repeat:
				*s = Randi('A', 'Z'+1)
				if opt_rand_repeat || !done[(uchar)*s]:
					done[(uchar)*s] = 1
					break
		 eif opt_alphabet:
			.
		 eif opt_word:
			*s = *word_p
		if opt_word:
			white()
			for_cstr(p, word):
				if p == word_p:
					rainbow(Rand(-180, 180))
				gprintf("%c", *p)
				if p == word_p:
					white()
		 else:
			rainbow(Rand(-180, 180))
			gsay(s)
		paint()
		repeat:
			speak_letter_bg(*s)
			keycode = gr_getc()
			entry = key_string(keycode)
#			warn("%d %s %d", keycode, entry, strlen(entry))
			++tries
			if cstr_case_eq(entry, s):
				speak(entry)
				++good
				break
			 else:
				cstr caps_entry = Strdup(entry)
				*caps_entry = toupper(*caps_entry)
				cstr no = Format("No, that's \"%s!\"", caps_entry)
				speak(no)
				Free(no)
		Rsleep(sleep_after_letter)
		if opt_alphabet:
			++*s
			if *s > 'Z':
				break
		 eif opt_word:
			++word_p
			if !*word_p
				break
		 eif opt_rand:
			if good == rand_n:
				break

	speak("You did it!")

	if opt_word:
		clear()
		move(-w_2, 0)
		white()
		gsay(word)
		paint()
		if opt_word_say_2:
			speak_word(word)
			if opt_word_spell_2:
				speak_letters(word)
				speak_word(word)

	if good == tries:
		speak("Perfect! Hip Hip Hoo-ray!")
	 else:
		cstr score = Format("%d out of %d!", good, tries)
		speak(score)
		warn(score)
		Free(score)
		speak("Well done!")

	if opt_word && launch_browser && launch_search:
		cstr url = Format(launch_search, word)
		Systeml(launch_browser, url)

	if sleep_after_word >= 0:
		Rsleep(sleep_after_word)
	else:
		gr_getc()

	if opt_word_rand:
		Free(word)
		word = "CAT"

def gr_exit()
	gr_exit(0)

int font_size_to_fit(cstr s)
	font(font_family "-medium", 200)
	num tw = text_width(s)
	num th = font_height()
	int fsw = (int)(200*w/tw * 0.90)
	int fsh = (int)(200*h/th * 0.90)
	int fs = imin(fsw, fsh)
	font(font_family "-medium", fs)
	return fs

speak_letters(cstr s):
	new(b, buffer, 256)
	for_cstr(p, s):
		Sprintf(b, "%c! ", *p)
	speak(buffer_to_cstr(b))
	buffer_free(b)

speak_letter_bg(char c)
	if Fork() == 0:
		speak_letter(c)
		Exit()

speak_letter(char c)
	char s[3] = "A!"
	s[0] = c
	speak(s)

speak_word(cstr s):
	cstr s_emp = Format("%s!", s)
	speak(s_emp)
	Free(s_emp)

speak(cstr s)
	new(b, buffer, 256)
	system_quote(s, b)
	cstr q = buffer_to_cstr(b)
	Systemf("echo %s | espeak 2>/dev/null", q)
	buffer_free(b)
