use crate::*;
use clap::Parser;
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "audio_engine")]
#[command(version, about = "FCB1010 Looper Pedal Audio Engine")]
pub struct Args {
    /// Path to Unix socket or named pipe name
    #[arg(
        short = 's',
        long = "socket",
        env = "SOCKET",
        default_value = "fcb_looper.sock"
    )]
    pub socket: String,

    /// Path to configuration dir
    #[arg(short = 'c', long = "config-path", env = "CONFIG_PATH", default_value = default_config_path())]
    pub config_file: PathBuf,
}

impl Args {
    pub fn new() -> Result<Self> {
        let res = Self::try_parse();
        if let Err(res) = &res {
            log::error!("{res}");
        }
        res.map_err(|_| LooperError::ArgsParse(std::panic::Location::caller()))
    }
}

fn default_config_path() -> String {
    let mut path = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
    path.push(".fcb_looper");
    path.push("state.json");
    path.to_string_lossy().to_string()
}
