#!/usr/bin/env -S deno run --allow-read

import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts";

import { Dict, ExplorerGuesser, Guessing, ManualProxy, ReducingGuesser, Runner } from "./lib/lib.ts";
import { initialize_prompt } from "./lib/prompt.ts";
import { VerboseLogging } from "./lib/runner.ts";
import { LineReader } from "./lib/utils.ts";

import { gutenberg } from "../data/data.ts";

async function main() {
	const args = await new Command().name("manual_proxy")
		.description(
			"Program to solve manually proxied TUSMO games with guesser controller.",
		)
		.option(
			"-f, --file <path:string>",
			"Sets dictionnary to use words from (defaults to internal french dict).",
		).option(
			"-g, --guesser <guesser:string>",
			`Guesser to use, available are ${[...guessers.keys()]}.`,
			{ default: "reducing" },
		).parse(Deno.args);

	const lines_ = new LineReader(Deno.stdin.readable);
	const init = await initialize_prompt(lines_);

	let dict = Dict.from_lines(gutenberg, init.length);
	if (args.options.file !== undefined) dict = await Dict.from_text_file(args.options.file, init.length);
	for (const [index, letter] of init.constraints) dict.constraint(index, letter);

	const guesser = guessers.get(args.options.guesser)!(dict);
	const game = new ManualProxy(init.length);

	const runner = new Runner(game, guesser, new VerboseLogging());
	await runner.play_all();
}

const guessers = new Map<string, (dict: Dict) => Guessing>([
	["explorer", (dict: Dict) => new ExplorerGuesser(dict)],
	["reducing", (dict: Dict) => new ReducingGuesser(dict)],
]);

if (import.meta.main) await main();
