using System.Collections.Generic;

namespace MyHN.Domain
{
  /// <summary>
  /// Représente un élément sur lequel un utilisateur peut voter.
  /// </summary>
  public abstract class Votable
  {
    private List<Vote> _votes;

    public IReadOnlyList<Vote> Votes => _votes;

    protected Votable()
    {
      _votes = new List<Vote>();
    }

    public void UpvoteBy(string user)
    {
      UpdateVote(VoteType.Up, user);
    }

    public void DownvoteBy(string user)
    {
      UpdateVote(VoteType.Down, user);
    }

    private void UpdateVote(VoteType type, string user)
    {
      var userVote = _votes.Find(o => o.CreatedBy == user);

      if (userVote != null)
      {
        userVote.Direction = type;
        return;
      }

      _votes.Add(new Vote(type, user));
    }
  }
}