use smbioslib::{SMBiosMemoryDevice, table_load_from_device};
use crate::dtos::memory::MemoryInfo;

pub fn get_memory_info() -> Vec<MemoryInfo> {
	let data = table_load_from_device().unwrap();
	let mut mem_info_list = vec![];

	for memory_device in data.collect::<SMBiosMemoryDevice>() {
		let clock_speed = match memory_device.speed() {
			Some(msd) => match msd {
				smbioslib::MemorySpeed::Unknown => continue,
				smbioslib::MemorySpeed::SeeExtendedSpeed => continue,
				smbioslib::MemorySpeed::MTs(speed) => speed,
			},
			None => continue,
		};

		let size = match memory_device.size() {
			Some(size) => match size {
				smbioslib::MemorySize::NotInstalled => continue,
				smbioslib::MemorySize::Unknown => continue,
				smbioslib::MemorySize::SeeExtendedSize => continue,
				smbioslib::MemorySize::Kilobytes(kb_size) => kb_size / 1024,
				smbioslib::MemorySize::Megabytes(mb_size) => mb_size,
			},
			None => continue,
		};

		let memory_type = match memory_device.memory_type() {
			Some(memory_type) => format!("{:?}", memory_type.value),
			None => continue,
		};

		mem_info_list.push(MemoryInfo {
			memory_type,
			capacity: size,
			clock_speed,
		});
	}
	mem_info_list
}
