using Model.Stub;
using System.Diagnostics;
using System.Xml.Serialization;

namespace Model
{
    public class Album
    {

        public string Name
        {
            get => name;

            set
            {
                if (value != null && value.Length < Manager.MAX_NAME_LENGTH)
                {
                    name = value;
                }
            }
        }

        private string name = Manager.DEFAULT_NAME;

        public string Description
        {
            get => description;

            set
            {
                if (value != null && value.Length < Manager.MAX_DESCRIPTION_LENGTH)
                {
                    description = value;
                }
            }
        }

        private string description = Manager.DEFAULT_DESC;

        private readonly List<Title> titles = new List<Title>();

        public IEnumerable<Title> Titles
        { 
            get
            {
                return new List<Title>(titles);
            }
        }

        public string ImageURL
        {
            get => imageURL;

            set
            {
                if (value == null || !value.Contains('.'))
                {
                    value = Manager.DEFAULT_URL;
                    imageURL = value;
                }
                if (value.Contains(' '))
                {
                    imageURL = value.Replace(' ', '_');
                }
                else if (value.Contains('.'))
                {
                    imageURL = value;
                }
            }
        }

        private string imageURL = Manager.DEFAULT_URL;

        public Artist Artist { get; set; }

        public string Information
        {
            get => information;

            set
            {
                if (value != null && value.Length < Manager.MAX_DESCRIPTION_LENGTH)
                {
                    information = value;
                }
            }
        }

        private string information = Manager.DEFAULT_DESC;

        public Album(string name, string imageURL, Artist artist, string description, string information)
        {
            Name = name;
            ImageURL = imageURL;
            Artist = artist;
            Description = description;
            Information = information;
        }

        public Album()
        {
            Artist = new Artist(Manager.DEFAULT_NAME);
        }

        public void AddTitle(Title title)
        {
            titles.Add(title);
        }

        public void RemoveTitle(Title title)
        {
            titles.Remove(title);
        }

        public override bool Equals(object? obj)
        {
            if (obj is null) return false;
            if (obj.GetType() != typeof(Album)) return false;
            if (obj is Album album && Name == album.Name) return true;
            else return false;
        }

        public override int GetHashCode()
        {
            return ImageURL.GetHashCode();
        }

        public override string ToString()
        {
            return $"Name : {Name}, Artist : {Artist}";
        }
    }
}