using Model.Stub;
using System.Diagnostics.Metrics;
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using static System.Reflection.Metadata.BlobBuilder;
using System.Reflection.Metadata;
using System.IO;

namespace Model.Serialization;

public class LinqXmlSerialization : IDataManager
{
    private static string XMLPATH = Path.Combine(Environment.CurrentDirectory, "Data");

    private static string XMLFILEPLAYLISTS = Path.Combine(XMLPATH, "playlists.xml");

    private static string XMLFILEALBUMS = Path.Combine(XMLPATH, "albums.xml");

    private static string XMLFILECUSTOMS = Path.Combine(XMLPATH, "customs.xml");

    private static string XMLFILEINFOS = Path.Combine(XMLPATH, "infos.xml");

    private static string XMLFILEARTISTS = Path.Combine(XMLPATH, "artists.xml");

    private List<Artist> artists;

    public IEnumerable<Artist> Artists
    {
        get
        {
            return new List<Artist>(artists);
        }
    }

    private List<Album> albums;

    public IEnumerable<Album> Albums
    {
        get
        {
            return new List<Album>(albums);
        }
    }

    private List<Playlist> playlists;

    public IEnumerable<Playlist> Playlists
    {
        get
        {
            return new List<Playlist>(playlists);
        }
    }

    private List<InfoTitle> infoTitles;

    public IEnumerable<InfoTitle> InfoTitles
    {
        get
        {
            return new List<InfoTitle>(infoTitles);
        }
    }

    private List<CustomTitle> customTitles;

    public IEnumerable<CustomTitle> CustomTitles
    {
        get
        {
            return new List<CustomTitle>(customTitles);
        }
    }

    public LinqXmlSerialization()
    {
        playlists = new List<Playlist>();
        artists = new List<Artist>();
        albums = new List<Album>();
        infoTitles = new List<InfoTitle>();
        customTitles = new List<CustomTitle>();
        if (!Directory.Exists(XMLPATH))
        {
            Directory.CreateDirectory(XMLPATH);
        }
        Directory.SetCurrentDirectory(XMLPATH);
        LoadSerialization();
    }

    public void AddAlbum(Album album)
    {
        albums.Add(album);
    }

    public void AddArtist(Artist artist)
    {
        artists.Add(artist);
    }

    public void AddCustomTitle(CustomTitle title)
    {
        customTitles.Add(title);
    }

    public void AddInfoTitle(InfoTitle title)
    {
        infoTitles.Add(title);
    }

    public void AddPlaylist(Playlist playlist)
    {
        playlists.Add(playlist);
    }

    public List<Album> GetAlbums()
    {
        return albums;
    }

    public List<Artist> GetArtists()
    {
        return artists;
    }

    public List<CustomTitle> GetCustomTitles()
    {
        return customTitles;
    }

    public List<InfoTitle> GetInfoTitles()
    {
        return infoTitles;
    }

    public List<Playlist> GetPlaylists()
    {
        return playlists;
    }

    public void RemoveAlbum(Album album)
    {
        albums.Remove(album);
    }

    public void RemoveArtist(Artist artist)
    {
        artists.Remove(artist);
    }

    public void RemoveCustomTitle(CustomTitle title)
    {
        customTitles.Remove(title);
    }

    public void RemoveInfoTitle(InfoTitle title)
    {
        infoTitles.Remove(title);
    }

    public void RemovePlaylist(Playlist playlist)
    {
        playlists.Remove(playlist);
    }

    public void LoadSerialization()
    {
        LoadArtists();
        LoadCustomTitles();
        LoadInfoTitles();
        LoadPlaylists();
        LoadAlbums();
    }

    public void SaveSerialization()
    {
        SaveArtists();
        SaveCustomTitles();
        SaveInfoTitles();
        SavePlaylists();
        SaveAlbums();
    }

