use lofty::{config::WriteOptions, prelude::*, probe::Probe, tag::Tag};
use std::path::Path;

pub fn write_tags(path: &Path, title: &str, artist: &str, album: &str, track_num: u32) {
    let mut tagged_file = Probe::open(path)
        .expect("couldn't open the flac :(\n")
        .read()
        .expect("couldn't read the flac :(\n");
    let tag = match tagged_file.primary_tag_mut() {
        Some(t) => t,
        None => {
            if let Some(first_tag) = tagged_file.first_tag_mut() {
                first_tag
            } else {
                let tag_type = tagged_file.primary_tag_type();
                tagged_file.insert_tag(Tag::new(tag_type));
                tagged_file.primary_tag_mut().unwrap()
            }
        }
    };

    tag.set_title(title.to_string());
    tag.set_artist(artist.to_string());
    tag.set_album(album.to_string());
    tag.set_track(track_num);

    tag.save_to_path(path, WriteOptions::default())
        .expect("couldn't save the tags :(\n");
}
