#[allow(unused_must_use)]
#[allow(unused_variables)]

mod data;

extern crate structopt;

use crate::data::dababy::DABABY;
use std::{
    fs::File,
    thread::sleep,
    time::Duration,
    path::Path,
    env::current_dir,
    process::{
        Command, Output,
    },
};
use rodio::{Decoder, OutputStream, source::Source};
use structopt::StructOpt;
use colored::Colorize;

#[derive(StructOpt)]
struct Opts {
    #[structopt(default_value = "created by cytolytic!", multiple = true)]
    /// LET'S GOOOOO
    message: Vec<String>,

    #[structopt(short = "b", long = "blue")]
    /// DABABY IN SUPER SAYAN BLUE
    blue: bool,
}

fn main() {
    setup();
    println!("{:?}", current_dir().unwrap());
    let opts = Opts::from_args();
    let mut message = String::new();
    opts.message.iter().for_each(|x| {
        &message.push_str(x.as_str());
        &message.push_str(" ");
    });
    print_dababy(message.clone(), opts.blue);
    if message.to_lowercase().contains("~/.dababy/go.mp3") {
        println!("{}", "GO SPOTTED IN TEXT".red());
        let (_strm, strm_hnd) = OutputStream::try_default().unwrap();
        let lets_go = File::open(Path::new("~")).unwrap();
        let src = Decoder::new(lets_go).unwrap();
        strm_hnd.play_raw(src.convert_samples()).unwrap();
        sleep(Duration::from_secs(4));
    }
}

fn print_dababy(message: String, blue: bool) {
    println!("\n{}{}", " ".repeat(20), "-".repeat(message.chars().count() + 4));
    println!("{}| {} |", " ".repeat(20), message);
    println!("{}{}", " ".repeat(20), "-".repeat(message.chars().count() + 4));
    println!(r"{}\ /", " ".repeat(21 + message.chars().count() / 2));
    println!(r"{}\ /", " ".repeat(21 + message.chars().count() / 2));
    if blue { println!("{}", DABABY.blue()); } else { println!("{}", DABABY); }
}

fn setup() {
    let go = Path::new("~/.dababy/go.mp3").exists();
    if go {()}
    if cfg!(windows) {
        println!("{}", "running setup for windows...".green());
        let output = Command::new("powershell")
            .args(
                &["/C",
                    "mkdir ~/.dababy;cd ~./.dababy; wget 'https://github.com/cytoo/dababy/releases/download/0.1/go.mp3'"]
            )
            .output()
            .unwrap_or_else(|x| panic!(format!("{}{}", "the setup has failed!\nReason:".red(), x)))
            .stdout;
    } else {
        println!("{}", "running setup for linux...".green());
        let output = Command::new("sh")
            .arg("-c")
            .arg("mkdir ~/.dababy;cd ~./.dababy; wget 'https://github.com/cytoo/dababy/releases/download/0.1/go.mp3'")
            .output()
            .unwrap_or_else(|x| panic!(format!("{}{}", "the setup has failed!\nReason:".red(), x)))
            .stdout;
    }
    println!("{}", "setup finished :D! enjoy the dababy".green());
}