use alloc::{
    string::{String, ToString},
    vec,
    vec::Vec,
};

/*
clparse
* A simple command line parser for ableOS
*/

// use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Argument {
    key: String,
    value: String,
}

#[derive(Debug, Clone)]
pub struct Command {
    pub root_command: String,
    // arguments: HashMap<String, String>,
    pub arguments: Vec<Argument>,
}

impl Command {
    pub fn parse(command: String) -> Command {
        let split_command = command.split("?");
        let mut root = "".to_string();
        let mut root_count = 0;
        let mut args: Vec<Argument> = vec![];
        for subcommand in split_command {
            match root_count {
                0 => root = subcommand.to_string(),
                1 => {
                    for subarg in subcommand.split("&") {
                        let mut arg1 = "";
                        let mut arg2 = "";
                        let mut arg_count = 0;
                        for arg in subarg.split("=") {
                            if arg_count == 0 {
                                arg1 = arg;
                            } else {
                                arg2 = arg;
                            }
                            arg_count += 1;
                        }
                        let arg_struct = Argument {
                            key: arg1.to_string(),
                            value: arg2.to_string(),
                        };
                        args.push(arg_struct);
                    }
                }
                _ => {}
            }
            root_count += 1;
        }

        Command {
            root_command: root,
            arguments: args,
        }
    }
}
pub fn test() {
    let x = Command::parse("hi?there=uwu&hi=abc".to_string());

    let y = &x.arguments[0];

    trace!("{}", x.root_command);
    trace!("{:?}", y);
    trace!("{}", y.key);
    trace!("{}", y.value);
}
