use std::time::Duration;

use edar::{FormatDuration, Metadata, MetadataError, extract_metadata};

const SONG_LOCATION: &str = "tests/assets/test_song";

fn assert_metadata(metadata: Metadata, is_wav: bool) {
    assert_eq!("A_Song", metadata.title.unwrap());
    assert_eq!("darrkenn", metadata.artist.unwrap());
    assert_eq!("A_Album", metadata.album.unwrap());
    assert_eq!("A_Genre", metadata.genre.unwrap());
    if !is_wav {
        assert!(metadata.cover.is_some())
    }
    assert!(metadata.time_stamp.is_some());
    assert_eq!(Duration::from_millis(1272), metadata.duration);
    assert_eq!(48000, metadata.sample_rate.unwrap());
    assert_eq!(1, metadata.channels.unwrap());
    assert_eq!("00:01", metadata.duration.format());
    assert!(metadata.has_tag);
}

#[test]
fn flac_metadata() {
    match extract_metadata(format!("{SONG_LOCATION}.flac").as_str()) {
        Ok(md) => assert_metadata(md, false),
        Err(e) => {
            panic!("{e}")
        }
    }
}

#[test]
fn mp3_metadata() {
    match extract_metadata(format!("{SONG_LOCATION}.mp3").as_str()) {
        Ok(md) => assert_metadata(md, false),
        Err(e) => {
            panic!("{e}")
        }
    }
}

#[test]
fn ogg_metadata() {
    match extract_metadata(format!("{SONG_LOCATION}.ogg").as_str()) {
        Ok(md) => assert_metadata(md, false),
        Err(e) => {
            panic!("{e}")
        }
    }
}

#[test]
fn wav_metadata() {
    match extract_metadata(format!("{SONG_LOCATION}.wav").as_str()) {
        Ok(md) => assert_metadata(md, true),
        Err(e) => {
            panic!("{e}")
        }
    }
}

#[test]
fn metadata_read_error() {
    match extract_metadata("") {
        Ok(_) => panic!("Not possible"),
        Err(e) => match e {
            MetadataError::ReadError => {}
            MetadataError::NoTag => panic!("Not possible"),
        },
    }
}
