// Manage sub modules
pub mod id;
pub mod path;
pub mod reg;

// common steam utils
use std::error::Error;
use std::process::{Command, Output};

pub fn launch_game(
    steam_path: &str,
    launch_option: &str,
    server: &str,
) -> Result<(), Box<dyn Error>> {
    let mut opt = launch_option.replace("\n", " ");

    if server == "perfectworld" {
        opt = opt.replace("-worldwide", "") + " -perfectworld";
    } else if server == "worldwide" {
        opt = opt.replace("-perfectworld", "") + " -worldwide";
    }

    let opts = format!("-applaunch 730 {}", opt);
    let opts_split = opts.split_whitespace().collect::<Vec<&str>>();

    let output: Output = Command::new(steam_path).args(opts_split).output()?;

    if !output.status.success() {
        let error_message = String::from_utf8_lossy(&output.stderr);
        println!("Error: {}", error_message);
        return Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::Other,
            "Failed to launch game",
        )));
    }

    Ok(())
}
