use serde_json;
use std::io;
use crate::command_init::fetch::Fetch;
use crate::constants::url::MyUrl;
const LABEL: &str = "label";
pub struct UserInput {}
impl UserInput {
pub async fn handle_index_styles() {
let styles_index_result = Fetch::from_url(MyUrl::URL_REGISTRY_STYLES_JSON).await;
if let Ok(styles_index) = styles_index_result {
match serde_json::from_str::<Vec<serde_json::Value>>(&styles_index) {
Ok(vec_styles) => ask_user_choose_style(vec_styles),
Err(err) => eprintln!("Error parsing styles_index: {err}"),
}
}
}
}
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!("\n{}: {}", 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(label) = vec_styles.get(index - 1).and_then(|s| s.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.");
}
}