use std::error::Error;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use wasm_crossword_generator::*;
use regexle::utils::read_csv;
use regexle::word::Answer;


fn print_puzzle(puzz: PerWordPuzzle) {
    for word in puzz.puzzle.solution.words.iter() {
        println!("{:?}", word);
    }
    puzz.puzzle.grid.iter().for_each(|row| {
        row.row.iter().for_each(|cell|
            print!("{:?}", cell)
        );
        println!();
    });
}
fn main() -> Result<(), Box<dyn Error>> {
    let records: Vec<Answer> = read_csv("processed_answers.csv")?;

    let words: Vec<Word> = records.par_iter().map(|ans| Word { text: ans.answer.clone(), clue: Option::from(ans.question.clone()) }).collect();
    // A real SolutionConf would probably want "requirements" to allow for retrying crossword
    // generation. Because there is only one word, we know we'll get the world's simplest puzzle in
    // one try.
    let solution_conf = SolutionConf {
        words,
        max_words: 20,
        width: 10,
        height: 10,
        requirements: None,
        initial_placement: None,
    };

    // A PerWordPuzzle is one where the player only guesses words, not locations. The player is
    // immediately informed if the guess is correct or not.
    let mut puzzle = PerWordPuzzle::new(solution_conf)?;
    // println!("{:?}", puzzle.puzzle.grid);
    print_puzzle(puzzle);
    // let guess = PlacedWord {
    //     // Because it is a PerWordPuzzle, the placement is ignored, unlike other Playmodes.
    //     placement: Placement { x: 0, y: 0, direction: rand::random() },
    //     // NOTE: you don't need to match the "clue" field, it is ignored for purposes of PartialEq
    //     word: Word { text: "library".to_string(), clue: None },
    // };
    // let guess_result = puzzle.guess_word(guess)?;
    //
    // // Because there is only one word, the guess will result in "Complete" instead of "Correct"
    // assert_eq!(guess_result, GuessResult::Complete);
    Ok(())
}
