use dialtone_ccli_util::{config::DialtoneConfig, sc::get_sc};
use dialtone_common::{
rest::sites::site_data::RegistrationMethod, utils::parse_acct::parse_user_acct,
};
use dialtone_reqwest::api_v1::sites::get_site_info::get_site_info;
use simplelog::info;
use tabled::{style::BorderText, Disable, Style, Table};
use crate::row_obj::RowObj;
pub async fn do_site_info(host_name: Option<&String>, json_output: &bool) {
let config = DialtoneConfig::from_config_dir();
let host_name = if host_name.is_none() {
if let Some(default_acct) = config.default_acct {
let name_host = parse_user_acct(&default_acct).unwrap();
name_host.host_name
} else {
panic!("A host name needs to be specified.")
}
} else {
host_name.unwrap().to_string()
};
info!("Getting site info for <b>{host_name}</b>");
let sc = get_sc(&host_name);
let site_info = get_site_info(&sc).await.unwrap();
if *json_output {
println!("{}", serde_json::to_string(&site_info).unwrap());
} else {
let mut rows: Vec<RowObj> = Vec::new();
rows.push(RowObj {
name: site_info.names.short_name,
value: site_info.names.long_name,
});
let mut reg_methods = site_info
.registration_methods
.iter()
.map(|m| match m {
RegistrationMethod::OpenRegistration => "Open Registration",
RegistrationMethod::SimpleCode => "Registration with a Code",
})
.collect::<Vec<&str>>()
.join(",");
if reg_methods.is_empty() {
reg_methods.push_str("Registration Closed")
}
rows.push(RowObj {
name: "Available Registration Methods".to_string(),
value: reg_methods,
});
println!(
"\n{}",
Table::new(rows)
.with(Disable::Row(..1))
.with(Style::modern().vertical_off())
.with(BorderText::new(0, format!(" {host_name} ")))
);
}
}