    public void LoadPlaylists()
    {
        if (!File.Exists(XMLFILEPLAYLISTS))
        {
            XDocument PlaylistFile = new XDocument();
            Playlist p1 = new Playlist();
            playlists.Add(p1);

            var playlist = playlists.Select(p => new XElement("Playlist",
                                                  new XAttribute("Name", p.Name),
                                                  new XElement("Description", p.Description),
                                                  new XElement("ImageURL", p.ImageURL),
                                                  new XElement("Titles", p.Titles.Any() ? p.Titles.Select(a => a.ImageURL).Aggregate((albumUrl, nextAlbum) => albumUrl + " " + nextAlbum) : "")
                                                  ));

            PlaylistFile.Add(new XElement("Playlists", playlist));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            using (TextWriter tw = File.CreateText(XMLFILEPLAYLISTS))
            {
                using (XmlWriter writer = XmlWriter.Create(tw, settings))
                {
                    PlaylistFile.Save(writer);
                }
            }
        }
        XDocument PlaylistsFichier = XDocument.Load(XMLFILEPLAYLISTS);

        playlists = PlaylistsFichier.Descendants("Playlist")
                                    .Select(eltPlaylist => new Playlist(
                                        eltPlaylist.Attribute("Name")!.Value,
                                        eltPlaylist.Element("Description")!.Value,
                                        eltPlaylist.Element("ImageURL")!.Value
                                    )).ToList();
        foreach (Playlist playlist in playlists)
        {
            var custom = PlaylistsFichier.Descendants("Playlist")
                                                 .Single(ct => ct.Attribute("Name")?.Value == playlist.Name)
                                                 .Element("Titles")!.ToString();
            if (custom == null)
            {
                continue;
            }

            CustomTitle? customTitle = GetCustomTitleByUrl(custom);

            if (customTitle == null)
            {
                continue;
            }

            playlist.AddTitle(customTitle);
        }
    }

    public void SavePlaylists()
    {
        XDocument PlaylistsFichier = new XDocument();

        var playlist = playlists.Select(playlist => new XElement("Playlist",
                                                    new XAttribute("Name", playlist.Name),
                                                    new XElement("Description", playlist.Description),
                                                    new XElement("ImageURL", playlist.ImageURL),
                                                    new XElement("Titles", playlist.Titles.Any() ? playlist.Titles.Select(p => p.ImageURL).Aggregate((playlistUrl, nextPlaylist) => playlistUrl + " " + nextPlaylist) : "")
                                                    ));
        PlaylistsFichier.Add(new XElement("Playlists", playlist));

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        using(TextWriter tw = File.CreateText(XMLFILEPLAYLISTS))
        {
            using(XmlWriter writer  = XmlWriter.Create(tw, settings))
            {
                PlaylistsFichier.Save(writer);
            }
        }
    }
        
    public void LoadArtists()
    {
        if (!File.Exists(XMLFILEARTISTS))
        {
            XDocument ArtistFile = new XDocument();
            Artist a1 = new Artist("a1");
            Artist a2 = new Artist("a2");

            artists.Add(a1);
            artists.Add(a2);

            var artist = artists.Select(artist => new XElement("Artist",
                                                  new XAttribute("Name", artist.Name)
                                                  ));

            ArtistFile.Add(new XElement("Artists", artist));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            using (TextWriter tw = File.CreateText(XMLFILEARTISTS))
            {
                using (XmlWriter writer = XmlWriter.Create(tw, settings))
                {
                    ArtistFile.Save(writer);
                }
            }
        }

        XDocument ArtistsFile = XDocument.Load(XMLFILEARTISTS);
        artists = ArtistsFile.Descendants("Artist")
                             .Select(eltArtist => new Artist(
                                     eltArtist.Attribute("Name")!.Value
                              )).ToList();

    }

    public void SaveArtists()
    {
        XDocument ArtistsFile = new XDocument();
        var artist = artists.Select(artist => new XElement("Artist",
                                              new XAttribute("Name", artist.Name)
                                              ));

        ArtistsFile.Add(new XElement("Artists", artist));

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        using (TextWriter tw = File.CreateText(XMLFILEARTISTS))
        {
            using (XmlWriter writer = XmlWriter.Create(tw, settings))
            {
                ArtistsFile.Save(writer);
            }
        }

    }

    public void LoadCustomTitles()
    {
        if (!File.Exists(XMLFILECUSTOMS))
        {

            XDocument CustomsFile2 = new XDocument();
            CustomTitle ct1 = new CustomTitle("ct1", "url1.png", "info1", "path1");
            CustomTitle ct2 = new CustomTitle("ct2", "url2.png", "info2", "path2");

            customTitles.Add(ct1);
            customTitles.Add(ct2);

            var custom = customTitles.Select(c => new XElement("CustomTitle",
                                                  new XAttribute("Name", c.Name),
                                                  new XElement("ImageURL", c.ImageURL),
                                                  new XElement("Information", c.Information),
                                                  new XElement("Path",c.Path)
                                                  ));

            CustomsFile2.Add(new XElement("Customs", custom));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            using (TextWriter tw = File.CreateText(XMLFILECUSTOMS))
            {
                using (XmlWriter writer = XmlWriter.Create(tw, settings))
                
                    CustomsFile2.Save(writer);
            }
        }
        XDocument CustomsFile = XDocument.Load(XMLFILECUSTOMS);

        customTitles = CustomsFile.Descendants("CustomTitle")
                                    .Select(eltPlaylist => new CustomTitle(
                                        eltPlaylist.Attribute("Name")!.Value,
                                        eltPlaylist.Element("ImageURL")!.Value,
                                        eltPlaylist.Element("Information")!.Value,
                                        eltPlaylist.Element("Path")!.Value
                                    )).ToList();
    }

