use crate::modules::windows::process::process_names_lower;
use std::process::Command;

use crate::modules::enums::SongInfo;

pub fn get_song() -> Option<SongInfo> {
    let player = detect_player()?;

    let output = match player.as_str() {
        "spotify" => run_powershell(&[
            "powershell",
            "-Command",
            "(Get-Process -Name spotify -ErrorAction SilentlyContinue).MainWindowTitle",
        ]),
        _ => None,
    }?;

    parse_song_output(&output)
}

fn parse_song_output(output: &str) -> Option<SongInfo> {
    // Spotify's window title is usually "Artist - Song"
    let trimmed = output.trim();
    let (artist, title) = trimmed.split_once(" - ")?;

    Some(SongInfo {
        artist: artist.to_string(),
        album: "Unknown Album".into(),
        title: title.to_string(),
    })
}

fn run_powershell(cmd: &[&str]) -> Option<String> {
    let output = Command::new(cmd[0]).args(&cmd[1..]).output().ok()?;
    if !output.status.success() {
        return None;
    }
    Some(String::from_utf8_lossy(&output.stdout).to_string())
}

fn detect_player() -> Option<String> {
    let players = ["spotify.exe", "vlc.exe", "groove.exe"];
    let names = process_names_lower();
    for p in players {
        if names.iter().any(|n| n.contains(p)) {
            // Return normalized base name without extension
            return Some(p.trim_end_matches(".exe").to_string());
        }
    }
    None
}
