use super::*;

pub struct Ast {}

impl Ast {
    pub fn new() -> Self {
        Self {}
    }
}

impl Generator for Ast {
    fn word_stream<'a>(
        &'a self,
        approx_len: usize,
        rng: &'a mut crate::Rng,
    ) -> Box<dyn Iterator<Item = Cow<'a, str>> + 'a> {
        fn gen_adjective(words: &mut Vec<&'static str>, rng: &mut crate::Rng, len: usize) {
            if len > 1 && rng.random_bool(0.15) {
                words.extend(*[&["very"][..], &["not", "very"][..]].choose(rng).unwrap());
            }

            words.extend(
                *[
                    &["blue"][..],
                    &["red"][..],
                    &["big"][..],
                    &["nice"][..],
                    &["happy"][..],
                    &["small"][..],
                    &["angry"][..],
                    &["stupid"][..],
                    &["terrible"][..],
                    &["anti-capitalist"][..],
                    &["hateful"][..],
                    &["beautiful"][..],
                    &["spammy"][..],
                    &["funny"][..],
                    &["bald"][..],
                    &["long"][..],
                    &["short"][..],
                    &["enviable"][..],
                    &["articulate"][..],
                    &["fraudulent"][..],
                    &["communist"][..],
                    &["rotund"][..],
                    &["nazi"][..],
                    &["fascistic"][..],
                    &["careless"][..],
                ]
                .choose(rng)
                .unwrap(),
            );
        }

        fn gen_noun(words: &mut Vec<&'static str>, rng: &mut crate::Rng, len: usize) {
            if len > 1 && rng.random_bool(0.25) {
                words.extend(
                    *[
                        &["my"][..],
                        &["your"][..],
                        &["my friend's"][..],
                        &["our"][..],
                        &["their"][..],
                    ]
                    .choose(rng)
                    .unwrap(),
                );
            }

            if len > 2 && rng.random_bool(0.25) {
                gen_adjective(words, rng, len - 1);
            }

            words.extend(
                *[
                    &["kangaroo"][..],
                    &["chip"][..],
                    &["carrot"][..],
                    &["weather"][..],
                    &["dog"][..],
                    &["cat"][..],
                    &["salami"][..],
                    &["cabbage"][..],
                    &["computer"][..],
                    &["onion"][..],
                    &["Sam Altman"][..],
                    &["goblin"][..],
                    &["eagle"][..],
                    &["democracy"][..],
                    &["america"][..],
                    &["Elon Musk"][..],
                    &["tariff"][..],
                    &["nazi"][..],
                    &["liberty"][..],
                ]
                .choose(rng)
                .unwrap(),
            );
        }

        fn gen_adverb(words: &mut Vec<&'static str>, rng: &mut crate::Rng, _len: usize) {
            words.extend(
                *[
                    &["quickly"][..],
                    &["slowly"][..],
                    &["joyfully"][..],
                    &["often"][..],
                    &["carelessly"][..],
                    &["angrily"][..],
                    &["menacingly"][..],
                    &["deliberately"][..],
                ]
                .choose(rng)
                .unwrap(),
            );
        }

        fn gen_verb(words: &mut Vec<&'static str>, rng: &mut crate::Rng, len: usize) {
            words.extend(
                *[
                    &["dies"][..],
                    &["speaks"][..],
                    &["eats"][..],
                    &["sits"][..],
                    &["thought"][..],
                    &["explodes"][..],
                    &["screams"][..],
                    &["votes"][..],
                    &["salutes"][..],
                    &["flies"][..],
                ]
                .choose(rng)
                .unwrap(),
            );

            if len > 1 && rng.random_bool(0.25) {
                gen_adverb(words, rng, len - 1);
            }
        }

        fn gen_action(words: &mut Vec<&'static str>, rng: &mut crate::Rng, len: usize) {
            if len > 1 && rng.random_bool(0.25) {
                gen_adverb(words, rng, len - 1);
            }

            words.extend(
                *[
                    &["eats"][..],
                    &["runs over"][..],
                    &["walks with"][..],
                    &["speaks to"][..],
                    &["talks to"][..],
                    &["lives with"][..],
                    &["cries with"][..],
                    &["screams at"][..],
                    &["shot"][..],
                    &["killed"][..],
                    &["murders"][..],
                    &["salutes"][..],
                    &["deports"][..],
                ]
                .choose(rng)
                .unwrap(),
            );
        }

        fn gen_clause(words: &mut Vec<&'static str>, rng: &mut crate::Rng, len: usize) {
            if len > 5 {
                gen_clause(words, rng, len / 2);
                words.extend(
                    *[
                        &["because"][..],
                        &["and", "also"][..],
                        &["when"][..],
                        &["if"][..],
                        &["or"][..],
                        &["and"][..],
                        &["then"][..],
                        &["before"][..],
                    ]
                    .choose(rng)
                    .unwrap(),
                );
                gen_clause(words, rng, len - len / 2);
            } else {
                enum Kind {
                    Gen(fn(&mut Vec<&'static str>, &mut crate::Rng, usize)),
                    Word(&'static str),
                }

                let clauses = [
                    &[Kind::Gen(gen_noun), Kind::Gen(gen_verb)][..],
                    &[
                        Kind::Gen(gen_noun),
                        Kind::Gen(gen_action),
                        Kind::Gen(gen_noun),
                    ][..],
                ];
                let clause = clauses.choose(rng).unwrap();
                for k in *clause {
                    match k {
                        Kind::Word(w) => words.push(w),
                        Kind::Gen(f) => f(words, rng, len / clause.len()),
                    }
                }
            }
        }

        let mut words = Vec::new();
        gen_clause(&mut words, rng, approx_len);
        Box::new(words.into_iter().map(|word| word.into()))
    }
}