    public void SaveCustomTitles()
    {
        XDocument CustomsFile = new XDocument();

        var customs = customTitles.Select(custom => new XElement("CustomTitle",
                                                    new XAttribute("Name", custom.Name),
                                                    new XElement("ImageURL", custom.ImageURL),
                                                    new XElement("Information", custom.Information),
                                                    new XElement("Path", custom.Path)
                                                    ));

        CustomsFile.Add(new XElement("Customs", customs));

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        using (TextWriter tw = File.CreateText(XMLFILECUSTOMS))
        {
            using (XmlWriter writer = XmlWriter.Create(tw, settings))
            {
                CustomsFile.Save(writer);
            }
        }
    }

    public void LoadAlbums()
    {
        if (!File.Exists(XMLFILEALBUMS))
        {
            XDocument AlbumFile = new XDocument();
            Album a1 = new Album();
            albums.Add(a1);

            var album = albums.Select(p => new XElement("Album",
                                                  new XAttribute("Name", p.Name),
                                                  new XElement("ImageURL", p.ImageURL),
                                                  new XElement("Artist", p.Titles.Any() ? p.Titles.Select(a => a.Name).Aggregate((artistName, nextArtist) => artistName + " " + nextArtist) : ""),
                                                  new XElement("Description", p.Description),
                                                  new XElement("Information", p.Information),
                                                  new XElement("Titles", p.Titles.Any() ? p.Titles.Select(a => a.ImageURL).Aggregate((albumUrl, nextAlbum) => albumUrl + " " + nextAlbum) : "")
                                                  ));

            AlbumFile.Add(new XElement("Albums", album));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            using (TextWriter tw = File.CreateText(XMLFILEALBUMS))
            {
                using (XmlWriter writer = XmlWriter.Create(tw, settings))
                {
                    AlbumFile.Save(writer);
                }
            }
        }
        XDocument AlbumsFile = XDocument.Load(XMLFILEALBUMS);

        albums = AlbumsFile.Descendants("Album")
                            .Select(eltPlaylist => new Album(
                                eltPlaylist.Attribute("Name")!.Value,
                                eltPlaylist.Element("ImageURL")!.Value,
                                GetArtistByName(eltPlaylist.Element("Artist")!.Value) ?? new Artist(),
                                eltPlaylist.Element("Description")!.Value,
                                eltPlaylist.Element("Information")!.Value
                            )).ToList();



        foreach (Album a in albums)
        {
            var title = AlbumsFile.Descendants("Album")
                                                 .Single(ct => ct.Attribute("Name")?.Value == a.Name)
                                                 .Element("Titles")!.ToString();
            if (title == null)
            {
                continue;
            }

            InfoTitle? infoTitle = GetInfoTitleByUrl(title);

            if (infoTitle == null)
            {
                continue;
            }

            a.AddTitle(infoTitle);
        }
    }

    public void SaveAlbums()
    {
        XDocument AlbumsFile = new XDocument();

        var album = albums.Select(a => new XElement("Album",
                                                    new XAttribute("Name", a.Name),
                                                    new XElement("ImageURL", a.ImageURL),
                                                    new XElement("Artist", a.Artist.Name),
                                                    new XElement("Description", a.Description),
                                                    new XElement("Information", a.Information),
                                                    new XElement("Titles", a.Titles.Any() ? a.Titles.Select(p => p.ImageURL).Aggregate((albumUrl, nextAlbum) => albumUrl + " " + nextAlbum) : "")
                                                    ));
        AlbumsFile.Add(new XElement("Albums", album));

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        using (TextWriter tw = File.CreateText(XMLFILEALBUMS))
        {
            using (XmlWriter writer = XmlWriter.Create(tw, settings))
            {
                AlbumsFile.Save(writer);
            }
        }
    }

