using Common.Library;
using Gready_Poang.EntityLayer;
using Gready_Poang.Services;
using System.Collections.ObjectModel;


namespace Gready_Poang.ViewModelLayer;

public class ParticipantViewModel : ViewModelBase
{
    #region Constructors
    public ParticipantViewModel() : base()
    {

    }

    public ParticipantViewModel(
        IRepository<Participant> repo,
        IMethodSharingService<Participant> sharingService,
        AppShellViewModel appShell,
        IObjectMessageService objectMessage) : base()
    {
        _Repository = repo;
        _sharingService = sharingService;
        _appShell = appShell;
        _objectMessage = objectMessage;
    }

    #endregion

    #region Private Variables
    private Participant? _ParticipantObject = new();
    private ObservableCollection<Participant> _ParticipantList = new();
    private readonly IRepository<Participant>? _Repository;
    private readonly IMethodSharingService<Participant> _sharingService;
    private readonly AppShellViewModel _appShell;
    private readonly IObjectMessageService _objectMessage;
    #endregion

    #region public Properties

    public Participant? ParticipantObject
    {
        get { return _ParticipantObject; }
        set
        {
            _ParticipantObject = value;
            RaisePropertyChanged(nameof(ParticipantObject));
        }
    }

    public ObservableCollection<Participant> ParticipantList
    {
        get { return _ParticipantList; }
        set
        {
            _ParticipantList = value;
            RaisePropertyChanged(nameof(ParticipantList));
        }
    }

    #endregion

    #region Get Method
    public ObservableCollection<Participant> Get()
    {
        if (_appShell.RoundRunningVisible == false)
        {
            _objectMessage.Delivered = true;
        }
        ParticipantList = _sharingService.Get();
        return ParticipantList;
    }



    #endregion

    #region Get(id) Method
    public Participant? Get(int id)
    {
        try
        {
            ParticipantObject = _Repository?.Get(id).GetAwaiter().GetResult();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine($"Error in Get method: {ex.Message}");
        }

        return ParticipantObject;
    }
    public virtual bool Save()
    {
        if (_Repository == null || ParticipantObject == null)
        {
            return false;
        }
        var tmpTask = _Repository.Save(ParticipantObject);
        int tmp = tmpTask.GetAwaiter().GetResult();
        if (tmp != -1)
        {
            ParticipantObject = new Participant();
            this.Get();
        }
        return tmp != -1;
    }
    #endregion
}