mod imp;

use crate::utils::csv_song_history::{Song, SongHistoryRecord};

glib::wrapper! {
    pub struct HistoryEntry(ObjectSubclass<imp::HistoryEntry>);
}

impl HistoryEntry {
    pub fn new(song: &SongHistoryRecord) -> Self {
        glib::Object::builder()
            .property("song_name", &song.song_name)
            .property("album", &song.album)
            .property("track_key", &song.track_key)
            .property("release_year", &song.release_year)
            .property("genre", &song.genre)
            .property("recognition_date", &song.recognition_date)
            .build()

        /*
            let mut builder = builder.property("song_name", song.song_name);
            if let Some(album) = song.album {
                builder = builder.property("album", album);
            }
            if let Some(track_key) = song.track_key {
                builder = builder.property("track_key", track_key);
            }
            if let Some(release_year) = song.release_year {
                builder = builder.property("release_year", release_year);
            }
            if let Some(genre) = song.genre {
                builder = builder.property("genre", genre);
            }
            builder = builder.property("recognition_date", song.recognition_date);
        */
    }

    pub fn get_song_history_record(&self) -> SongHistoryRecord {
        SongHistoryRecord {
            song_name: self.song_name(),
            album: self.album(),
            track_key: self.track_key(),
            release_year: self.release_year(),
            genre: self.genre(),
            recognition_date: self.recognition_date(),
        }
    }

    pub fn get_song(&self) -> Song {
        Song {
            song_name: self.song_name(),
            album: self.album().filter(|val| !val.is_empty()),
            track_key: self.track_key().filter(|val| !val.is_empty()),
            release_year: self.release_year().filter(|val| !val.is_empty()),
            genre: self.genre().filter(|val| !val.is_empty()),
        }
    }
}
