extern crate chrono;
extern crate timer;
extern crate clap;
use clap::Parser;
use std::sync::mpsc::channel;

#[derive(clap::Parser)]
struct Tbreakarguments {
    mode: String,
    time: i64,
}

fn main() {
    let args = Tbreakarguments::parse();
    let timer = timer::Timer::new();
    let (tx, rx) = channel();

    if args.mode == "soft" || args.mode == "hard" {
        timer.schedule_with_delay(chrono::Duration::minutes(args.time), move || {
            let _ignored = tx.send(1);
        });

        let _confirmation = rx.recv();
        send_action(args.mode);
    } else {
        println!("ERROR: Incorrect mode - please specify <soft,hard>")
    }
}

fn send_action(mode: String) {
    // soft = minimize active window - hard = lock screen
    if mode == "soft" {
        let (code, _output, error) = run_script::run_script!(
            r#"
        wmctrl -i -r `xdotool getwindowfocus` -e 0,0,0,0,0
        exit 0
        "#
        )
        .unwrap();
        println!("Exit Code: {}", code);

        if !error.is_empty() {
            println!("Error: {}", error)
        }
    } else if mode == "hard" {
        let (code, _output, error) = run_script::run_script!(
            r#"
        xdg-screensaver lock
        exit 1
        "#
        )
        .unwrap();
        println!("Exit Code: {}", code);

        if !error.is_empty() {
            println!("Error: {}", error)
        }
    }
}
