use crate::Polygon;
use iced::Color;
use kira::sound::static_sound::StaticSoundData;
use regex::Regex;

pub fn is_delta_format_valid(str: &str) -> bool {
    let re = Regex::new(r"^\d{1,2}:\d{1,2}:\d{1,2}$").unwrap();
    re.is_match(str)
}
pub fn string_to_color<S: AsRef<str>>(s: S) -> Color {
    match s.as_ref() {
        "Green" => Color::from_rgb(0.0, 1.0, 0.0),
        "Blue" => Color::from_rgb(0.0, 0.0, 1.0),
        "Cyan" => Color::from_rgb(0.0, 1.0, 1.0),
        "Yellow" => Color::from_rgb(1.0, 1.0, 0.0),
        "Pink" => Color::from_rgb(1.0, 0.0, 1.0),
        _ => Color::BLACK,
    }
}

pub fn delta_to_string(delta: f32) -> String {
    let time = [
        ((delta / 60.0) as u32),
        ((delta % 60.0) as u32),
        ((delta * 100.0) % 100.0) as u32,
    ];
    let mut time_str = [String::new(), String::new(), String::new()];
    for i in 0..3 {
        if time[i] < 10 {
            time_str[i] = format!("0{}", time[i]);
        } else {
            time_str[i] = time[i].to_string();
        }
    }

    format!("{}:{}:{}", time_str[0], time_str[1], time_str[2])
}
pub fn str_to_sec(str: &str) -> f32 {
    let parts: Vec<&str> = str.split(':').collect();

    let min: f32 = parts[0].parse().expect("str of music length not valid");
    let sec: f32 = parts[1].parse().expect("str of music length not valid");
    let cs: f32 = parts[2].parse().expect("str of music length not valid");
    min * 60.0 + sec + (cs / 100.0)
}

pub fn string_to_polygon<S: AsRef<str>>(str: S) -> Polygon {
    let s = str.as_ref();
    let mut poly: Polygon;
    if s.starts_with("Ngon") {
        if let Ok(sides) = s.trim_start_matches("Ngon").parse::<u8>() {
            poly = Polygon::n_gon(sides, dummy_sound());
        } else {
            poly = Polygon::n_gon(0, dummy_sound());
        }
    } else {
        match s {
            "Segment" => {
                poly = Polygon::segment(dummy_sound());
            }
            "Triangle" => {
                poly = Polygon::triangle(dummy_sound());
            }
            "Square" => {
                poly = Polygon::square(dummy_sound());
            }
            "Nr6In30" => {
                poly = Polygon::nr_6_in_30(dummy_sound());
            }
            "Nr7In30" => {
                poly = Polygon::nr_7_in_30(dummy_sound());
            }
            "Nr8In30" => {
                poly = Polygon::nr_8_in_30(dummy_sound());
            }
            "Nr9In30" => {
                poly = Polygon::nr_9_in_30(dummy_sound());
            }
            "Nr8In42" => {
                poly = Polygon::nr_8_in_42(dummy_sound());
            }
            "Nr9In42" => {
                poly = Polygon::nr_9_in_42(dummy_sound());
            }
            "Nr10aIn42" => {
                poly = Polygon::nr_10a_in_42(dummy_sound());
            }
            "Nr10bIn42" => {
                poly = Polygon::nr_10b_in_42(dummy_sound());
            }
            _ => poly = Polygon::n_gon(0, dummy_sound()),
        }
    }
    poly.name = s.to_string();
    poly
}
fn dummy_sound() -> StaticSoundData {
    StaticSoundData::from_file("assets/tick.ogg").expect("Fail to load audio")
}
