use serde_json;
use std::io;
use crate::command_init::fetch::Fetch;
use crate::constants::url::URL;
const LABEL: &str = "label";
pub struct UserInput {}
impl UserInput {
pub async fn handle_index_styles() {
let url_registry_styles_json = URL::URL_REGISTRY_STYLES_JSON;
let styles_index_result = Fetch::from_url(&url_registry_styles_json).await;
if let Ok(styles_index) = styles_index_result {
let vec_styles: Vec<serde_json::Value> = serde_json::from_str(&styles_index).unwrap();
ask_user_choose_style(vec_styles);
}
}
}
fn ask_user_choose_style(vec_styles: Vec<serde_json::Value>) {
for (index, style) in vec_styles.iter().enumerate() {
if let Some(label) = style.get(LABEL) {
println!("{}: {}", index + 1, label);
}
}
println!("Please choose a style by entering the corresponding number:");
let mut user_input = String::new();
io::stdin().read_line(&mut user_input).expect("🔸 Failed to read line");
if let Ok(index) = user_input.trim().parse::<usize>() {
if index > 0 && index <= vec_styles.len() {
if let Some(selected_style) = vec_styles.get(index - 1) {
if let Some(label) = selected_style.get(LABEL) {
println!("You selected: {}", label);
}
}
} else {
println!(
"🔸 Invalid choice. Please select a number between 1 and {}.",
vec_styles.len()
);
}
} else {
println!("🔸 Invalid input. Please enter a number.");
}
}