use crate::{files::{get_downloaded_versions, download_versions, delete_version, disable_version}, db::userlist_get_all_current_versions_with_mods, modrinth::get_raw_versions};
#[allow(unused_imports)]
use crate::{List, get_current_list, config::Cfg, db::userlist_get_all_downloads, input::Input};
pub async fn download(config: Cfg, input: Input) -> Result<(), Box<dyn std::error::Error>> {
let current_list = get_current_list(config.clone())?;
let downloaded_versions = get_downloaded_versions(current_list.clone())?;
let current_version_ids = userlist_get_all_current_versions_with_mods(config.clone(), String::from(¤t_list.id))?;
let mut to_download: Vec<String> = vec![];
//(mod_id, version_id)
let mut to_disable: Vec<(String, String)> = vec![];
for version in current_version_ids {
let mod_id = version.0;
let current_version = version.1;
let current_download = downloaded_versions.get(&mod_id);
if current_download.is_none() {
to_download.push(current_version);
} else {
let downloaded_version = current_download.ok_or("SOMETHING_HAS_REALLY_GONE_WRONG")?;
if ¤t_version != downloaded_version {
to_disable.push((mod_id.clone(), String::from(downloaded_version)));
to_download.push(current_version);
}
}
}
if !to_download.is_empty() {
download_versions(current_list.clone(), get_raw_versions(String::from(&config.apis.modrinth), to_download).await).await?;
} else {
println!("There are no new versions to download");
}
if !to_disable.is_empty() {
for ver in to_disable {
if input.delete_old {
println!("Deleting version {} for mod {}", ver.1, ver.0);
delete_version(current_list.clone(), ver.1)?;
} else {
disable_version(config.clone(), current_list.clone(), ver.1, ver.0)?;
};
}
}
Ok(())
}