" . PHP_EOL .
			"" . PHP_EOL .
			"	
" . PHP_EOL .
			"		 " . PHP_EOL .
			"		 " . PHP_EOL .
			"		 " . PHP_EOL .
			"		" . PHP_EOL .
			"		" . $title . " " . PHP_EOL .
			"	" . PHP_EOL .
			"	" . PHP_EOL;
		$post =
			"	" . PHP_EOL .
			"" . PHP_EOL;
		$html5 = $pre . $html_data . $post;
		return $html5;
	}
	/*
	 * Writes HTML table with the following info:
	 * Token + shortened pubkey | Name | Description | Users | View Links(?) | Join URL
	 */
	function get_table_html($info_arrays) {
		$table_lines = array();
		foreach($info_arrays as $id => $content) {
			/*
			 * $id is "room token+shortened_pubkey", e.g. "example+09af"
			 * Each $content looks like this:
			 * $info_array = array(
			 * 		"name"         => "Name of the room",
			 * 		"language"     => "🇩🇪",
			 * 		"description"  => "Some text that describes the community",
			 * 		"active_users" => 1234,
			 * 		"preview_link" => "https://example.com/r/example",
			 * 		"join_link"    => "https://example.com/example?public_key=[64_hex_chars]"
			 * );
			 */
			$exploded = explode("/", $content["join_link"]);  // https: + "" + 1.2.3.4:56789 + token?public_key=0123456789abcdef
			$server_url = $exploded[0] . "//" . $exploded[2]; // extract server_url
			$token  = explode("?", $exploded[3])[0];          // extract token
			$line =
				"	" . PHP_EOL .
				"		" . $id . " " . PHP_EOL .
				"		" . $content["language"] . " " . PHP_EOL .
				"		" . $content["name"] . " " . PHP_EOL .
				"		" . $content["description"] . " " . PHP_EOL .
				"		" . $content["active_users"] . " " . PHP_EOL .
				"		" . $content["preview_link"] . "  " . PHP_EOL .
				"		 " . PHP_EOL .
				"		" . $content["join_link"] . "  " . PHP_EOL .
				"	 " . PHP_EOL;
			$table_lines[] = $line;
		}
		// prefix
		$prefix =
			"Session Communities " . PHP_EOL .
			"" . PHP_EOL .
			"	" . PHP_EOL .
			"		Identifier " . PHP_EOL .
			"		L " . PHP_EOL .
			"		Name " . PHP_EOL .
			"		Description " . PHP_EOL .
			"		Users " . PHP_EOL .
			"		Preview " . PHP_EOL .
			"		QR " . PHP_EOL .
			"		Join URL " . PHP_EOL .
			"	 " . PHP_EOL;
		// suffix
		$suffix =
			"
" . PHP_EOL .
			"" . PHP_EOL;
		// concatenate html
		$html = $prefix;
		foreach($table_lines as $line) {
			$html = $html . $line;
		}
		$html = $html . $suffix;
		return $html;
	}
	/*
	 * Needed until all Community servers reliably generate QR codes again
	 */
	function get_qr_img_element_from_join_url($join_url) {
		$data = get_base64_qr_code_from_join_url($join_url);
		$mime = "image/png";
		$src = "data:" . $mime . ";base64," . $data;
		$result =
			" ";
//		echo($result . PHP_EOL);
		return $result;
	}
	/*
	 * Use Google API to generate QR codes and encode them as base64
	 */
	function get_base64_qr_code_from_join_url($join_url) {
		// https://developers.google.com/chart/infographics/docs/qr_codes
		$data =  urlencode($join_url);
		$size = "512x512";
		$api_url =
			"https://chart.googleapis.com/chart?cht=qr" .
			"&chs=" . $size .
			"&chl=" . $data .
			"&chld=L|0"; // error correction level: L = 7%, M = 15%, Q = 25%, H = 30% | margin in number of rows
		$img_base64 = base64_encode(file_get_contents($api_url));
//		echo($img_base64 . PHP_EOL);
		return $img_base64;
	}
	/*
	 * TODO: Description
	 */
	function create_qr_code_modals_html($info_arrays) {
		$html = "";
		foreach($info_arrays as $id => $content) {
			/*
			 * $id is "room token+shortened_pubkey", e.g. "example+09af"
			 * Each $content looks like this:
			 * $info_array = array(
			 * 		"name"         => "Name of the room",
			 * 		"language"     => "🇩🇪",
			 * 		"description"  => "Some text that describes the community",
			 * 		"active_users" => 1234,
			 * 		"preview_link" => "https://example.com/r/example",
			 * 		"join_link"    => "https://example.com/example?public_key=[64_hex_chars]"
			 * );
			 */
			$img_elem = get_qr_img_element_from_join_url($content["join_link"]); // TODO: incorporate ID to use in js function
			$modal_html =
				"" . PHP_EOL .
				"	
" . PHP_EOL .
				"		× " . PHP_EOL .
				"		" . $img_elem . PHP_EOL .
				"	
" . PHP_EOL .
				"
 " . PHP_EOL;
			$html = $html . $modal_html;
		}
		return $html;
	}
	/*
	 * Generate the HTML for the toast/snackbar when you copy a join link
	 */
	function get_copy_snackbar() {
		$snackbar_html =
			"" .
				"Copied the URL to the clipboard. Paste into Session app to join." .
			"
";
		return $snackbar_html;
	}
	/*
	 * TODO: Description
	 */
	function generateHTML($timestamp, $info_arrays) {
		$title = "Self-updating list of active Session Communities";
		$table_html = get_table_html($info_arrays);
		$modal_html = create_qr_code_modals_html($info_arrays);
		$snackbar_html = get_copy_snackbar();
		$html =
			$table_html . PHP_EOL .
			$modal_html . PHP_EOL .
			$snackbar_html . PHP_EOL;
		$final_html = create_html_page_from_html_data($html, $title, $timestamp);
		return $final_html;
	}
?>