use fancy_regex::Regex;
use nu_engine::command_prelude::*;
use std::collections::BTreeMap;
use std::{fmt, str};
use unicode_segmentation::UnicodeSegmentation;
pub type Counted = BTreeMap<Counter, usize>;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
    fn name(&self) -> &str {
        "str stats"
    }
    fn signature(&self) -> Signature {
        Signature::build("str stats")
            .category(Category::Strings)
            .input_output_types(vec![(Type::String, Type::record())])
    }
    fn description(&self) -> &str {
        "Gather word count statistics on the text."
    }
    fn search_terms(&self) -> Vec<&str> {
        vec!["count", "word", "character", "unicode", "wc"]
    }
    fn is_const(&self) -> bool {
        true
    }
    fn run(
        &self,
        engine_state: &EngineState,
        _stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        stats(engine_state, call, input)
    }
    fn run_const(
        &self,
        working_set: &StateWorkingSet,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        stats(working_set.permanent(), call, input)
    }
    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Count the number of words in a string",
                example: r#""There are seven words in this sentence" | str stats"#,
                result: Some(Value::test_record(record! {
                        "lines" =>     Value::test_int(1),
                        "words" =>     Value::test_int(7),
                        "bytes" =>     Value::test_int(38),
                        "chars" =>     Value::test_int(38),
                        "graphemes" => Value::test_int(38),
                        "unicode-width" => Value::test_int(38),
                })),
            },
            Example {
                description: "Counts unicode characters",
                example: r#"'今天天气真好' | str stats"#,
                result: Some(Value::test_record(record! {
                        "lines" =>     Value::test_int(1),
                        "words" =>     Value::test_int(6),
                        "bytes" =>     Value::test_int(18),
                        "chars" =>     Value::test_int(6),
                        "graphemes" => Value::test_int(6),
                        "unicode-width" => Value::test_int(12),
                })),
            },
            Example {
                description: "Counts Unicode characters correctly in a string",
                example: r#""Amélie Amelie" | str stats"#,
                result: Some(Value::test_record(record! {
                        "lines" =>     Value::test_int(1),
                        "words" =>     Value::test_int(2),
                        "bytes" =>     Value::test_int(15),
                        "chars" =>     Value::test_int(14),
                        "graphemes" => Value::test_int(13),
                        "unicode-width" => Value::test_int(13),
                })),
            },
        ]
    }
}
fn stats(
    engine_state: &EngineState,
    call: &Call,
    input: PipelineData,
) -> Result<PipelineData, ShellError> {
    let span = call.head;
    if matches!(input, PipelineData::Empty) {
        return Err(ShellError::PipelineEmpty { dst_span: span });
    }
    input.map(
        move |v| {
            let value_span = v.span();
            if let Value::Error { error, .. } = v {
                return Value::error(*error, span);
            }
            match v.coerce_into_string() {
                Ok(s) => counter(&s, span),
                Err(_) => Value::error(
                    ShellError::PipelineMismatch {
                        exp_input_type: "string".into(),
                        dst_span: span,
                        src_span: value_span,
                    },
                    span,
                ),
            }
        },
        engine_state.signals(),
    )
}
fn counter(contents: &str, span: Span) -> Value {
    let counts = uwc_count(&ALL_COUNTERS[..], contents);
    fn get_count(counts: &BTreeMap<Counter, usize>, counter: Counter, span: Span) -> Value {
        Value::int(counts.get(&counter).copied().unwrap_or(0) as i64, span)
    }
    let record = record! {
        "lines" => get_count(&counts, Counter::Lines, span),
        "words" => get_count(&counts, Counter::Words, span),
        "bytes" => get_count(&counts, Counter::Bytes, span),
        "chars" => get_count(&counts, Counter::CodePoints, span),
        "graphemes" => get_count(&counts, Counter::GraphemeClusters, span),
        "unicode-width" => get_count(&counts, Counter::UnicodeWidth, span),
    };
    Value::record(record, span)
}
pub trait Count {
    fn count(&self, s: &str) -> usize;
}
impl Count for Counter {
    fn count(&self, s: &str) -> usize {
        match *self {
            Counter::GraphemeClusters => s.graphemes(true).count(),
            Counter::Bytes => s.len(),
            Counter::Lines => {
                const LF: &str = "\n"; const CR: &str = "\r"; const CRLF: &str = "\r\n"; const NEL: &str = "\u{0085}"; const FF: &str = "\u{000C}"; const LS: &str = "\u{2028}"; const PS: &str = "\u{2029}"; let line_ending_types = [CRLF, LF, CR, NEL, FF, LS, PS];
                let pattern = &line_ending_types.join("|");
                let newline_pattern = Regex::new(pattern).expect("Unable to create regex");
                let line_endings = newline_pattern
                    .find_iter(s)
                    .map(|f| match f {
                        Ok(mat) => mat.as_str().to_string(),
                        Err(_) => "".to_string(),
                    })
                    .collect::<Vec<String>>();
                let has_line_ending_suffix =
                    line_ending_types.iter().any(|&suffix| s.ends_with(suffix));
                if has_line_ending_suffix {
                    line_endings.len()
                } else {
                    line_endings.len() + 1
                }
            }
            Counter::Words => s.unicode_words().count(),
            Counter::CodePoints => s.chars().count(),
            Counter::UnicodeWidth => unicode_width::UnicodeWidthStr::width(s),
        }
    }
}
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub enum Counter {
    Lines,
    Words,
    Bytes,
    GraphemeClusters,
    CodePoints,
    UnicodeWidth,
}
pub const ALL_COUNTERS: [Counter; 6] = [
    Counter::GraphemeClusters,
    Counter::Bytes,
    Counter::Lines,
    Counter::Words,
    Counter::CodePoints,
    Counter::UnicodeWidth,
];
impl fmt::Display for Counter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match *self {
            Counter::GraphemeClusters => "graphemes",
            Counter::Bytes => "bytes",
            Counter::Lines => "lines",
            Counter::Words => "words",
            Counter::CodePoints => "codepoints",
            Counter::UnicodeWidth => "unicode-width",
        };
        write!(f, "{s}")
    }
}
pub fn uwc_count<'a, I>(counters: I, s: &str) -> Counted
where
    I: IntoIterator<Item = &'a Counter>,
{
    let mut counts: Counted = counters.into_iter().map(|c| (*c, c.count(s))).collect();
    if let Some(lines) = counts.get_mut(&Counter::Lines) {
        if s.is_empty() {
            *lines = 0;
        } else if *lines == 0 && !s.is_empty() {
            *lines = 1;
        } else {
            }
    }
    counts
}
#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_examples() {
        use crate::test_examples;
        test_examples(SubCommand {})
    }
}
#[test]
fn test_one_newline() {
    let s = "\n".to_string();
    let counts = uwc_count(&ALL_COUNTERS[..], &s);
    let mut correct_counts = BTreeMap::new();
    correct_counts.insert(Counter::Lines, 1);
    correct_counts.insert(Counter::Words, 0);
    correct_counts.insert(Counter::GraphemeClusters, 1);
    correct_counts.insert(Counter::Bytes, 1);
    correct_counts.insert(Counter::CodePoints, 1);
    correct_counts.insert(Counter::UnicodeWidth, 1);
    assert_eq!(correct_counts, counts);
}
#[test]
fn test_count_counts_lines() {
    const NEL: &str = "\u{0085}"; const FF: &str = "\u{000C}"; const LS: &str = "\u{2028}"; const PS: &str = "\u{2029}"; let mut s = String::from("foo\r\nbar\n\nbaz");
    s += NEL;
    s += "quux";
    s += FF;
    s += LS;
    s += "xi";
    s += PS;
    s += "\n";
    let counts = uwc_count(&ALL_COUNTERS[..], &s);
    let mut correct_counts = BTreeMap::new();
    correct_counts.insert(Counter::Lines, 8);
    correct_counts.insert(Counter::Words, 5);
    correct_counts.insert(Counter::GraphemeClusters, 23);
    correct_counts.insert(Counter::Bytes, 29);
    correct_counts.insert(Counter::CodePoints, 24);
    correct_counts.insert(Counter::UnicodeWidth, 23);
    assert_eq!(correct_counts, counts);
}
#[test]
fn test_count_counts_words() {
    let i_can_eat_glass = "Μπορῶ νὰ φάω σπασμένα γυαλιὰ χωρὶς νὰ πάθω τίποτα.";
    let s = String::from(i_can_eat_glass);
    let counts = uwc_count(&ALL_COUNTERS[..], &s);
    let mut correct_counts = BTreeMap::new();
    correct_counts.insert(Counter::GraphemeClusters, 50);
    correct_counts.insert(Counter::Lines, 1);
    correct_counts.insert(Counter::Bytes, i_can_eat_glass.len());
    correct_counts.insert(Counter::Words, 9);
    correct_counts.insert(Counter::CodePoints, 50);
    correct_counts.insert(Counter::UnicodeWidth, 50);
    assert_eq!(correct_counts, counts);
}
#[test]
fn test_count_counts_codepoints() {
    let one = "é";
    let two = "é";
    let counters = [Counter::CodePoints];
    let counts = uwc_count(&counters[..], one);
    let mut correct_counts = BTreeMap::new();
    correct_counts.insert(Counter::CodePoints, 1);
    assert_eq!(correct_counts, counts);
    let counts = uwc_count(&counters[..], two);
    let mut correct_counts = BTreeMap::new();
    correct_counts.insert(Counter::CodePoints, 2);
    assert_eq!(correct_counts, counts);
}