ui-cli 0.1.1

A CLI to add components to your app.
use serde_json;
use std::io;

use crate::constants::urls::URL_REGISTRY_STYLES_JSON;

use super::init_fetch_functions::handle_fetch_from_init;

const LABEL: &str = "label";

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/*                        🦀 MAIN 🦀                          */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

pub async fn handle_index_styles() {
    let styles_index_result = handle_fetch_from_init(URL_REGISTRY_STYLES_JSON).await;
    // println!("{}", styles_index_result.as_ref().unwrap());

    // Parse the JSON string into Vec<serde_json::Value>
    if let Ok(styles_index) = styles_index_result {
        // Convert the String to a Vec<serde_json::Value>
        let vec_styles: Vec<serde_json::Value> = serde_json::from_str(&styles_index).unwrap();
        ask_user_choose_style(vec_styles);
    }
}

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/*                     ✨ FUNCTIONS ✨                        */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/// Ask user to choose a style
fn ask_user_choose_style(vec_styles: Vec<serde_json::Value>) {
    // Print available styles
    for (index, style) in vec_styles.iter().enumerate() {
        if let Some(label) = style.get(LABEL) {
            println!("{}: {}", index + 1, label);
        }
    }

    // Prompt user for choice
    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");

    // Parse the choice and print the selected style
    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.");
    }
}