    public void LoadInfoTitles()
    {
        if (!File.Exists(XMLFILEINFOS))
        {
            XDocument InfoFile = new XDocument();
            InfoTitle it1 = new InfoTitle();
            infoTitles.Add(it1);

            var infoTitle = infoTitles.Select(p => new XElement("InfoTitle",
                                                  new XAttribute("Name", p.Name),
                                                  new XElement("ImageURL", p.ImageURL),
                                                  new XElement("Information", p.Information),
                                                  new XElement("Feats", p.Feat.Any() ? p.Feat.Select(a => a.Name).Aggregate((artistName, nextArtist) => artistName + " " + nextArtist) : ""),
                                                  new XElement("Description", p.Description),
                                                  new XElement("Genre", p.Genre)
                                                  ));

            InfoFile.Add(new XElement("InfoTitles", infoTitle));

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            using (TextWriter tw = File.CreateText(XMLFILEINFOS))
            {
                using (XmlWriter writer = XmlWriter.Create(tw, settings))
                {
                    InfoFile.Save(writer);
                }
            }
        }
        XDocument InfosFile = XDocument.Load(XMLFILEINFOS);

        infoTitles = InfosFile.Descendants("InfoTitle")
                                    .Select(eltPlaylist => new InfoTitle(
                                        eltPlaylist.Attribute("Name")!.Value,
                                        eltPlaylist.Element("ImageURL")!.Value,
                                        eltPlaylist.Element("Information")!.Value,
                                        GetArtistByName(eltPlaylist.Element("Feats")!.Value) ?? new Artist(),
                                        eltPlaylist.Element("Description")!.Value,
                                        GetGenreByName(eltPlaylist.Element("Genre")!.Value)
                                    )).ToList();
        foreach (InfoTitle it in infoTitles)
        {
            var feat = InfosFile.Descendants("InfoTitle")
                                                 .Single(infot => infot.Attribute("Name")?.Value == it.Name)
                                                 .Element("Feats")!.ToString();
            if (feat == null)
            {
                continue;
            }

            Artist? Feat = GetArtistByName(feat);

            if (Feat == null)
            {
                continue;
            }

            it.AddFeat(Feat);
        }
    }

    public void SaveInfoTitles()
    {
        XDocument InfosFile = new XDocument();

        var info = infoTitles.Select(it => new XElement("InfoTitle",
                                                    new XAttribute("Name", it.Name),
                                                    new XElement("ImageURL", it.ImageURL),
                                                    new XElement("Information", it.Information),
                                                    new XElement("Genre", it.Genre.ToString()),
                                                    new XElement("Description", it.Description),
                                                    new XElement("Feats", it.Feat.Any() ? it.Feat.Select(p => p.Name).Aggregate((featName, nextFeat) => featName + " " + nextFeat) : "")
                                                    ));
        InfosFile.Add(new XElement("InfoTitles", info));

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        using (TextWriter tw = File.CreateText(XMLFILEINFOS))
        {
            using (XmlWriter writer = XmlWriter.Create(tw, settings))
            {
                InfosFile.Save(writer);
            }
        }
    }
    public static Genre GetGenreByName(string genre)
    {
        if (genre == "HIP_HOP") return Genre.HIP_HOP;
        if (genre == "POP") return Genre.POP;
        if (genre == "ROCK") return Genre.ROCK;
        if (genre == "ELECTRO") return Genre.ELECTRO;
        if (genre == "CLASSIQUE") return Genre.CLASSIQUE;
        if (genre == "JAZZ") return Genre.JAZZ;
        if (genre == "VARIETE_FRANCAISE") return Genre.VARIETE_FRANCAISE;
        if (genre == "VARIETE_INTERNATIONALE") return Genre.VARIETE_INTERNATIONALE;
        if (genre == "REGGAE") return Genre.REGGAE;
        if (genre == "RAP") return Genre.RAP;
        if (genre == "RNB") return Genre.RNB;
        if (genre == "DISCO") return Genre.DISCO;
        if (genre == "BLUES") return Genre.BLUES;
        if (genre == "COUNTRY") return Genre.COUNTRY;
        if (genre == "FUNK") return Genre.FUNK;
        if (genre == "GOSPEL") return Genre.GOSPEL;
        if (genre == "METAL") return Genre.METAL;
        else return Genre.K_POP;
    }

