use std::io::{self, Write};

use super::{Game, Info};

pub struct Proxy {
	length: usize,
}

impl Proxy {
	pub fn init() -> (Self, Vec<Info>) {
		println!("Infos.");
		let infos = prompt();
		let infos = Info::from_init(infos.chars());
		let length = infos.len();
		(Self { length }, infos)
	}
}

impl Game for Proxy {
	fn guess(&mut self, word: &str) -> Result<(), Vec<Info>> {
		println!("Guessing");
		println!("  {word}");
		loop {
			let result = prompt();
			let Some(infos) = Info::from_printable(result.chars(), word.chars()) else {
				continue;
			};
			return Err(infos);
		}
	}

	fn length(&mut self) -> usize {
		self.length
	}
}

fn prompt() -> String {
	print!("> ");
	io::stdout().flush().ok();
	let mut line = String::new();
	io::stdin().read_line(&mut line).unwrap();
	line.trim_end_matches("\n").to_string()
}
