use std::{
    env::{self, args},
    path::Path,
    process::Command,
    thread::sleep,
    time::Duration,
};

fn main() {
    let args: Vec<String> = args().collect();
    let timeout: u64 = args.get(1)
        .expect("There should be at least 1 argument, which specifies amount of time (in seconds) for timer to work!")
        .parse()
        .expect("The first argument should be a non-negative integer number");
    let measurement: char = args
        .get(2)
        .map_or('s', |arg| arg.chars().next().unwrap_or('s'));
    let sleeping_amount: Duration = match measurement {
        's' => Duration::from_secs(timeout),
        'm' => Duration::from_mins(timeout),
        _ => panic!("Invalid measurement of time! For now, only 's' and 'm' are supported"),
    };
    let alert_path = args.get(3).map_or("assets/not_sussy_song.opus", |path| {
        assert!(
            Path::new(path).exists(),
            "The path provided should point to an existing file"
        );
        path
    });

    sleep(sleeping_amount);

    let mut sound_alert = Command::new("pw-play")
        .arg(alert_path)
        .spawn()
        .expect("pw-play utility should be installed");

    let _ = Command::new("zenity")
        .args(["--warning", "--text=Take a break from computer!"])
        .spawn()
        .unwrap()
        .wait();

    sound_alert.kill().unwrap();
}