    public InfoTitle? GetInfoTitleByUrl(string url)
    {
        foreach(InfoTitle it in infoTitles)
        {
            if (it.Name == url)
            {
                return it;
            }
        }
        return null;
    }

    public Artist? GetArtistByName(string name)
    {
        foreach(Artist a in artists)
        {
            if (a.Name == name)
            {
                return a;
            }
        }
        return null;
    }

    public Album? GetAlbumByUrl(string url)
    {
        foreach(Album a in albums)
        {
            if (a.ImageURL == url)
            {
                return a;
            }
        }
        return null;
    }

    public CustomTitle? GetCustomTitleByUrl(string custom)
    {
        foreach(CustomTitle customTitle in customTitles)
        {
            if(customTitle.Name == custom)
            {
                return customTitle;
            }
        }
        return null;
    }

    public void AddAlbums(List<Album> albumsList)
    {
        foreach(Album a in albumsList)
        {
            albums.Add(a);
        }
    }

    public void AddArtists(List<Artist> artistsList)
    {
        foreach (Artist a in artistsList)
        {
            artists.Add(a);
        }
    }

    public void AddPlaylists(List<Playlist> playlistsList)
    {
        foreach (Playlist p in playlistsList)
        {
            playlists.Add(p);
        }
    }

    public void AddCustomTitles(List<CustomTitle> customTitlesList)
    {
        foreach (CustomTitle ct in customTitlesList)
        {
            customTitles.Add(ct);
        }
    }

    public void AddInfoTitles(List<InfoTitle> infoTitlesList)
    {
        foreach (InfoTitle it in infoTitlesList)
        {
            infoTitles.Add(it);
        }
    }

    public Playlist? GetPlaylistByUrl(string url)
    {
        foreach(Playlist p in playlists)
        {
            if (p.ImageURL == url)
            {
                return p;
            }
        }
        return null;
    }

    public void UpdateCustomTitle(CustomTitle title, string name, string url, string info, string path)
    {
        title.Name = name;
        title.ImageURL = url;
        title.Information = info;
        title.Path = path;
    }

    public void UpdateCustomTitleByUrl(string url, string name, string newUrl, string info, string path)
    {
        CustomTitle? title = GetCustomTitleByUrl(url);
        if (title != null)
        {
            title.Name = name;
            title.ImageURL = newUrl;
            title.Information = info;
            title.Path = path;
        }
    }

    public void UpdateInfoTitle(InfoTitle title, string name, string url, string info, Artist artist, string description, Genre genre)
    {
        title.Name = name;
        title.ImageURL = url;
        title.Information = info;
        title.Artist = artist;
        title.Description = description;
        title.Genre = genre;
    }

    public void UpdateInfoTitleByName(string url, string name, string newUrl, string info, Artist artist, string description, Genre genre)
    {
        InfoTitle? title = GetInfoTitleByUrl(url);
        if (title != null)
        {
            title.Name = name;
            title.ImageURL = newUrl;
            title.Information = info;
            title.Artist = artist;
            title.Description = description;
            title.Genre = genre;
        }
    }

    public void UpdateInfoTitleByArtistName(InfoTitle title, string name, string url, string info, string artist, string description, Genre genre)
    {
        title.Name = name;
        title.ImageURL = url;
        title.Information = info;
        Artist? artist2 = GetArtistByName(artist);
        if (artist2 != null)
        {
            title.Artist = artist2;
        }
        title.Description = description;
        title.Genre = genre;
    }

    public void UpdateInfoTitleByNameByArtistName(string url, string name, string newUrl, string info, string artist, string description, Genre genre)
    {
        InfoTitle? title = GetInfoTitleByUrl(url);
        if (title != null)
        {
            title.Name = name;
            title.ImageURL = newUrl;
            title.Information = info;
            Artist? artist2 = GetArtistByName(artist);
            if (artist2 != null)
            {
                title.Artist = artist2;
            }
            title.Description = description;
            title.Genre = genre;
        }
    }

    public void UpdateAlbum(Album album, string name, string url, Artist artist, string description, string info)
    {
        album.Name = name;
        album.ImageURL = url;
        album.Artist = artist;
        album.Description = description;
        album.Information = info;
    }

    public void UpdateAlbumByUrl(string url, string name, string newUrl, Artist artist, string description, string info)
    {
        Album? album = GetAlbumByUrl(url);
        if (album != null)
        {
            album.Name = name;
            album.ImageURL = newUrl;
            album.Artist = artist;
            album.Description = description;
            album.Information= info;
        }
    }

