use crate::Settings;
use anyhow::Result;
use std::{fs::DirEntry, path::PathBuf};

#[allow(dead_code)]
impl Settings {
    fn get_mod_info(&self, entry: DirEntry) -> Result<Option<(String, PathBuf)>> {
        if !entry.file_type()?.is_file() {
            return Ok(None);
        }
        let path = PathBuf::from(entry.file_name());

        let extension = path.extension();
        if !extension.map_or(false, |e| e.eq_ignore_ascii_case("pak")) {
            return Ok(None);
        }

        let info = self
            .read_mod_file(&path)?
            .deserialize_meta_lsx()?
            .module_info;
        Ok(Some((info.uuid, path)))
    }

    pub(crate) fn uninstall_all_helper(
        &self,
        _dry_run: bool,
        _indexes_to_delete: Vec<usize>,
    ) -> Result<()> {
        todo!()
        // let mut order = self.get_load_order()?;

        // let uuid_paths: Result<HashMap<_, _>> = self
        //     .read_mod_files_dir()?
        //     .filter_map(|e| self.get_mod_info(e?).transpose())
        //     .collect();

        // let mut uuid_paths = uuid_paths?;

        // if !dry_run {
        //     indexes_to_delete.sort_unstable_by_key(|i| usize::MAX - i);

        //     for index in indexes_to_delete {
        //         let mod_info = order.get(index).

        //         let Some(entry) = uuid_paths.get(uuid)

        //         std::fs::remove_file()?;
        //         order.remove(entry.index);

        //         if dry_run {
        //             println!("{} would be uninstalled with a non-dry run", info.name);
        //         } else {
        //             println!("uninstalling {}", info.name);
        //             let mod_file = self.get_mod_file_path(path);
        //         }
        //     }

        //     todo!("write order");
        // }

        // Ok(())
    }
}

// fn get_unordered_index<'a>(
//     ordered: &'a ModulesChildren,
//     mod_index: usize,
//     mods: &'a ModuleInfoNode,
// ) -> Result<Option<(ModuleInfo, usize)>> {
//     let Some(order_entry) = ordered.node.get(mod_index) else {
//         return Ok(None);
//     };

//     let uuid = order_entry
//         .get_attribute_value_where_id(|id| id == "UUID")
//         .ok_or_else(|| anyhow!("missing UUID for mod order index {mod_index}"))?
//         .to_string();

//     let Some(index) = mods.find_index_where_child_id(&uuid) else {
//         return Ok(None);
//     };

//     let pak = mods.children[0].node[index].clone().try_into()?;

//     Ok(Some((pak, index)))
// }
