using Common.Library;
using CommunityToolkit.Mvvm.ComponentModel;
using GreadyPoang.EntityLayer;
using System.Collections.ObjectModel;

namespace GreadyPoang.ViewModelLayer;

public class MethodSharingService : ObservableObject, IMethodSharingService<Participant>
{
    private readonly IRepository<Participant> _repository;

    public MethodSharingService(IRepository<Participant> repository)
    {
        _repository = repository;
    }


    public ObservableCollection<Participant> Get()
    {
        ObservableCollection<Participant> _participantList = new();

        if (_repository != null)
        {
            var participantsTask = _repository.Get();
            var participants = participantsTask is Task<IEnumerable<Participant>> task
                ? task.GetAwaiter().GetResult()
                : (IEnumerable<Participant>)participantsTask;
            foreach (var participant in participants)
            {
                if (!_participantList.Any(p => p.ParticipantId == participant.ParticipantId))
                {
                    _participantList.Add(participant);
                }
            }
        }
        return _participantList;
    }
}