    public void UpdateAlbumByArtistName(Album album, string name, string url, string artist, string description, string info)
    {
        album.Name = name;
        album.ImageURL = url;
        Artist? artist2 = GetArtistByName(artist);
        if (artist2 != null)
        {
            album.Artist = artist2;
        }
        album.Description = description;
        album.Information = info;
    }

    public void UpdateAlbumByUrlByArtistName(string url, string name, string newUrl, string artist, string description, string info)
    {
        Album? album = GetAlbumByUrl(url);
        if (album != null)
        {
            album.Name = name;
            album.ImageURL = newUrl;
            Artist? artist2 = GetArtistByName(artist);
            if (artist2 != null)
            {
                album.Artist = artist2;
            }
            album.Description = description;
            album.Information = info;
        }
    }

    public void UpdatePlaylist(Playlist playlist, string name, string description, string url)
    {
        playlist.Name = name;
        playlist.Description = description;
        playlist.ImageURL = url;
    }

    public void UpdatePlaylistByUrl(string url, string name, string description, string newUrl)
    {
        Playlist? playlist = GetPlaylistByUrl(url);
        if (playlist != null)
        {
            playlist.Name = name;
            playlist.Description = description;
            playlist.ImageURL = newUrl;
        }
    }

    public void UpdateArtist(Artist artist, string name)
    {
        artist.Name = name;
    }

    public void UpdateArtistByName(string name, string newName)
    {
        Artist? artist = GetArtistByName(newName);
        if (artist != null)
        {
            artist.Name = newName;
        }
    }

    public void RemoveAlbums(List<Album> albumsList)
    {
        foreach (Album album in albumsList)
        {
            albums.Remove(album);
        }
    }

    public void RemoveArtists(List<Artist> artistsList)
    {
        foreach(Artist artist in artistsList)
        {
            artists.Remove(artist);
        }
    }

    public void RemovePlaylists(List<Playlist> playlistsList)
    {
        foreach (Playlist playlist in playlistsList)
        {
            playlists.Remove(playlist);
        }
    }

    public void RemoveCustomTitles(List<CustomTitle> customTitlesList)
    {
        foreach(CustomTitle customTitle in customTitlesList)
        {
            customTitles.Remove(customTitle);
        }
    }

    public void RemoveInfoTitles(List<InfoTitle> infoTitlesList)
    {
        foreach (InfoTitle infoTitle in infoTitlesList)
        {
            infoTitles.Remove(infoTitle);
        }
    }

    public bool ExistsPlaylist(Playlist playlist)
    {
        foreach(Playlist p in playlists)
        {
            if (p ==  playlist)
            {
                return true;
            }
        }
        return false;
    }

    public bool ExistsPlaylistByUrl(string url)
    {
        foreach(Playlist p in playlists)
        {
            if (p.ImageURL == url)
            {
                return true;
            }
        }
        return false;
    }

    public bool ExistsAlbum(Album album)
    {
        foreach(Album a in albums)
        {
            if(a == album)
            {
                return true;
            }
        }
        return false;
    }

    public bool ExistsAlbumByUrl(string url)
    {
        foreach(Album a in albums)
        {
            if (a.ImageURL == url)
            {
                return true;
            }
        }
        return false;
    }

    public bool ExistsArtist(Artist artist)
    {
        foreach (Artist a in artists)
        {
            if (a == artist)
            {
                return true;
            }
        }
        return false;
    }

    public bool ExistsArtistByName(string name)
    {
        foreach(Artist a in artists)
        {
            if(a.Name == name)
            {
                return true;
            }
        }
        return false;
    }

    public bool ExistsCustomTitle(CustomTitle title)
    {
        foreach(CustomTitle ct in customTitles)
        {
            if(title == ct)
            {
                return true;
            }
        }
        return false;
    }

    public bool ExistsCustomTitleByUrl(string url)
    {
        foreach(CustomTitle ct in customTitles)
        {
            if(ct.ImageURL == url)
            {
                return true;
            }
        }
        return false;
    }

    public bool ExistsInfoTitle(InfoTitle title)
    {
        foreach(InfoTitle it in infoTitles)
        {
            if(it == title)
            {
                return true;
            }
        }
        return false;
    }

    public bool ExistsInfoTitleByUrl(string url)
    {
        foreach(InfoTitle it in infoTitles)
        {
            if(it.ImageURL == url)
            {
                return true;
            }
        }
        return false;
    }
}
