use std::ffi::OsString;
use std::fs::read_dir;
use std::path::PathBuf;
use imgui::Ui;
use log::debug;

pub struct GuiFileList {}

impl GuiFileList {
    pub fn display_path(root: PathBuf, selected_filename: &String, ui: &Ui) -> String {
        let mut working_filename = selected_filename.clone();
        ui.text(format!("Displaying {}", root.to_str().unwrap_or("Unable to parse path")));

        let mut known_files: Vec<OsString> = vec![];

        for entry in read_dir(root.as_path()).unwrap() {
            known_files.push(entry.unwrap().file_name());
        }

        known_files.sort();

        for (index, entry) in known_files.iter().enumerate() {
            let mut working_select = ui.selectable_config(format!("{}", entry.clone().into_string().unwrap()));
            if entry.to_str().unwrap().to_string() == selected_filename.as_str().to_string() {
                working_select = working_select.selected(true);
            }
            if working_select.build() {
                debug!("SELECTED {index} / {entry:?}");
                working_filename = entry.clone().into_string().unwrap();
            };
        }
        working_filename
    }
}
