#!/usr/bin/python3

from random import randint, random, choice

from crime_campus_exam_data import greeting, network, hidden_data

def resolve_language(text, language):
	if isinstance(text, str):
		return text
	if isinstance(text, dict):
		return text[language]
	german, english = text
	if language == "de":
		return german
	else:
		return english

def get_both_languages(text):
	if isinstance(text, str):
		return text, text
	if isinstance(text, dict):
		return text["de"], text["en"]
	german, english = text
	return german, english

def generate_text(number_of_lines = 200, language = None):
	""" language can be None (both), "en" or "de".
	"""
	local_hidden_data = hidden_data[:]
	do_german = not language or language == "de"
	do_english = not language or language == "en"
	result = ""
	for i in range(number_of_lines):
		emissions, _ = network.choose()
		german = ""
		english = ""
		for emission in emissions:
			ger, eng = get_both_languages(emission)
			german += ger
			english += eng
		if do_german: result += german + "\n"
		if do_english: result += english + "\n"
		if random() < len(local_hidden_data) / (number_of_lines - i):
			index = randint(0, len(local_hidden_data) - 1)
			hidden_line = local_hidden_data.pop(index)
			ger, eng = get_both_languages(hidden_line)
			if do_german: result += german + "\n"
			if do_english: result += english + "\n"
	return result

def read_template(filename = "./template.html"):
	with open(filename, "r") as file:
		return file.read()

def write_index(text, filename = "./deploy/index.html"):
	with open(filename, "w") as file:
		file.write(text)

def main():
	template = read_template()
	for language in ["de", "en"]:
		generated = generate_text(language = language)
		generated = resolve_language(greeting, language) + "\n" + generated
		text = template.replace("<replace with text>", generated)
		write_index(text, filename = f"./deploy/{language}/index.html")

if __name__ == "__main__":
	main()

