use crate::games_enum::SupportedGames;
use crate::platform::Platform;
use crate::profile::SensProfile;
use crate::steam_folder::SteamFolders;
use serde::{Deserialize, Serialize};
use std::fs;
use std::fs::File;
use std::io;
use std::io::{Read, Write};
use std::path::Path;
extern crate dirs;
#[derive(Serialize, Deserialize)]
pub struct Profiles {
profiles: Vec<SensProfile>,
steam_paths: SteamFolders,
platform: Platform,
}
impl Profiles {
pub fn new() -> Self {
Self {
profiles: Vec::new(),
steam_paths: SteamFolders::new(),
platform: Platform::new(),
}
}
pub fn set_game_sens_in_profile(
&mut self,
game: SupportedGames,
sens: f64,
profile_index: i32,
) {
self.profiles[profile_index as usize].set_game_sens(game, sens);
}
pub fn equalize_profile_at_index(&mut self, game: SupportedGames, index: i32) {
self.profiles[index as usize].equalize(game);
}
pub fn set_platform(&mut self) {
self.platform = Platform::new();
}
pub fn len(&self) -> usize {
self.profiles.len()
}
pub fn add_profile(&mut self) {
self.profiles.push(SensProfile::new());
}
pub fn remove_profile(&mut self, index: i32) {
self.profiles.remove(index as usize);
}
pub fn switch_profile(&self, index: i32) -> Result<(), io::Error> {
self.profiles[index as usize].save_all_to_configs()?;
Ok(())
}
pub fn get_steam_folder_at_index(&self, index: i32) -> String {
self.steam_paths.get_steam_folder_at_pointer(index as usize)
}
pub fn add_steam_folder(&mut self, path_string: String) {
self.steam_paths.add_steam_folder(path_string);
}
pub fn remove_steam_folder_at_index(&mut self, index: i32) {
self.steam_paths
.remove_steam_folder_at_index(index as usize)
}
pub fn set_paths(&mut self) -> Result<(), io::Error> {
for profile in self.profiles.iter_mut() {
profile.set_all_paths(&self.steam_paths, self.platform.clone())?;
}
Ok(())
}
pub fn fs_read_game_sens_at_index(
&mut self,
game: SupportedGames,
index: i32,
) -> Result<(), io::Error> {
self.profiles[index as usize].fs_read_game_sens(game)?;
Ok(())
}
pub fn fs_read_all_game_sens_at_index(&mut self, index: i32) -> Result<(), io::Error> {
self.profiles[index as usize].fs_read_all_game_sens()?;
Ok(())
}
pub fn save_json(&self) -> Result<(), io::Error> {
let homepath: String = dirs::config_dir().unwrap().to_str().unwrap().to_string();
if !(Path::new(&(homepath.clone() + "/sens/profiles.json")).exists()) {
if !(Path::new(&(homepath.clone())).exists()) {
fs::create_dir(homepath.clone()).unwrap();
}
if !(Path::new(&(homepath.clone() + "/sens")).exists()) {
fs::create_dir(homepath.clone() + "/sens").unwrap();
}
File::create(homepath.clone() + "/sens/profiles.json")?.write_all(b"")?;
} else {
fs::remove_file(homepath.clone() + "/sens/profiles.json")?;
}
File::create(homepath + "/sens/profiles.json")?
.write(serde_json::to_string(&self).unwrap().as_bytes())?;
Ok(())
}
pub fn fs_load_profiles() -> Result<Self, io::Error> {
let homepath: String = dirs::config_dir().unwrap().to_str().unwrap().to_string();
if !(Path::new(&(homepath.clone() + "/sens/profiles.json")).exists()) {
return Ok(Self::new());
}
let file = File::open(homepath + "/sens/profiles.json");
let mut contents = String::new();
file.unwrap().read_to_string(&mut contents)?;
let tmp = serde_json::from_str(&contents).unwrap();
Ok(tmp)
}
pub fn to_string(&self, steam: bool) -> String {
let mut return_string: String = "".to_string();
if steam {
for i in 0..self.steam_paths.len() {
return_string = return_string
+ &self.steam_paths.get_steam_folder_at_pointer(i as usize)
+ "\n";
}
} else {
for i in 0..self.profiles.len() {
return_string = return_string + &self.profiles[i].get_name() + "\n";
}
}
return_string
}
pub fn append_steam_folder(&mut self, path: String) {
self.steam_paths.add_steam_folder(path);
}
pub fn show_profile(&self, index: i32) -> String {
self.profiles[index as usize].to_string()
}
pub fn change_name_at_index(&mut self, index: i32, name: String) {
self.profiles[index as usize].set_name(name);
}
pub fn verify_existence() -> Result<(), io::Error> {
let homepath: String = dirs::config_dir().unwrap().to_str().unwrap().to_string();
if !(Path::new(&(homepath.clone() + "/sens/profiles.json")).exists()) {
Profiles::new().save_json()?;
}
Ok(())
}
}