﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using SimilarDetails.Utils;

namespace SimilarDetails.Model
{
    public class Detail
    {
        public Detail()
        {
            RelatedWith = new List<Link>();
            RelatedTo = new List<Link>();
        }

        [Key]
        public int DetailId { get; set; }
        [Required, Index("IX_Name_Article", 1, IsUnique = true), MaxLength(50)]
        public string Name { get; set; }
        [Required, Index("IX_Name_Article", 2, IsUnique = true), MaxLength(50)]
        public string Article { get; set; }

        [NotMapped]
        public string Description => Name + "/" + Article;

        [InverseProperty("From")]
        public virtual ICollection<Link> RelatedWith { get; set; }
        [InverseProperty("To")]
        public virtual ICollection<Link> RelatedTo { get; set; }

        public override string ToString()
        {
            return Name + "/" + Article;
        }
    }

    public class DetailsComparer : IEqualityComparer<Detail>
    {
        public bool Equals(Detail x, Detail y)
        {
            bool nameEquality = StringUtils.DeleteExtraSpaces(x.Name.ToLower()) == StringUtils.DeleteExtraSpaces(y.Name.ToLower());
            bool articleEquality = StringUtils.DeleteExtraChars(x.Article.ToLower()) == StringUtils.DeleteExtraChars(y.Article.ToLower());
            return nameEquality && articleEquality;
        }

        public int GetHashCode(Detail detail)
        {
            return detail.Name.GetHashCode()*3 + detail.Article.GetHashCode();
        }
    }
}
